def build_wheel(target_directory: str, requirement: str) -> str: logger = StreamLogger(sys.stdout) requirement_parser = RequirementParser(logger=logger) package_directory = os.path.join(ROOT, "unittests", "data") escaped_requirement = shlex.quote(requirement) target_directory = os.path.abspath(target_directory) with tempfile.TemporaryDirectory() as build_directory: os.chdir(build_directory) nix = Nix(logger=logger) nix.shell( command=f"pip wheel {escaped_requirement} --find-links {package_directory} --no-deps", derivation_path=DERIVATION_PATH, nix_arguments=dict(), ) try: parsed_requirement = requirement_parser.parse(requirement) except ParsingFailed: for path in os.listdir("."): if path.endswith(".whl"): wheel_path = path break else: raise Exception("Build process did not produce .whl file") else: for path in os.listdir("."): if path.endswith(".whl") and parsed_requirement.name() in path: wheel_path = path break else: raise Exception("Build process did not produce .whl file") target_file_name = os.path.basename(wheel_path) target_path = os.path.join(target_directory, target_file_name) shutil.move(wheel_path, target_path) return target_file_name
def setUp(self) -> None: self.logger = StreamLogger(output=sys.stdout) self.nix = Nix(nix_path=[NIX_PATH], logger=self.logger) self.assertNotEqual(self.name_of_testcase, "undefined")
def nix(logger): return Nix(logger)
def nix(self) -> Nix: return Nix( nix_path=self.configuration.nix_path, executable_directory=self.configuration.nix_executable_directory, logger=self.logger(), )
def nix(logger: Logger) -> Nix: return Nix(logger)
def test_evaluate_nix_expression_raises_exception_when_executable_not_found( logger): nix = Nix(executable_directory="/does-not-exist", logger=logger) with pytest.raises(ExecutableNotFound): nix.evaluate_expression("true")
def nix_instance(tmpdir, logger): nix_path_addition = tmpdir.mkdir("testpath_exists") yield Nix(nix_path=["test_variable={}".format(str(nix_path_addition))], logger=logger)
def setUp(self) -> None: self.logger = StreamLogger(output=sys.stdout) self.nix = Nix(logger=self.logger) self.assertNotEqual(self.name_of_testcase, "undefined") self.requirement_parser = RequirementParser(self.logger)
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("")
def nix(): return Nix(verbose=True)
def expression_evaluater(logger): nix_instance = Nix(logger=logger) return lambda expression: nix_instance.evaluate_expression( "let pkgs = import <nixpkgs> {}; in " + expression )