Beispiel #1
0
def download_licenses(ctx,
                      vendor_dir=None,
                      requirements_file='vendor.txt',
                      package=None,
                      only=False,
                      patched=False):
    log('Downloading licenses')
    if not vendor_dir:
        if patched:
            vendor_dir = _get_patched_dir(ctx)
            requirements_file = 'patched.txt'
        else:
            vendor_dir = _get_vendor_dir(ctx)
    requirements_file = vendor_dir / requirements_file
    requirements = packages_missing_licenses(ctx,
                                             vendor_dir,
                                             requirements_file,
                                             package=package)

    with NamedTemporaryFile(prefix="pipenv",
                            suffix="vendor-reqs",
                            delete=False,
                            mode="w") as fh:
        fh.write("\n".join(requirements))
        new_requirements_file = fh.name
    new_requirements_file = Path(new_requirements_file)
    log(requirements)
    tmp_dir = vendor_dir / '__tmp__'
    # TODO: Fix this whenever it gets sorted out (see https://github.com/pypa/pip/issues/5739)
    cmd = "pip download --no-binary :all: --only-binary requests_download --no-deps"
    enum_cmd = "pip download --no-deps"
    ctx.run('pip install flit')  # needed for the next step
    for req in requirements_file.read_text().splitlines():
        if req.startswith("enum34"):
            exe_cmd = "{0} -d {1} {2}".format(enum_cmd, tmp_dir.as_posix(),
                                              req)
        else:
            exe_cmd = "{0} --no-build-isolation -d {1} {2}".format(
                cmd, tmp_dir.as_posix(), req)
        try:
            ctx.run(exe_cmd)
        except invoke.exceptions.UnexpectedExit as e:
            if "Disabling PEP 517 processing is invalid" not in e.result.stderr:
                log("WARNING: Failed to download license for {0}".format(req))
                continue
            parse_target = (
                "Disabling PEP 517 processing is invalid: project specifies a build "
                "backend of {backend} in pyproject.toml")
            target = parse.parse(parse_target, e.result.stderr.strip())
            backend = target.named.get("backend")
            if backend is not None:
                if "." in backend:
                    backend, _, _ = backend.partition(".")
                ctx.run("pip install {0}".format(backend))
            ctx.run("{0} --no-build-isolation -d {1} {2}".format(
                cmd, tmp_dir.as_posix(), req))
    for sdist in tmp_dir.iterdir():
        extract_license(vendor_dir, sdist)
    new_requirements_file.unlink()
    drop_dir(tmp_dir)
Beispiel #2
0
def proper_case(package_name):
    """Properly case project name from pypi.org."""
    # Hit the simple API.
    r = _get_requests_session().get(
        f"https://pypi.org/pypi/{package_name}/json", timeout=0.3, stream=True)
    if not r.ok:
        raise OSError(
            f"Unable to find package {package_name} in PyPI repository.")

    r = parse.parse("https://pypi.org/pypi/{name}/json", r.url)
    good_name = r["name"]
    return good_name
Beispiel #3
0
def download_licenses(
    ctx,
    vendor_dir=None,
    requirements_file="vendor.txt",
    package=None,
    only=False,
    patched=False,
):
    log("Downloading licenses")
    if not vendor_dir:
        if patched:
            vendor_dir = _get_patched_dir(ctx)
            requirements_file = "patched.txt"
        else:
            vendor_dir = _get_vendor_dir(ctx)
    requirements_file = vendor_dir / requirements_file
    requirements = packages_missing_licenses(ctx,
                                             vendor_dir,
                                             requirements_file,
                                             package=package)
    log(requirements)
    tmp_dir = vendor_dir / "__tmp__"
    # TODO: Fix this whenever it gets sorted out (see https://github.com/pypa/pip/issues/5739)
    cmd = "pip download --no-binary :all: --only-binary requests_download --no-deps"
    enum_cmd = "pip download --no-deps"
    ctx.run("pip install flit")  # needed for the next step
    for req in requirements:
        if req.startswith("enum34"):
            exe_cmd = f"{enum_cmd} -d {tmp_dir.as_posix()} {req}"
        else:
            exe_cmd = "{} --no-build-isolation -d {} {}".format(
                cmd, tmp_dir.as_posix(), req)
        try:
            ctx.run(exe_cmd)
        except invoke.exceptions.UnexpectedExit as e:
            if "Disabling PEP 517 processing is invalid" not in e.result.stderr:
                log(f"WARNING: Failed to download license for {req}")
                continue
            parse_target = (
                "Disabling PEP 517 processing is invalid: project specifies a build "
                "backend of {backend} in pyproject.toml")
            target = parse.parse(parse_target, e.result.stderr.strip())
            backend = target.named.get("backend")
            if backend is not None:
                if "." in backend:
                    backend, _, _ = backend.partition(".")
                ctx.run(f"pip install {backend}")
            ctx.run(
                f"{cmd} --no-build-isolation -d {tmp_dir.as_posix()} {req}")
    for sdist in tmp_dir.iterdir():
        extract_license(vendor_dir, sdist)
    drop_dir(tmp_dir)