Beispiel #1
0
def installShortcut(name : str, path : str, workingDir : str, icon : str, desciption : str):
    if not CraftCore.compiler.isWindows:
        return True
    from shells import Powershell
    pwsh = Powershell()
    shortcutPath = Path(os.environ["APPDATA"]) / f"Microsoft/Windows/Start Menu/Programs/Craft/{name}.lnk"
    shortcutPath.parent.mkdir(parents=True, exist_ok=True)

    return pwsh.execute([os.path.join(CraftCore.standardDirs.craftBin(), "install-lnk.ps1"),
                         "-Path", pwsh.quote(path),
                  "-WorkingDirectory", pwsh.quote(OsUtils.toNativePath(workingDir)),
                  "-Name", pwsh.quote(shortcutPath),
                  "-Icon", pwsh.quote(icon),
                  "-Description", pwsh.quote(desciption)])
Beispiel #2
0
def getFile(url, destdir, filename='', quiet=None) -> bool:
    """download file from 'url' into 'destdir'"""
    if quiet is None:
        quiet = CraftCore.settings.getboolean("ContinuousIntegration",
                                              "Enabled", False)
    CraftCore.log.debug("getFile called. url: %s" % url)
    if url == "":
        CraftCore.log.error("fetch: no url given")
        return False

    pUrl = urllib.parse.urlparse(url)
    if not filename:
        filename = os.path.basename(pUrl.path)

    utils.createDir(destdir)

    if pUrl.scheme == "s3":
        return s3File(url, destdir, filename)
    elif pUrl.scheme == "minio":
        return minioGet(pUrl.netloc + pUrl.path, destdir, filename)

    absFilename = Path(destdir) / filename
    # try the other methods as fallback if we are bootstrapping
    bootStrapping = not (CraftCore.standardDirs.etcDir() /
                         "cacert.pem").exists()
    if not CraftCore.settings.getboolean("General", "NoWget"):
        if CraftCore.cache.findApplication("wget"):
            if wgetFile(url, destdir, filename, quiet):
                return True
            if not bootStrapping:
                return False

    if CraftCore.cache.findApplication("curl"):
        if curlFile(url, destdir, filename, quiet):
            return True
        if not bootStrapping:
            return False

    if bootStrapping and absFilename.exists():
        os.remove(absFilename)

    if absFilename.exists():
        return True

    CraftCore.log.info(f"Downloading: {url} to {absFilename}")
    with utils.ProgressBar() as progress:

        def dlProgress(count, blockSize, totalSize):
            if totalSize != -1:
                progress.print(int(count * blockSize * 100 / totalSize))
            else:
                sys.stdout.write(
                    ("\r%s bytes downloaded" % (count * blockSize)))
                sys.stdout.flush()

        try:
            urllib.request.urlretrieve(
                url,
                filename=absFilename,
                reporthook=dlProgress
                if CraftCore.debug.verbose() >= 0 else None)
        except Exception as e:
            CraftCore.log.warning(e)
            powershell = Powershell()
            if powershell.pwsh:
                return powershell.execute([
                    f"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (new-object net.webclient).DownloadFile(\"{url}\", \"{absFilename}\")"
                ])
            return False
    return True