Ejemplo n.º 1
0
def test_that_target_platform_can_be_constructed_from_python_version(
        platform_generator: PlatformGenerator, nix, python_3_environment_nix):
    platform = platform_generator.from_python_version(PythonVersion.python3)
    assert isinstance(platform, TargetPlatform)

    python_3_version = nix.shell(
        command=
        'python -c "from platform import python_version; print(python_version()[:3])"',
        derivation_path=python_3_environment_nix,
    ).splitlines()[0]
    assert platform.python_version == python_3_version
Ejemplo n.º 2
0
def test_that_generated_platform_environment_dictionary_respects_python_version(
        platform_generator: PlatformGenerator, python_3_6_environment_nix,
        nix):
    platform = platform_generator.from_python_version(PythonVersion.python36)
    assert isinstance(platform, TargetPlatform)
    output_string = nix.shell(
        command=" ".join([
            'python -c "from setuptools._vendor.packaging.markers import default_environment;',
            "from json import dumps;",
            'print(dumps(default_environment()))"',
        ]),
        derivation_path=python_3_6_environment_nix,
    )
    output_json = json.loads(output_string)
    assert platform.environment_dictionary() == output_json
Ejemplo n.º 3
0
def current_platform(platform_generator: PlatformGenerator) -> TargetPlatform:
    platform = platform_generator.current_platform()
    if platform is None:
        raise Exception("Could not recognize current platform")
    else:
        return platform
Ejemplo n.º 4
0
def platform_generator(nix: Nix, logger: Logger) -> PlatformGenerator:
    return PlatformGenerator(nix, logger)
Ejemplo n.º 5
0
 def target_platform(self) -> TargetPlatform:
     platform_generator = PlatformGenerator(nix=self.nix(),
                                            logger=self.logger())
     target_platform = platform_generator.from_python_version(
         self.configuration.python_version)
     return target_platform
Ejemplo n.º 6
0
def main(
    version: str,
    verbose: int,
    nix_shell: str,
    nix_path: List[str],
    basename: str,
    cache_dir: str,
    extra_build_inputs: List[str],
    extra_env: str,
    enable_tests: bool,
    python_version: str,
    requirements: List[str],
    editable: List[str],
    setup_requires: List[str],
    overrides: List[AnyOverrides],
    default_overrides: bool,
    wheels_cache: List[str],
) -> None:
    """SPECIFICATION should be requirements.txt (output of pip freeze).
    """

    logger = Logger(output=sys.stdout)
    nix_executable_directory: Optional[str] = (os.path.abspath(
        os.path.dirname(nix_shell)) if os.path.exists(nix_shell) else None)

    nix = Nix(
        nix_path=nix_path,
        executable_directory=nix_executable_directory,
        verbose=verbose != 0,
    )
    platform_generator = PlatformGenerator(nix=nix)

    if default_overrides:
        overrides += tuple([
            pypi2nix.overrides.OverridesGithub(owner="garbas",
                                               repo="nixpkgs-python",
                                               path="overrides.nix")
        ])

    with open(os.path.join(os.path.dirname(__file__), "VERSION")) as f:
        pypi2nix_version = f.read()

    if version:
        click.echo(pypi2nix_version)
        return

    python_version_argument = python_version
    python_versions = pypi2nix.utils.PYTHON_VERSIONS.keys()
    if not python_version:
        raise click.exceptions.UsageError(
            'Missing option "-V" / "--python-version".  Choose from ' +
            (", ".join(python_versions)))
    python_version = pypi2nix.utils.PYTHON_VERSIONS[python_version]
    target_platform = platform_generator.from_python_version(
        python_version_argument)

    requirement_collector = RequirementsCollector(target_platform)
    setup_requirement_collector = RequirementsCollector(target_platform)

    extra_build_inputs = pypi2nix.utils.args_as_list(extra_build_inputs)
    setup_requires = pypi2nix.utils.args_as_list(setup_requires)

    for item in editable:
        requirement_collector.add_line(item)
    for build_input in setup_requires:
        setup_requirement_collector.add_line(build_input)

    # temporary pypi2nix folder and make sure it exists
    tmp_dir = os.path.join(tempfile.gettempdir(), "pypi2nix")
    if not os.path.exists(tmp_dir):
        os.makedirs(tmp_dir)

    current_dir = os.getcwd()
    requirements_name = os.path.join(current_dir, basename)

    if not cache_dir:
        cache_dir = os.path.join(tmp_dir, "cache")

    download_cache_dir = os.path.join(cache_dir, "download")
    wheel_cache_dir = os.path.join(cache_dir, "wheel")

    if not os.path.exists(download_cache_dir):
        os.makedirs(download_cache_dir)

    if not os.path.exists(wheel_cache_dir):
        os.makedirs(wheel_cache_dir)

    assert requirements is not None

    project_hash = md5_sum_of_files_with_file_names(requirements)

    project_dir = os.path.join(tmp_dir, project_hash)
    if os.path.exists(project_dir):
        shutil.rmtree(project_dir)
    os.makedirs(project_dir)

    for requirement_file_path in requirements:
        requirement_collector.add_file(requirement_file_path)

    requirement_set = requirement_collector.requirements()
    setup_requirements = setup_requirement_collector.requirements()

    sources = Sources()
    sources.update(requirement_set.sources())
    sources.update(setup_requirements.sources())

    click.echo("pypi2nix v{} running ...".format(pypi2nix_version))
    click.echo("")

    click.echo("Stage1: Downloading wheels and creating wheelhouse ...")

    pip = Pip(
        nix=nix,
        project_directory=project_dir,
        extra_env=extra_env,
        extra_build_inputs=extra_build_inputs,
        verbose=verbose,
        wheels_cache=wheels_cache,
        target_platform=target_platform,
    )
    wheel_builder = pypi2nix.stage1.WheelBuilder(pip=pip,
                                                 project_directory=project_dir,
                                                 logger=logger)
    wheels = wheel_builder.build(requirements=requirement_set,
                                 setup_requirements=setup_requirements)
    requirements_frozen = wheel_builder.get_frozen_requirements()
    default_environment = pip.default_environment()
    additional_dependency_graph = wheel_builder.additional_build_dependencies

    click.echo("Stage2: Extracting metadata from pypi.python.org ...")

    stage2 = pypi2nix.stage2.Stage2(sources=sources, verbose=verbose)

    packages_metadata = stage2.main(
        wheel_paths=wheels,
        default_environment=default_environment,
        wheel_cache_dir=wheel_cache_dir,
        additional_dependencies=additional_dependency_graph,
    )
    click.echo("Stage3: Generating Nix expressions ...")

    pypi2nix.stage3.main(
        packages_metadata=packages_metadata,
        sources=sources,
        requirements_name=requirements_name,
        requirements_frozen=requirements_frozen,
        extra_build_inputs=extra_build_inputs,
        enable_tests=enable_tests,
        python_version=python_version,
        current_dir=current_dir,
        common_overrides=overrides,
    )

    click.echo("")
    click.echo("Nix expressions generated successfully.")
    click.echo("")
    click.echo("To start development run:")
    click.echo("    nix-shell requirements.nix -A interpreter")
    click.echo("")
    click.echo("More information you can find at")
    click.echo("    https://github.com/garbas/pypi2nix")
    click.echo("")
Ejemplo n.º 7
0
def platform_generator(nix):
    return PlatformGenerator(nix)
Ejemplo n.º 8
0
def test_available_python_versions_exist_in_nixpkgs(
        python_version: PythonVersion, platform_generator: PlatformGenerator):
    target_platform = platform_generator.from_python_version(python_version)
    assert target_platform is not None