示例#1
0
文件: stage2.py 项目: garbas/pypi2nix
def process_wheel(wheel_cache_dir, wheel, sources, verbose, index=INDEX_URL, chunk_size=2048):
    """
    """

    if wheel["name"] in sources:
        release = dict()
        release["url"] = sources[wheel["name"]]
        release["hash_type"] = "sha256"

        if release["url"].startswith("http://") or release["url"].startswith("https://"):

            release["fetch_type"] = "fetchurl"

            r = requests.get(release["url"], stream=True, timeout=None)
            r.raise_for_status()  # TODO: handle this nicer

            with tempfile.TemporaryFile() as fd:
                for chunk in r.iter_content(chunk_size):
                    fd.write(chunk)
                    fd.seek(0)
                    hash = hashlib.sha256(fd.read())

            release["hash_value"] = hash.hexdigest()
        elif release["url"].startswith("git"):
            release["fetch_type"] = "fetchgit"
            command = "nix-prefetch-git {url}".format(**release)
            return_code, output = cmd(command, verbose != 0)
            if return_code != 0:
                raise click.ClickException(
                    "URL {url} for package {name} is not valid.".format(url=release["url"], name=wheel["name"])
                )
            for output_line in output.split("\n"):
                output_line = output_line.strip()
                if output_line.startswith("hash is "):
                    release["hash_value"] = output_line[len("hash is ") :].strip()
                elif output_line.startswith("git revision is "):
                    release["rev"] = output_line[len("git revision is ") :].strip()

            if release.get("hash_value", None) is None:
                raise click.ClickException("Could not determine the hash from ouput:\n{output}".format(output=output))
            if release.get("rev", None) is None:
                raise click.ClickException(
                    "Could not determine the revision from ouput:\n{output}".format(output=output)
                )

    else:
        url = "{}/{}/json".format(index, wheel["name"])
        r = requests.get(url, timeout=None)
        r.raise_for_status()  # TODO: handle this nicer
        wheel_data = r.json()

        if not wheel_data.get("releases"):
            raise click.ClickException("Unable to find releases for packge {name}".format(**wheel))

        release = find_release(wheel_cache_dir, wheel, wheel_data)

    wheel.update(release)

    return wheel
示例#2
0
def test_can_send_input(logger: Logger):
    exit_code, output = cmd(["cat", "-"], logger=logger, input="test_input")
    assert output == "test_input"
示例#3
0
def process_wheel(wheel_cache_dir,
                  wheel,
                  sources,
                  verbose,
                  index=INDEX_URL,
                  chunk_size=2048):
    """
    """

    if wheel['name'] in sources:
        release = dict()
        release['url'] = sources[wheel['name']]['url']
        release['hash_type'] = 'sha256'

        repo_type = sources[wheel['name']]['type']
        if repo_type == 'url':

            release['fetch_type'] = 'fetchurl'

            r = requests.get(release['url'], stream=True, timeout=None)
            r.raise_for_status()  # TODO: handle this nicer

            with tempfile.TemporaryFile() as fd:
                for chunk in r.iter_content(chunk_size):
                    fd.write(chunk)
                    fd.seek(0)
                    hash = hashlib.sha256(fd.read())

            release['hash_value'] = hash.hexdigest()

        elif repo_type == 'git':
            revision = ''
            if release['url'].startswith('git+'):
                release['url'] = release['url'][4:]
            if '@' in release['url']:
                release['url'], revision = release['url'].split('@')

            release['fetch_type'] = 'fetchgit'
            command = 'nix-prefetch-git {url} {revision}'.format(
                url=release['url'],
                revision=revision,
            )
            return_code, output = cmd(command, verbose != 0)
            if return_code != 0:
                raise click.ClickException(
                    "URL {url} for package {name} is not valid.".format(
                        url=release['url'], name=wheel['name']))
            for output_line in output.split('\n'):
                output_line = output_line.strip()
                if output_line.startswith('hash is '):
                    release['hash_value'] = output_line[len('hash is '
                                                            ):].strip()
                elif output_line.startswith('git revision is '):
                    release['rev'] = output_line[len('git revision is '
                                                     ):].strip()

            if release.get('hash_value', None) is None:
                raise click.ClickException(
                    'Could not determine the hash from ouput:\n{output}'.
                    format(output=output))
            if release.get('rev', None) is None:
                raise click.ClickException(
                    'Could not determine the revision from ouput:\n{output}'.
                    format(output=output))

        elif repo_type == 'hg':
            revision = ''
            if release['url'].startswith('hg+'):
                release['url'] = release['url'][3:]
            if '@' in release['url']:
                release['url'], revision = release['url'].split('@')

            release['fetch_type'] = 'fetchhg'
            command = 'nix-prefetch-hg {url} {revision}'.format(
                url=release['url'],
                revision=revision,
            )
            return_code, output = cmd(command, verbose != 0)
            if return_code != 0:
                raise click.ClickException(
                    "URL {url} for package {name} is not valid.".format(
                        url=release['url'], name=wheel['name']))
            HASH_PREFIX = 'hash is '
            REV_PREFIX = 'hg revision is '
            for output_line in output.split('\n'):
                print(output_line)
                output_line = output_line.strip()
                if output_line.startswith(HASH_PREFIX):
                    release['hash_value'] = output_line[len(HASH_PREFIX
                                                            ):].strip()
                elif output_line.startswith(REV_PREFIX):
                    release['rev'] = output_line[len(REV_PREFIX):].strip()

            if release.get('hash_value', None) is None:
                raise click.ClickException(
                    'Could not determine the hash from ouput:\n{output}'.
                    format(output=output))
            if release.get('rev', None) is None:
                raise click.ClickException(
                    'Could not determine the revision from ouput:\n{output}'.
                    format(output=output))

        elif repo_type == 'path':
            release['fetch_type'] = 'path'

        else:
            raise click.ClickException(
                'Source type `{}` not implemented'.format(repo_type))

    else:
        url = "{}/{}/json".format(index, wheel['name'])
        r = requests.get(url, timeout=None)
        r.raise_for_status()  # TODO: handle this nicer
        wheel_data = r.json()

        if not wheel_data.get('releases'):
            raise click.ClickException(
                "Unable to find releases for packge {name}".format(**wheel))

        release = find_release(wheel_cache_dir, wheel, wheel_data)

    wheel.update(release)

    return wheel
示例#4
0
def test_consistent_output(logger: Logger):
    exit_code, output = cmd(["seq", "5"], logger=logger)
    assert output == "1\n2\n3\n4\n5\n"
示例#5
0
 def format_file(self, path: Path) -> None:
     if self._is_nixfmt_installed():
         cmd(["nixfmt", str(path)], logger=self._logger)
     else:
         self._logger.warning(
             f"Could not format {path}, nixfmt is not installed")