def load(cls, env): # type: (Env) -> InstalledRepository """ Load installed packages. For now, it uses the pip "freeze" command. """ repo = cls() seen = set() for entry in env.sys_path: for distribution in sorted( metadata.distributions(path=[entry]), key=lambda d: str(d._path), ): name = distribution.metadata["name"] version = distribution.metadata["version"] package = Package(name, version, version) package.description = distribution.metadata.get("summary", "") if package.name in seen: continue seen.add(package.name) repo.add_package(package) path = Path(str(distribution._path)) is_standard_package = True try: path.relative_to(env.site_packages) except ValueError: is_standard_package = False if is_standard_package: continue src_path = env.path / "src" # A VCS dependency should have been installed # in the src directory. If not, it's a path dependency try: path.relative_to(src_path) from poetry.vcs.git import Git git = Git() revision = git.rev_parse("HEAD", src_path / package.name).strip() url = git.remote_url(src_path / package.name) package.source_type = "git" package.source_url = url package.source_reference = revision except ValueError: package.source_type = "directory" package.source_url = str(path.parent) return repo
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))
def _check_recommended_installation(self): # type: () -> None from pathlib import Path current = Path(__file__) try: current.relative_to(self.home) except ValueError: raise RuntimeError( "Poetry was not installed with the recommended installer. " "Cannot update automatically.")
def load(cls, env): # type: (Env) -> InstalledRepository """ Load installed packages. """ repo = cls() seen = set() for entry in reversed(env.sys_path): for distribution in sorted( metadata.distributions(path=[entry]), key=lambda d: str(d._path), ): name = distribution.metadata["name"] path = Path(str(distribution._path)) version = distribution.metadata["version"] package = Package(name, version, version) package.description = distribution.metadata.get("summary", "") if package.name in seen: continue try: path.relative_to(_VENDORS) except ValueError: pass else: continue seen.add(package.name) repo.add_package(package) is_standard_package = env.is_path_relative_to_lib(path) if is_standard_package: if path.name.endswith(".dist-info"): paths = cls.get_package_paths(env=env, name=package.pretty_name) if paths: for src in paths: if cls.is_vcs_package(src, env): cls.set_package_vcs_properties(package, env) break else: # TODO: handle multiple source directories? package._source_type = "directory" package._source_url = paths.pop().as_posix() continue if cls.is_vcs_package(path, env): cls.set_package_vcs_properties(package, env) else: # If not, it's a path dependency package._source_type = "directory" package._source_url = str(path.parent) return repo
def _check_recommended_installation(self): # type: () -> None from pathlib import Path current = Path(__file__) try: current.relative_to(self.home) except ValueError: raise PoetrySimpleConsoleException( "Poetry was not installed with the recommended installer, " "so it cannot be updated automatically." )
def handle(self): from poetry.__version__ import __version__ from poetry.repositories.pypi_repository import PyPiRepository from poetry.semver import Version from poetry.utils._compat import Path current = Path(__file__) try: current.relative_to(self.home) except ValueError: raise RuntimeError( "Poetry was not installed with the recommended installer. " "Cannot update automatically.") version = self.argument("version") if not version: version = ">=" + __version__ repo = PyPiRepository(fallback=False) packages = repo.find_packages("poetry", version, allow_prereleases=self.option("preview")) if not packages: self.line("No release found for the specified version") return packages.sort(key=cmp_to_key(lambda x, y: 0 if x.version == y.version else int(x.version < y.version or -1))) release = None for package in packages: if package.is_prerelease(): if self.option("preview"): release = package break continue release = package break if release is None: self.line("No new release found") return if release.version == Version.parse(__version__): self.line("You are using the latest version") return self.update(release)
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 ValueError("No file/folder found for package {}".format(name)) for package in packages: self._includes.append( PackageInclude(self._path, package["include"], package.get("from")) ) for include in includes: self._includes.append(Include(self._path, include))
def load(cls, env): # type: (Env) -> InstalledRepository """ Load installed packages. """ repo = cls() seen = set() for entry in reversed(env.sys_path): for distribution in sorted( metadata.distributions(path=[entry]), key=lambda d: str(d._path), ): name = distribution.metadata["name"] path = Path(str(distribution._path)) version = distribution.metadata["version"] package = Package(name, version, version) package.description = distribution.metadata.get("summary", "") if package.name in seen: continue try: path.relative_to(_VENDORS) except ValueError: pass else: continue seen.add(package.name) repo.add_package(package) is_standard_package = True try: path.relative_to(env.site_packages) except ValueError: is_standard_package = False if is_standard_package: if path.name.endswith(".dist-info"): paths = cls.get_package_paths( sitedir=env.site_packages, name=package.pretty_name) if paths: # TODO: handle multiple source directories? package.source_type = "directory" package.source_url = paths.pop().as_posix() continue src_path = env.path / "src" # A VCS dependency should have been installed # in the src directory. If not, it's a path dependency try: path.relative_to(src_path) from poetry.core.vcs.git import Git git = Git() revision = git.rev_parse("HEAD", src_path / package.name).strip() url = git.remote_url(src_path / package.name) package.source_type = "git" package.source_url = url package.source_reference = revision except ValueError: package.source_type = "directory" package.source_url = str(path.parent) return repo