|
| 1 | +import os |
| 2 | +import re |
| 3 | +from pathlib import Path |
| 4 | +from typing import Dict, Iterable, List, Tuple, Union |
| 5 | + |
| 6 | +__all__ = ( |
| 7 | + "EditableProject", |
| 8 | + "__version__", |
| 9 | +) |
| 10 | + |
| 11 | +__version__ = "0.5" |
| 12 | + |
| 13 | + |
| 14 | +# Check if a project name is valid, based on PEP 426: |
| 15 | +# https://peps.python.org/pep-0426/#name |
| 16 | +def is_valid(name: str) -> bool: |
| 17 | + return ( |
| 18 | + re.match(r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", name, re.IGNORECASE) |
| 19 | + is not None |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +# Slightly modified version of the normalisation from PEP 503: |
| 24 | +# https://peps.python.org/pep-0503/#normalized-names |
| 25 | +# This version uses underscore, so that the result is more |
| 26 | +# likely to be a valid import name |
| 27 | +def normalize(name: str) -> str: |
| 28 | + return re.sub(r"[-_.]+", "_", name).lower() |
| 29 | + |
| 30 | + |
| 31 | +class EditableException(Exception): |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +class EditableProject: |
| 36 | + def __init__(self, project_name: str, project_dir: Union[str, os.PathLike]) -> None: |
| 37 | + if not is_valid(project_name): |
| 38 | + raise ValueError(f"Project name {project_name} is not valid") |
| 39 | + self.project_name = normalize(project_name) |
| 40 | + self.bootstrap = f"_editable_impl_{self.project_name}" |
| 41 | + self.project_dir = Path(project_dir) |
| 42 | + self.redirections: Dict[str, str] = {} |
| 43 | + self.path_entries: List[Path] = [] |
| 44 | + self.subpackages: Dict[str, Path] = {} |
| 45 | + |
| 46 | + def make_absolute(self, path: Union[str, os.PathLike]) -> Path: |
| 47 | + return (self.project_dir / path).resolve() |
| 48 | + |
| 49 | + def map(self, name: str, target: Union[str, os.PathLike]) -> None: |
| 50 | + if "." in name: |
| 51 | + raise EditableException( |
| 52 | + f"Cannot map {name} as it is not a top-level package" |
| 53 | + ) |
| 54 | + abs_target = self.make_absolute(target) |
| 55 | + if abs_target.is_dir(): |
| 56 | + abs_target = abs_target / "__init__.py" |
| 57 | + if abs_target.is_file(): |
| 58 | + self.redirections[name] = str(abs_target) |
| 59 | + else: |
| 60 | + raise EditableException(f"{target} is not a valid Python package or module") |
| 61 | + |
| 62 | + def add_to_path(self, dirname: Union[str, os.PathLike]) -> None: |
| 63 | + self.path_entries.append(self.make_absolute(dirname)) |
| 64 | + |
| 65 | + def add_to_subpackage(self, package: str, dirname: Union[str, os.PathLike]) -> None: |
| 66 | + self.subpackages[package] = self.make_absolute(dirname) |
| 67 | + |
| 68 | + def files(self) -> Iterable[Tuple[str, str]]: |
| 69 | + yield f"{self.project_name}.pth", self.pth_file() |
| 70 | + if self.subpackages: |
| 71 | + for package, location in self.subpackages.items(): |
| 72 | + yield self.package_redirection(package, location) |
| 73 | + if self.redirections: |
| 74 | + yield f"{self.bootstrap}.py", self.bootstrap_file() |
| 75 | + |
| 76 | + def dependencies(self) -> List[str]: |
| 77 | + deps = [] |
| 78 | + if self.redirections: |
| 79 | + deps.append("editables") |
| 80 | + return deps |
| 81 | + |
| 82 | + def pth_file(self) -> str: |
| 83 | + lines = [] |
| 84 | + if self.redirections: |
| 85 | + lines.append(f"import {self.bootstrap}") |
| 86 | + for entry in self.path_entries: |
| 87 | + lines.append(str(entry)) |
| 88 | + return "\n".join(lines) |
| 89 | + |
| 90 | + def package_redirection(self, package: str, location: Path) -> Tuple[str, str]: |
| 91 | + init_py = package.replace(".", "/") + "/__init__.py" |
| 92 | + content = f"__path__ = [{str(location)!r}]" |
| 93 | + return init_py, content |
| 94 | + |
| 95 | + def bootstrap_file(self) -> str: |
| 96 | + bootstrap = [ |
| 97 | + "from editables.redirector import RedirectingFinder as F", |
| 98 | + "F.install()", |
| 99 | + ] |
| 100 | + for name, path in self.redirections.items(): |
| 101 | + bootstrap.append(f"F.map_module({name!r}, {path!r})") |
| 102 | + return "\n".join(bootstrap) |
0 commit comments