def findPurelyDerivativeDirs(self, path=None): if path is None: path = LayerLocalPath(self.path) derivdirs = [] dirs = path.listdir(files=False, dirs=True, symlinks=False) for d in dirs: if len(d.listdir(symlinks=False)) == 0: derivdirs.append(d) else: derivdirs.extend(self.findPurelyDerivativeDirs(d)) return derivdirs
def run(target_layer, path, level, new_path=None, **kwargs): from layers.lib import LayerSet, Layer, LayerLocalPath from layers.cli import argtype from layers.lib import process level = argtype.level(target_layer, path)(level) targetPath = LayerLocalPath(path.absolute()) targetOrigin = targetPath.origin newOrigin = targetOrigin print(f"Moving {targetPath.localPath} ", end="") if level is not None: newOrigin = newOrigin.inLevel(level) print(f"from level {targetPath.layer.level} to {level}", end="") if level is not None and new_path is not None: print(" and ", end="") if new_path is not None: newOrigin = newOrigin.withLocalPath(new_path.absolute().relative_to( targetOrigin.layer.path)) print(f"to {newOrigin.localPath}", end="") print() print(f"{str(targetOrigin)} -> {str(newOrigin)}") process.LayerFileMoveProcess(fromFile=targetOrigin, toFile=newOrigin).start().wait() LayerSet(newOrigin.layer).sync()
def mapper(arg): max_level = len(Layer(target_layer.absolute()).layers) - 1 if isinstance(arg, int): return max(min(arg, max_level), 0) elif arg == 'top': return 0 elif arg == 'bottom': return max_level elif arg == 'down': return min( LayerLocalPath(target_file.resolve().absolute()).layer.level + 1, max_level) elif arg == 'up': return max( LayerLocalPath(target_file.resolve().absolute()).layer.level - 1, 0)
def _move(self, fromFile: LayerLocalPath, toFile: LayerLocalPath, onStatus): from time import sleep import os def getSize(path: Path): if path.is_file() or path.is_symlink(): stat = os.stat(path) return stat.st_blksize * stat.st_blocks elif path.is_dir(): total = 0 files = [Path(p) for p in os.listdir(path)] for f in files: total += getSize(path / f) return total else: return 0 fromFileSize = getSize(fromFile.path) runStatus = True def copyStatusThread(): minSleepMs = 30 while runStatus: timeToCalc = timedelta(seconds=0) startCalc = datetime.now() try: currentSize = getSize(toFile.path) except OSError: continue timeToCalc = datetime.now() - startCalc if fromFileSize != 0: onStatus(currentSize=currentSize, progress=(float(currentSize) / float(fromFileSize))) else: onStatus(currentSize=currentSize, progress=1) sleep( max(timedelta(milliseconds=minSleepMs), timeToCalc * 3).total_seconds()) copyStatusThread = Thread(target=copyStatusThread) copyStatusThread.start() fromFile.move(toFile, onTempContainerCreate=lambda path: path) runStatus = False
def purge(self): for root, dirs, files in os.walk(self.path): paths = files paths.extend(dirs) paths = [LayerLocalPath(Path(root) / p) for p in paths] for fpath in paths: if fpath.path.is_symlink(): if not fpath.path.resolve(strict=False).exists(): fpath.path.unlink()
def files(self) -> [LayerLocalPath]: #All the files in this layer _files = [] for root, dirs, files in os.walk(self.path): proot = Path(root) for f in files: if (proot / f).name != GlobalConsts.LAYER_CONFIG_FILE: if not (proot / f).is_symlink(): _files.append( LayerLocalPath(layer=self, path=(proot / f).relative_to( self.path))) return _files
def filesIn(self, level: int) -> [LayerLocalPath]: files: [LayerLocalPath] = [] layerPath = (TEST_ENV / str(self.layers[level])).absolute() for root, dirs, _files in os.walk(layerPath): for f in _files: p = Path(root) / f if p.is_file() and not p.is_symlink(): files.append( LayerLocalPath(layer=layerPath, path=p.relative_to(layerPath))) return [ f for f in files if str(f.localPath) != GlobalConsts.LAYER_CONFIG_FILE ]
def PathInLayer(arg): import os from pathlib import Path from layers.lib import LayerLocalPath, Layer, Exceptions path = Path(arg) path = path.absolute() try: llpath = LayerLocalPath(Layer.findRoot(path), path.absolute()) except Exceptions.NotALayerDirectoryError as e: print(e) return None return llpath
def dirlinks(self) -> [LayerLocalPath]: files: [LayerLocalPath] = [] for layer in TEST_STRUCT['layers']: layerPath = (TEST_ENV / layer).absolute() for root, dirs, _files in os.walk(layerPath): for f in dirs: p = Path(root) / f if p.is_symlink(): files.append( LayerLocalPath(layer=layerPath, path=p.relative_to(layerPath))) return [ f for f in files if str(f.localPath) != GlobalConsts.LAYER_CONFIG_FILE ]