class PathSource: def __init__(self, path: str) -> None: self.path = Path(path) @property def _normalized_path(self) -> Path: return self.path.resolve() def sha256(self) -> str: return self._normalized_path.sha256_sum() def nix_expression(self) -> str: return " ".join([ "builtins.fetchurl {", f'url = "file://{self._normalized_path}";', f'sha256 = "{self.sha256()}";', "}", ])
def build_wheel(target_directory: Path, requirement: str) -> str: logger = StreamLogger(sys.stdout) requirement_parser = RequirementParser(logger=logger) package_directory: str = str(ROOT / "unittests" / "data") escaped_requirement = shlex.quote(requirement) target_directory = target_directory.resolve() with tempfile.TemporaryDirectory() as build_directory: os.chdir(str(build_directory)) nix = Nix(logger=logger) nix.shell( command= f"pip wheel {escaped_requirement} --find-links {str(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 = target_directory / target_file_name shutil.move(wheel_path, str(target_path)) return target_file_name
def find_root(start: Path = Path(".")) -> Path: absolute_location = start.resolve() if (absolute_location / ".git").is_directory(): return absolute_location else: return find_root(absolute_location / "..")