| 1 | """ |
| 2 | File- and directory related helpers for resource utilities. |
| 3 | """ |
| 4 | |
| 5 | #c Copyright 2008-2025, the GAVO project <[email protected]> |
| 6 | #c |
| 7 | #c This program is free software, covered by the GNU GPL. See the |
| 8 | #c COPYING file in the source distribution. |
| 9 | |
| 10 | |
| 11 | import osscip-python python python-stdlib 3.11 os/__init__: |
| 12 | import rescip-python python python-stdlib 3.11 re/__init__: |
| 13 | import warningsscip-python python python-stdlib 3.11 warnings/__init__: |
| 14 | |
| 15 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import basescip-python python gavodachs 2.13 `gavo.base`/__init__: |
| 16 | from gavoscip-python python gavodachs 2.13 gavo/__init__: import rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__: |
| 17 | from gavo.basescip-python python gavodachs 2.13 `gavo.base`/__init__: import parsecontextscip-python python gavodachs 2.13 `gavo.base.parsecontext`/__init__: |
| 18 | |
| 19 | class Errorscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/Error#(basescip-python python gavodachs 2.13 `gavo.base`/__init__:.Errorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#): |
| 20 | pass |
| 21 | |
| 22 | |
| 23 | fnamePatscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/fnamePat. = rescip-python python python-stdlib 3.11 re/__init__:.compilescip-python python python-stdlib 3.11 re/compile().(r"([^.]*)(\..*)") |
| 24 | def stingySplitextscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/stingySplitext().(fNamescip-python python gavodachs 2.13 `gavo.helpers.filestuff`/stingySplitext().(fName)): |
| 25 | """returns name, extension for fName. |
| 26 | |
| 27 | The main difference to os.path.splitext is that the main name is not allowed |
| 28 | to contain dots and the extension can contain more than one dot. |
| 29 | |
| 30 | fName is supposed to be a single file name without any path specifier |
| 31 | (you might get away with it if your directories do not contain dots, though). |
| 32 | """ |
| 33 | mat = fnamePatscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/fnamePat..matchscip-python python python-stdlib 3.11 re/Pattern#match().(fNamescip-python python gavodachs 2.13 `gavo.helpers.filestuff`/stingySplitext().(fName)) |
| 34 | if mat: |
| 35 | return mat.groupscip-python python python-stdlib 3.11 re/Match#group().(1), mat.groupscip-python python python-stdlib 3.11 re/Match#group().(2) |
| 36 | else: |
| 37 | return fNamescip-python python gavodachs 2.13 `gavo.helpers.filestuff`/stingySplitext().(fName), "" |
| 38 | |
| 39 | |
| 40 | class FileRenamerscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#(objectscip-python python python-stdlib 3.11 builtins/object#): |
| 41 | """is a name mapper for file rename operations and the like. |
| 42 | |
| 43 | Warning: This whole thing more or less pretends there are no |
| 44 | symlinks. |
| 45 | """ |
| 46 | def __init__scip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(self), mapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(map), showOnlyscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(showOnly)=False): |
| 47 | selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(self).mapscip-python python python-stdlib 3.11 builtins/map#, selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(self).showOnlyscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#showOnly. = mapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(map), showOnlyscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#__init__().(showOnly) |
| 48 | |
| 49 | @classmethodscip-python python python-stdlib 3.11 builtins/classmethod# |
| 50 | def loadFromFilescip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#loadFromFile().(clsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#loadFromFile().(cls), inFscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#loadFromFile().(inF), **kwargsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#loadFromFile().(kwargs)): |
| 51 | """returns a name map for whatever is serialized in the file inF. |
| 52 | |
| 53 | The format of fName is line-base, with each line being one of |
| 54 | |
| 55 | - empty -- ignored |
| 56 | - beginning with a hash -- ignored |
| 57 | - <old> -> <new> -- introducing a map |
| 58 | """ |
| 59 | map = {} |
| 60 | try: |
| 61 | for ln in inFscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#loadFromFile().(inF): |
| 62 | if not ln.strip() or ln.strip().startswith("#"): |
| 63 | continue |
| 64 | old, new = [s.strip() for s in ln.split("->", 2)] |
| 65 | if old in map: |
| 66 | raise Errorscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/Error#("Two mappings for %s"%old) |
| 67 | map[old] = new |
| 68 | except ValueErrorscip-python python python-stdlib 3.11 builtins/ValueError#: |
| 69 | raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.uiscip-python python gavodachs 2.13 `gavo.base`/ui..logOldExcscip-python python gavodachs 2.13 `gavo.base.events`/EventDispatcher#logOldExc().(Errorscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/Error#("Invalid mapping line: %s"%repr(ln))) |
| 70 | return clsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#loadFromFile().(cls)(map, **kwargsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#loadFromFile().(kwargs)) |
| 71 | |
| 72 | def getFileMapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#getFileMap().(selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#getFileMap().(self), pathscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#getFileMap().(path)): |
| 73 | """returns a dictionary old->new of renames within path. |
| 74 | """ |
| 75 | fileMap = {} |
| 76 | for dir, subdirs, fNames in osscip-python python python-stdlib 3.11 os/__init__:.walkscip-python python python-stdlib 3.11 os/walk().(pathscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#getFileMap().(path)): |
| 77 | for fName in fNames: |
| 78 | stem, ext = stingySplitextscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/stingySplitext().(fName) |
| 79 | if stem in selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#getFileMap().(self).mapscip-python python python-stdlib 3.11 builtins/map#: |
| 80 | fileMap[osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..joinscip-python python python-stdlib 3.11 ntpath/join().(dir, fName)] = osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..joinscip-python python python-stdlib 3.11 ntpath/join().(dir, |
| 81 | selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#getFileMap().(self).mapscip-python python python-stdlib 3.11 builtins/map#[stem]+ext) |
| 82 | return fileMap |
| 83 | |
| 84 | def makeRenameProcscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(self), fileMapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(fileMap)): |
| 85 | """returns a sequence of (old,new) pairs that, when executed, keep |
| 86 | any new from clobbering any existing old. |
| 87 | |
| 88 | The function will raise an Error if there's a cycle in fileMap. fileMap |
| 89 | will be destroyed by this procedure |
| 90 | """ |
| 91 | proc = [] |
| 92 | def addOpscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(srcscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(src), destscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(dest), sourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(sources)=None): |
| 93 | if sourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(sources) is None: |
| 94 | sourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(sources) = setscip-python python python-stdlib 3.11 builtins/set#() |
| 95 | if destscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(dest) in sourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(sources): |
| 96 | raise Errorscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/Error#("Rename cycle involving %s"%sourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(sources)) |
| 97 | if destscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(dest) in fileMapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(fileMap): |
| 98 | sourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(sources).addscip-python python python-stdlib 3.11 builtins/set#add().(srcscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(src)) |
| 99 | addOpscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(destscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(dest), fileMapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(fileMap).pop(destscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(dest)), sourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(sources)) |
| 100 | proc.appendscip-python python python-stdlib 3.11 builtins/list#append().((srcscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(src), destscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(dest))) |
| 101 | while fileMapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(fileMap): |
| 102 | addOpscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#addOp().(*fileMapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(fileMap).popitem()) |
| 103 | return proc |
| 104 | |
| 105 | def renameInPathscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(self), pathscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(path)): |
| 106 | """performs a name map below path. |
| 107 | |
| 108 | The rules are: |
| 109 | |
| 110 | - extensions are ignored -- if we map foo to bar, foo.txt and foo.asc |
| 111 | will be renamed bar.txt and foo.txt respectively |
| 112 | - the order of statements in the source is irrelevant. However, we try |
| 113 | not to clobber anything we've just renamed and will complain about |
| 114 | cycles. Also, each file will be renamed not more than once. |
| 115 | """ |
| 116 | fileMap = selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(self).getFileMapscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#getFileMap().(pathscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(path)) |
| 117 | for src, dest in selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(self).makeRenameProcscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#makeRenameProc().(fileMap): |
| 118 | if osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..existsscip-python python python-stdlib 3.11 ntpath/join().(dest): |
| 119 | raise Errorscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/Error#("Request to clobber %s"%repr(dest)) |
| 120 | if osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..existsscip-python python python-stdlib 3.11 ntpath/join().(src): |
| 121 | if selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(self).showOnlyscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#showOnly.: |
| 122 | printscip-python python python-stdlib 3.11 builtins/print().("%s -> %s"%(src, dest)) |
| 123 | else: |
| 124 | osscip-python python python-stdlib 3.11 os/__init__:.renamescip-python python python-stdlib 3.11 os/rename().(src, dest) |
| 125 | else: |
| 126 | if not osscip-python python python-stdlib 3.11 os/__init__:.pathscip-python python python-stdlib 3.11 os/path..existsscip-python python python-stdlib 3.11 ntpath/join().(dest): |
| 127 | warningsscip-python python python-stdlib 3.11 warnings/__init__:.warnscip-python python python-stdlib 3.11 _warnings/warn().("Neither source nor dest found in pair %s, %s"%(src, |
| 128 | dest)) |
| 129 | |
| 130 | def renameInPathsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPaths().(selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPaths().(self), pathListscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPaths().(pathList)): |
| 131 | for path in pathListscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPaths().(pathList): |
| 132 | selfscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPaths().(self).renameInPathscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/FileRenamer#renameInPath().(path) |
| 133 | |
| 134 | |
| 135 | def iterSourcesscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/iterSources().(ddIdscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/iterSources().(ddId), argsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/iterSources().(args)=[]): |
| 136 | """iterates over the current sources of the data descriptor ddId (which is |
| 137 | qualified like rdId#id |
| 138 | |
| 139 | If you pass something nonempty to args, an iterator over its values |
| 140 | will be returned. This is for convenient implementation of scripts |
| 141 | that work on CL arguments if given, on all files otherwise. |
| 142 | """ |
| 143 | if argsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/iterSources().(args): |
| 144 | return iterscip-python python python-stdlib 3.11 builtins/iter().(argsscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/iterSources().(args)) |
| 145 | else: |
| 146 | if ddIdscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/iterSources().(ddId).count("#")!=1: |
| 147 | raise basescip-python python gavodachs 2.13 `gavo.base`/__init__:.ReportableErrorscip-python python gavodachs 2.13 `gavo.base.structure`/Structure#("iterSources must get a fully qualified id" |
| 148 | " (i.e., one with exactly one hash).") |
| 149 | dd = parsecontextscip-python python gavodachs 2.13 `gavo.base.parsecontext`/__init__:.resolveCrossIdscip-python python gavodachs 2.13 `gavo.base.parsecontext`/resolveCrossId().(ddIdscip-python python gavodachs 2.13 `gavo.helpers.filestuff`/iterSources().(ddId), rscdefscip-python python gavodachs 2.13 `gavo.rscdef`/__init__:.DataDescriptorscip-python python gavodachs 2.13 `gavo.rscdef.dddef`/DataDescriptor#) |
| 150 | return dd.sources.iterSources() |