def checkDuplicatesCmdMain(cmdArgs): folders = cmdArgs.FOLDERS if len(folders) == 0: folders = [pathlib.Path()] db = FileDb.FileDb() for dbPath in folders: db.addIndexedTree(pathlib.Path(), dbPath) storageBase = cmdArgs.storageBase duplicates = list() for _, fileInfo in db.entries(): if fileInfo.duplicate is not None: duplicates.append(fileInfo) success = True for fileInfo in sorted(duplicates, key=lambda x: x.id): duplicate = fileInfo.duplicate while duplicate is not None: if not checkDuplicates(storageBase, fileInfo, duplicate): success = False print( f"'{fileInfo.filePath}' and '{duplicate.filePath}' are binary different" ) duplicate = duplicate.duplicate return 0 if success else 1
def process(self, folders: List[pathlib.Path]): success = True for folder in folders: with FileDb.ChecksumFileReader(folder.joinpath( self.__checksumFile)) as reader: for filePath, cs in reader: if not self.__restoreFile(cs, folder, filePath): success = False return success
def addCachedChecksums(self, checksumFile: pathlib.Path, filterPath: Optional[pathlib.Path]): with FileDb.ChecksumFileReader(checksumFile) as reader: for fp, c in reader: if filterPath is not None: try: fp = fp.relative_to(filterPath) except ValueError: continue self.__cachedChecksums[fp] = c
def processChecksumFile(self, cachedChecksums: pathlib.Path, filterPath: Optional[pathlib.Path]): with FileDb.ChecksumFileReader(cachedChecksums) as reader: basePath = pathlib.Path() for fp, c in reader: if filterPath is not None: try: fp = fp.relative_to(filterPath) except ValueError: continue if not self.isExcluded(fp): self.__action(basePath, fp, self.__findFileByChecksum(fp, c))
def restoreCmdMain(cmdArgs): db = FileDb.FileDb() for dbPath in cmdArgs.db: db.addIndexedTree(pathlib.Path(), dbPath) restoreCmd = RestoreCommand(db=db, dbStorage=cmdArgs.dbStorage, checksumFile=cmdArgs.checksumFile, skipExisting=cmdArgs.skipExisting) folders = cmdArgs.FOLDERS if len(folders) == 0: folders = [pathlib.Path()] success = restoreCmd.process(folders) return 0 if success else 1
def indexCmdMain(cmdArgs): indexFileName = cmdArgs.checksumFile folders = cmdArgs.FOLDERS if len(folders ) > 1 and indexFileName is not None and indexFileName.is_absolute(): raise ValueError( 'absolute path to checksum file is given and multiple folders specified' ) if len(folders) == 0: folders = [pathlib.Path()] fileTreeIterator = createFileTreeIterator(cmdArgs) create = cmdArgs.indexAction != 'verify' verify = cmdArgs.indexAction != 'create' rejectChanges = create and cmdArgs.changesMode == 'reject' reviewChanges = create and cmdArgs.changesMode == 'review' success = True try: for folder in folders: indexBuilder = FileDb.IndexBuilder( folder=folder, indexFileName=indexFileName, fileTreeIterator=fileTreeIterator, create=create, verify=verify, rejectChanges=rejectChanges, reviewChanges=reviewChanges, reuseChecksums=cmdArgs.reuseChecksums) if not indexBuilder.run(): success = False except FileDb.IndexValidationError as e: print(e, file=stderr) return 2 return 0 if success else 1
def findCmdMain(cmdArgs): db = FileDb.FileDb() for dbPath in cmdArgs.db: db.addIndexedTree(dbPath) processNew = cmdArgs.new if cmdArgs.moveTarget is not None: action = CopyFindAction(target=cmdArgs.moveTarget, move=True, new=processNew) elif cmdArgs.copyTarget is not None: action = CopyFindAction(target=cmdArgs.copyTarget, move=False, new=processNew) elif processNew: action = printOnlyNewFindAction if cmdArgs.ignoreRenames else printNewFindAction else: action = printFindAction cmd = FindCommand(action=action, db=db, fileTreeIterator=createFileTreeIterator(cmdArgs)) if cmdArgs.excludedList is not None: cmd.addExcludedList(cmdArgs.excludedList) cachedChecksums = cmdArgs.cachedChecksums files = cmdArgs.FILES if len(files) == 0 and cachedChecksums is not None: cmd.processChecksumFile(cachedChecksums, cmdArgs.cachedChecksumsRoot) else: if len(files) == 0: files = [pathlib.Path()] if cachedChecksums is not None: cmd.addCachedChecksums(cachedChecksums, cmdArgs.cachedChecksumsRoot) for filePath in files: cmd.process(filePath)
def createFileTreeIterator(_cmdArgs): iterator = FileDb.FileTreeIterator() iterator.addExcluded('*.sha[12]', 'Thumbs.db', '@*') return iterator