def unmergeFileList(rootdir, fileList): """ delete files in the fileList if has matches """ for filename, filehash in fileList: fullPath = os.path.join(rootdir, os.path.normcase(filename)) if os.path.isfile(fullPath) or os.path.islink(fullPath): if filehash: algorithm = CraftHash.HashAlgorithm.getAlgorithmFromPrefix( filehash) currentHash = algorithm.stringPrefix( ) + CraftHash.digestFile(fullPath, algorithm) if not filehash or currentHash == filehash: OsUtils.rm(fullPath, True) else: CraftCore.log.warning( f"We can't remove {fullPath} as its hash has changed," f" that usually implies that the file was modified or replaced" ) elif not os.path.isdir(fullPath) and os.path.lexists(fullPath): CraftCore.log.debug(f"Remove a dead symlink {fullPath}") OsUtils.rm(fullPath, True) elif not os.path.isdir(fullPath): CraftCore.log.warning("file %s does not exist" % fullPath) containingDir = os.path.dirname(fullPath) if os.path.exists(containingDir) and not os.listdir(containingDir): CraftCore.log.debug(f"Delete empty dir {containingDir}") utils.rmtree(containingDir)
def _compress(self, archiveName, sourceDir, destDir): utils.deleteFile(archiveName) app = utils.utilsCache.findApplication("7za") kw = {} progressFlags = "" if utils.utilsCache.checkCommandOutputFor(app, "-bs"): progressFlags = " -bso2 -bsp1" kw["stderr"] = subprocess.PIPE archive = os.path.join(destDir, archiveName) if os.path.isfile(archive): utils.deleteFile(archive) cmd = f"\"{app}\" a {progressFlags} -r \"{archive}\" \"{sourceDir}/*\"" if not utils.system(cmd, displayProgress=True, **kw): craftDebug.log.critical(f"while packaging. cmd: {cmd}") if not craftSettings.getboolean("Packager", "CreateCache"): CraftHash.createDigestFiles(archive) else: cacheFilePath = os.path.join(self.cacheLocation(), "manifest.json") if os.path.exists(cacheFilePath): with open(cacheFilePath, "rt+") as cacheFile: cache = json.load(cacheFile) else: cache = {} if not str(self) in cache: cache[str(self)] = {} cache[str(self)][archiveName] = { "checksum": CraftHash.digestFile(archive, CraftHash.HashAlgorithm.SHA256) } with open(cacheFilePath, "wt+") as cacheFile: json.dump(cache, cacheFile, sort_keys=True, indent=2)
def unmergeFileList(rootdir, fileList): """ delete files in the fileList if has matches """ for filename, filehash in fileList: fullPath = os.path.join(rootdir, os.path.normcase(filename)) if os.path.isfile(fullPath): algorithm = CraftHash.HashAlgorithm.getAlgorithmFromPrefix(filehash) if not algorithm: currentHash = CraftHash.digestFile(fullPath, CraftHash.HashAlgorithm.MD5) else: currentHash = algorithm.stringPrefix() + CraftHash.digestFile(fullPath, algorithm) if currentHash == filehash or filehash == "": OsUtils.rm(fullPath, True) else: craftDebug.log.warning( f"We can't remove {fullPath} as its hash has changed," f" that usually implies that the fiel was modified or replaced") elif not os.path.isdir(fullPath): craftDebug.log.warning("file %s does not exist" % fullPath)
def getFileListFromDirectory(imagedir, filePaths): """ create a file list containing hashes """ ret = [] algorithm = CraftHash.HashAlgorithm.SHA256 for filePath in filePaths: relativeFilePath = os.path.relpath(filePath, imagedir) digest = algorithm.stringPrefix() + CraftHash.digestFile(filePath, algorithm) ret.append((relativeFilePath, digest)) return ret
def _generateManifest(self, destDir, archiveName, manifestLocation=None, manifestUrls=None): if not manifestLocation: manifestLocation = destDir manifestLocation = os.path.join(manifestLocation, "manifest.json") archiveFile = os.path.join(destDir, archiveName) name = archiveName if not os.path.isabs(archiveName) else os.path.relpath(archiveName, destDir) manifest = CraftManifest.load(manifestLocation, urls=manifestUrls) entry = manifest.get(str(self)) entry.addFile(name, CraftHash.digestFile(archiveFile, CraftHash.HashAlgorithm.SHA256), version=self.version) manifest.dump(manifestLocation)
def generateSrcManifest(self) -> bool: archiveNames = self.localFileNames() if self.subinfo.hasTargetDigestUrls(): url, alg = self.subinfo.targetDigestUrl() archiveNames.append(self.subinfo.archiveName()[0] + CraftHash.HashAlgorithm.fileEndings().get(alg)) manifestLocation = os.path.join(self.__archiveDir, "manifest.json") manifest = CraftManifest.load(manifestLocation, urls=CraftCore.settings.getList("Packager", "ArchiveRepositoryUrl")) entry = manifest.get(str(self), compiler="all") entry.files.clear() for archiveName in archiveNames: name = (Path(self.package.path) / archiveName).as_posix() archiveFile = self.__downloadDir / archiveName if not archiveFile.is_file(): continue digests = CraftHash.digestFile(archiveFile, CraftHash.HashAlgorithm.SHA256) entry.addFile(name, digests, version=self.version) manifest.dump(manifestLocation) return True