def test_that_we_can_generate_an_empty_requirement_set_from_freshly_constructed_collector( current_platform, requirement_parser, logger: Logger, project_dir: str): collector = RequirementsCollector(current_platform, requirement_parser, logger, project_dir, DependencyGraph()) requirements = collector.requirements() assert len(requirements) == 0
def test_that_we_can_add_a_requirements_file_path( collector: RequirementsCollector, tmpdir: pathlib.Path) -> None: requirements_txt = tmpdir / "requirements.txt" requirements_lines = ["pytest", "flake8"] with open(requirements_txt, "w") as f: for requirement in requirements_lines: print(requirement, file=f) collector.add_file(str(requirements_txt)) assert "pytest" in collector.requirements() assert "flake8" in collector.requirements()
def test_that_requirements_with_relative_paths_are_absolute_paths_after_adding( collector: RequirementsCollector, ) -> None: collector.add_line("./path/to/egg#egg=testegg") requirement = collector.requirements().get("testegg") assert isinstance(requirement, PathRequirement) assert os.path.isabs(requirement.path())
def test_that_we_can_add_command_line_requirements_by_name( collector: RequirementsCollector, ) -> None: collector.add_line("pytest") requirements = collector.requirements() assert "pytest" in requirements
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("")