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 dependencies(self): if self._dependencies is None: # avoid circular dependency when loading DirectoryDependency from poetry.core.packages import DirectoryDependency from poetry.core.packages import FileDependency from poetry.core.packages import dependency_from_pep_508 self._dependencies = [] for requirement in self.requires: dependency = None try: dependency = dependency_from_pep_508(requirement) except ValueError: # PEP 517 requires can be path if not PEP 508 path = Path(requirement) try: if path.is_file(): dependency = FileDependency(name=canonicalize_name( path.name), path=path) elif path.is_dir(): dependency = DirectoryDependency( name=canonicalize_name(path.name), path=path) except OSError: # compatibility Python < 3.8 # https://docs.python.org/3/library/pathlib.html#methods pass if dependency is None: # skip since we could not determine requirement continue self._dependencies.append(dependency) return self._dependencies
def _make_file_or_dir_dep( name, path, base=None ): # type: (str, Path, Optional[Path]) -> Optional[Union[FileDependency, DirectoryDependency]] """ Helper function to create a file or directoru dependency with the given arguments. If path is not a file or directory that exists, `None` is returned. """ _path = path if not path.is_absolute() and base: # a base path was specified, so we should respect that _path = Path(base) / path if _path.is_file(): return FileDependency(name, path, base=base) elif _path.is_dir(): return DirectoryDependency(name, path, base=base) return None