Exemple #1
0
    def fetchBinary(self) -> bool:
        if self.subinfo.options.package.disableBinaryCache:
            return False

        for url in [self.cacheLocation()] + self.cacheRepositoryUrls():
            CraftCore.log.debug(f"Trying to restore {self} from cache: {url}.")
            if url == self.cacheLocation():
                fileUrl = f"{url}/manifest.json"
                if os.path.exists(fileUrl):
                    with open(fileUrl, "rt", encoding="UTF-8") as f:
                        manifest = CraftManifest.fromJson(json.load(f))
                else:
                  continue
            else:
                manifest = CraftManifest.fromJson(CraftCore.cache.cacheJsonFromUrl(f"{url}/manifest.json"))
            fileEntry = manifest.get(str(self)).files
            files = []
            for f in fileEntry:
                if f.version == self.version:
                    files.append(f)
            latest = None
            if not files:
                CraftCore.log.debug(f"Could not find {self}={self.version} in {url}")
                continue
            latest = files[0]

            if url != self.cacheLocation():
                downloadFolder = self.cacheLocation(os.path.join(CraftCore.standardDirs.downloadDir(), "cache"))
            else:
                downloadFolder = self.cacheLocation()
            localArchiveAbsPath = OsUtils.toNativePath(os.path.join(downloadFolder, latest.fileName))
            localArchivePath, localArchiveName = os.path.split(localArchiveAbsPath)


            if url != self.cacheLocation():
                if not os.path.exists(localArchiveAbsPath):
                    os.makedirs(localArchivePath, exist_ok=True)
                    fUrl = f"{url}/{latest.fileName}"
                    if not utils.getFile(fUrl, localArchivePath, localArchiveName):
                        CraftCore.log.warning(f"Failed to fetch {fUrl}")
                        return False
            elif not os.path.isfile(localArchiveAbsPath):
                continue

            if not CraftHash.checkFilesDigests(localArchivePath, [localArchiveName],
                                               digests=latest.checksum,
                                               digestAlgorithm=CraftHash.HashAlgorithm.SHA256):
                CraftCore.log.warning(f"Hash did not match, {localArchiveName} might be corrupted")
                return False
            self.subinfo.buildPrefix = latest.buildPrefix
            if not (self.cleanImage()
                    and utils.unpackFile(localArchivePath, localArchiveName, self.imageDir())
                    and self.internalPostInstall()
                    and self.postInstall()
                    and self.qmerge()
                    and self.internalPostQmerge()
                    and self.postQmerge()):
                return False
            return True
        return False
Exemple #2
0
 def _getFileInfoFromArchiveCache(self) -> []:
     out = []
     for url in CraftCore.settings.getList("Packager", "ArchiveRepositoryUrl"):
         manifest = CraftManifest.fromJson(CraftCore.cache.cacheJsonFromUrl(utils.urljoin(url, "manifest.json")))
         files = manifest.get(str(self), compiler="all").files
         if files:
             out.append((url, files))
     return out
Exemple #3
0
    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
Exemple #4
0
    def fetchBinary(self, downloadRetriesLeft=3) -> bool:
        if self.subinfo.options.package.disableBinaryCache:
            return False
        for url in [self.cacheLocation()] + self.cacheRepositoryUrls():
            CraftCore.log.debug(f"Trying to restore {self} from cache: {url}.")
            if url == self.cacheLocation():
                fileUrl = f"{url}/manifest.json"
                if os.path.exists(fileUrl):
                    with open(fileUrl, "rt", encoding="UTF-8") as f:
                        manifest = CraftManifest.fromJson(json.load(f))
                else:
                    continue
            else:
                manifest = CraftManifest.fromJson(
                    CraftCore.cache.cacheJsonFromUrl(f"{url}/manifest.json"))
            fileEntry = manifest.get(str(self)).files
            files = []
            for f in fileEntry:
                if f.version == self.version:
                    files.append(f)
            if not files:
                CraftCore.log.info(
                    f"Could not find {self}={self.version} in {url}")
                continue
            latest = files[0]

            if not self.subinfo.options.dynamic.compatible(
                    latest.config, latest.configHash):
                CraftCore.log.info(
                    "Failed to restore package, configuration missmatch")
                CraftCore.debug.debug_line()
                CraftCore.log.info("Cached config: {}".format(", ".join(
                    f"{k}={v}" for k, v in latest.config.items())))
                CraftCore.log.info(
                    f"Local config:  {self.subinfo.options.dynamic}")
                CraftCore.debug.debug_line()
                # try next cache
                continue

            # if we are creating the cache, a rebuild on a failed fetch would be suboptimal
            createingCache = CraftCore.settings.getboolean(
                "Packager", "CreateCache", False)

            if url != self.cacheLocation():
                downloadFolder = self.cacheLocation(
                    os.path.join(CraftCore.standardDirs.downloadDir(),
                                 "cache"))
            else:
                downloadFolder = self.cacheLocation()
            localArchiveAbsPath = OsUtils.toNativePath(
                os.path.join(downloadFolder, latest.fileName))
            localArchivePath, localArchiveName = os.path.split(
                localArchiveAbsPath)

            if url != self.cacheLocation():
                if not os.path.exists(localArchiveAbsPath):
                    os.makedirs(localArchivePath, exist_ok=True)
                    fileName = latest.fileName
                    if CraftCore.compiler.isWindows:
                        fileName = fileName.replace("\\", "/")
                    fUrl = f"{url}/{fileName}"
                    # try it up to 3 times
                    retries = 3
                    while True:
                        if GetFiles.getFile(fUrl, localArchivePath,
                                            localArchiveName):
                            break
                        msg = f"Failed to fetch {fUrl}"
                        retries -= 1
                        if not retries:
                            if createingCache:
                                raise BlueprintException(msg, self.package)
                            else:
                                CraftCore.log.warning(msg)
                            return False
            elif not os.path.isfile(localArchiveAbsPath):
                continue

            if not CraftHash.checkFilesDigests(
                    localArchivePath, [localArchiveName],
                    digests=latest.checksum,
                    digestAlgorithm=CraftHash.HashAlgorithm.SHA256):
                msg = f"Hash did not match, {localArchiveName} might be corrupted"
                CraftCore.log.warning(msg)
                if downloadRetriesLeft and CraftChoicePrompt.promptForChoice(
                        "Do you want to delete the files and redownload them?",
                    [("Yes", True), ("No", False)],
                        default="Yes"):
                    return utils.deleteFile(
                        localArchiveAbsPath) and self.fetchBinary(
                            downloadRetriesLeft=downloadRetriesLeft - 1)
                if createingCache:
                    raise BlueprintException(msg, self.package)
                return False
            self.subinfo.buildPrefix = latest.buildPrefix
            self.subinfo.isCachedBuild = True
            if not (self.cleanImage() and utils.unpackFile(
                    localArchivePath, localArchiveName, self.imageDir())
                    and self.internalPostInstall() and self.postInstall()
                    and self.qmerge() and self.internalPostQmerge()
                    and self.postQmerge()):
                return False
            return True
        return False