def __init__(self, name, directory=".", packages=None, includes=None): self._name = module_name(name) self._in_src = False self._is_package = False self._path = Path(directory) self._includes = [] packages = packages or [] includes = includes or [] if not packages: # It must exist either as a .py file or a directory, but not both pkg_dir = Path(directory, self._name) py_file = Path(directory, self._name + ".py") if pkg_dir.is_dir() and py_file.is_file(): raise ValueError("Both {} and {} exist".format( pkg_dir, py_file)) elif pkg_dir.is_dir(): packages = [{"include": str(pkg_dir.relative_to(self._path))}] elif py_file.is_file(): packages = [{"include": str(py_file.relative_to(self._path))}] else: # Searching for a src module src = Path(directory, "src") src_pkg_dir = src / self._name src_py_file = src / (self._name + ".py") if src_pkg_dir.is_dir() and src_py_file.is_file(): raise ValueError("Both {} and {} exist".format( pkg_dir, py_file)) elif src_pkg_dir.is_dir(): packages = [{ "include": str(src_pkg_dir.relative_to(src)), "from": str(src.relative_to(self._path)), }] elif src_py_file.is_file(): packages = [{ "include": str(src_py_file.relative_to(src)), "from": str(src.relative_to(self._path)), }] else: raise ModuleOrPackageNotFound( "No file/folder found for package {}".format(name)) for package in packages: formats = package.get("format") if formats and not isinstance(formats, list): formats = [formats] self._includes.append( PackageInclude( self._path, package["include"], formats=formats, source=package.get("from"), )) for include in includes: self._includes.append( Include(self._path, include["path"], formats=include["format"]))
def get_package_paths(cls, env: Env, name: str) -> Set[Path]: """ Process a .pth file within the site-packages directories, and return any valid paths. We skip executable .pth files as there is no reliable means to do this without side-effects to current run-time. Mo check is made that the item refers to a directory rather than a file, however, in order to maintain backwards compatibility, we allow non-existing paths to be discovered. The latter behaviour is different to how Python's site-specific hook configuration works. Reference: https://docs.python.org/3.8/library/site.html :param env: The environment to search for the .pth file in. :param name: The name of the package to search .pth file for. :return: A `Set` of valid `Path` objects. """ paths = set() # we identify the candidate pth files to check, this is done so to handle cases # where the pth file for foo-bar might have been installed as either foo-bar.pth or # foo_bar.pth (expected) in either pure or platform lib directories. candidates = itertools.product( {env.purelib, env.platlib}, {name, module_name(name)}, ) for lib, module in candidates: pth_file = lib.joinpath(module).with_suffix(".pth") if not pth_file.exists(): continue with pth_file.open() as f: for line in f: line = line.strip() if line and not line.startswith( ("#", "import ", "import\t")): path = Path(line) if not path.is_absolute(): try: path = lib.joinpath(path).resolve() except FileNotFoundError: # this is required to handle pathlib oddity on win32 python==3.5 path = lib.joinpath(path) paths.add(path) src_path = env.path / "src" / name if not paths and src_path.exists(): paths.add(src_path) return paths
def __init__( self, name: str, directory: str = ".", packages: list[dict[str, Any]] | None = None, includes: list[dict[str, Any]] | None = None, ) -> None: from poetry.core.masonry.utils.include import Include from poetry.core.masonry.utils.package_include import PackageInclude from poetry.core.utils.helpers import module_name self._name = module_name(name) self._in_src = False self._is_package = False self._path = Path(directory) self._includes: list[Include] = [] packages = packages or [] includes = includes or [] if not packages: # It must exist either as a .py file or a directory, but not both pkg_dir = Path(directory, self._name) py_file = Path(directory, self._name + ".py") if pkg_dir.is_dir() and py_file.is_file(): raise ValueError(f"Both {pkg_dir} and {py_file} exist") elif pkg_dir.is_dir(): packages = [{"include": str(pkg_dir.relative_to(self._path))}] elif py_file.is_file(): packages = [{"include": str(py_file.relative_to(self._path))}] else: # Searching for a src module src = Path(directory, "src") src_pkg_dir = src / self._name src_py_file = src / (self._name + ".py") if src_pkg_dir.is_dir() and src_py_file.is_file(): raise ValueError(f"Both {pkg_dir} and {py_file} exist") elif src_pkg_dir.is_dir(): packages = [{ "include": str(src_pkg_dir.relative_to(src)), "from": str(src.relative_to(self._path)), }] elif src_py_file.is_file(): packages = [{ "include": str(src_py_file.relative_to(src)), "from": str(src.relative_to(self._path)), }] else: raise ModuleOrPackageNotFound( f"No file/folder found for package {name}") for package in packages: formats = package.get("format") if formats and not isinstance(formats, list): formats = [formats] self._includes.append( PackageInclude( self._path, package["include"], formats=formats, source=package.get("from"), )) for include in includes: self._includes.append( Include(self._path, include["path"], formats=include["format"]))