Exemplo n.º 1
0
def invoke_install(path, *, dependency_group=None, **kwargs):
    try:
        call("install",
             "--requirement",
             dependency_group or "requirements.txt",
             cwd=path)
    except subprocess.CalledProcessError as e:
        return e.returncode
    return 0
Exemplo n.º 2
0
def _old_installed_distributions(local: bool):
    list_args = ["list"]
    if local:
        list_args.append("--local")
    result = call(*list_args)

    # result is of the form:
    # <package_name> (<version>)
    #
    # or, if editable
    # <package_name> (<version>, <location>)
    #
    # or, could be a warning line

    ret = {}

    pattern = re.compile(r"(.*) \((.*)\)")

    for line in result.strip().split("\n"):
        match = re.match(pattern, line)

        if match:
            name, paren = match.groups()
            version, location = (paren.split(", ") + [None])[:2]

            ret[name] = Distribution(name, version, location)
        else:
            # This is a warning line or some other output
            pass

    return ret
Exemplo n.º 3
0
def version() -> str:
    result = call("--version")

    # result is of the form:
    # pip <version> from <directory> (python <python version>)

    return result.split(" ")[1]
Exemplo n.º 4
0
def _new_installed_distributions():
    result = call("list", "--format=columns")

    # result is of the form:
    # <package_name>   <version>
    #
    # or, if editable
    # <package_name>   <version>   <location>
    # (with arbitrary spaces)

    ret = {}

    # Remove first two heder lines
    lines = result.strip().split("\n")[2:]

    for line in lines:
        # Split on whitespace to get
        # split = ['<name>', '<version>', '<location>'|None]
        split = line.split() + [None]
        name = split[0]
        version = split[1]
        location = split[2]

        ret[name] = Distribution(name, version, location)

    return ret
Exemplo n.º 5
0
def _old_installed_distributions():
    result = call('list')

    # result is of the form:
    # <package_name> (<version>)
    #
    # or, if editable
    # <package_name> (<version>, <location>)
    #
    # or, could be a warning line

    ret = {}

    pattern = re.compile(r'(.*) \((.*)\)')

    for line in result.strip().split('\n'):
        match = re.match(pattern, line)

        if match:
            name, paren = match.groups()
            version, location = (paren.split(', ') + [None])[:2]

            ret[name] = Distribution(name, version, location)
        else:
            # This is a warning line or some other output
            pass

    return ret
Exemplo n.º 6
0
def version():
    result = call('--version')

    # result is of the form:
    # pip <version> from <directory> (python <python version>)

    return result.split(' ')[1]
Exemplo n.º 7
0
def hash(filename, algorithm='sha256'):
    """
    Hash the given filename. Unavailable in `pip<8.0.0`
    """
    if incompatible:
        raise Incompatible

    if algorithm not in ['sha256', 'sha384', 'sha512']:
        raise InvalidArguments('Algorithm {} not supported'.format(algorithm))

    result = call('hash', '--algorithm', algorithm, filename)

    # result is of the form:
    # <filename>:\n--hash=<algorithm>:<hash>\n
    return result.strip().split(':')[-1]
Exemplo n.º 8
0
def hash(filename: os.PathLike, algorithm: str = "sha256") -> str:
    """
    Hash the given filename. Unavailable in `pip<8.0.0`
    """
    if incompatible:
        raise Incompatible

    if algorithm not in ["sha256", "sha384", "sha512"]:
        raise InvalidArguments("Algorithm {} not supported".format(algorithm))

    result = call("hash", "--algorithm", algorithm, filename)

    # result is of the form:
    # <filename>:\n--hash=<algorithm>:<hash>\n
    return result.strip().split(":")[-1]
Exemplo n.º 9
0
def _new_installed_distributions(local: bool, paths: List[os.PathLike]):
    list_args = ["list", "-v", "--format=json"]
    if local:
        list_args.append("--local")
    for path in paths:
        list_args.extend(["--path", str(path)])
    result = call(*list_args)

    ret = {}

    # The returned JSON is an array of objects, each of which looks like this:
    # { "name": "some-package", "version": "0.0.1", "location": "/path/", ... }
    # The location key was introduced with pip 10.0.0b1, so we don't assume its
    # presence.
    for raw_dist in json.loads(result):
        dist = Distribution(raw_dist["name"], raw_dist["version"],
                            raw_dist.get("location"))
        ret[dist.name] = dist

    return ret
Exemplo n.º 10
0
def installed_distributions():
    result = call('list', '--format=legacy')

    # result is of the form:
    # <package_name> (<version>)
    #
    # or, if editable
    # <package_name> (<version>, <location>)

    ret = {}

    for line in result.strip().split('\n'):
        # Split on the first whitespace to get
        # split = ['<name>', '(<version>, <location>)']
        split = line.split(' ', 1)
        name = split[0]
        # Strip the first/last parens, then split on the ', ' which may be
        # between <version> and <location> (if not pad with None)
        version, location = (split[1][1:-1].split(', ') + [None])[:2]
        ret[name] = Distribution(name, version, location)

    return ret