def set_installer(self, event: ConsoleCommandEvent, event_name: str, _: Any) -> None: from .commands.installer_command import InstallerCommand command: InstallerCommand = cast(InstallerCommand, event.command) if not isinstance(command, InstallerCommand): return # If the command already has an installer # we skip this step if command.installer is not None: return from poetry.installation.installer import Installer poetry = command.poetry installer = Installer( event.io, command.env, poetry.package, poetry.locker, poetry.pool, poetry.config, ) installer.use_executor( poetry.config.get("experimental.new-installer", False)) command.set_installer(installer)
def set_installer(self, event, event_name, _): # type: (PreHandleEvent, str, Any) -> None command = event.command.config.handler # type: InstallerCommand if not isinstance(command, InstallerCommand): return # If the command already has an installer # we skip this step if command.installer is not None: return from poetry.installation.installer import Installer poetry = command.poetry installer = Installer( event.io, command.env, poetry.package, poetry.locker, poetry.pool, poetry.config, ) installer.use_executor( poetry.config.get("experimental.new-installer", False)) command.set_installer(installer)
def handle(self): from poetry.installation.installer import Installer installer = Installer(self.io, self.env, self.poetry.package, self.poetry.locker, self.poetry.pool) installer.lock() return installer.run()
def handle(self): from poetry.installation.installer import Installer packages = self.argument("packages") is_dev = self.option("dev") original_content = self.poetry.file.read() content = self.poetry.file.read() poetry_content = content["tool"]["poetry"] section = "dependencies" if is_dev: section = "dev-dependencies" # Deleting entries requirements = {} for name in packages: found = False for key in poetry_content[section]: if key.lower() == name.lower(): found = True requirements[key] = poetry_content[section][key] break if not found: raise ValueError("Package {} not found".format(name)) for key in requirements: del poetry_content[section][key] # Write the new content back self.poetry.file.write(content) # Update packages self.reset_poetry() installer = Installer(self.io, self.env, self.poetry.package, self.poetry.locker, self.poetry.pool) installer.dry_run(self.option("dry-run")) installer.update(True) installer.whitelist(requirements) try: status = installer.run() except Exception: self.poetry.file.write(original_content) raise if status != 0 or self.option("dry-run"): # Revert changes if not self.option("dry-run"): self.error("\n" "Removal failed, reverting pyproject.toml " "to its original content.") self.poetry.file.write(original_content) return status
def _update(self, version: Version) -> None: from poetry.core.packages.dependency import Dependency from poetry.core.packages.project_package import ProjectPackage from poetry.config.config import Config from poetry.installation.installer import Installer from poetry.packages.locker import NullLocker from poetry.repositories.installed_repository import InstalledRepository from poetry.utils.env import EnvManager env = EnvManager.get_system_env(naive=True) installed = InstalledRepository.load(env) root = ProjectPackage("poetry-updater", "0.0.0") root.python_versions = ".".join(str(c) for c in env.version_info[:3]) root.add_dependency(Dependency("poetry", version.text)) installer = Installer( self.io, env, root, NullLocker(self.data_dir.joinpath("poetry.lock"), {}), self.pool, Config(), installed=installed, ) installer.update(True) installer.dry_run(self.option("dry-run")) installer.run()
def _configure_installer(self, command: InstallerCommand, io: IO) -> None: from poetry.installation.installer import Installer poetry = command.poetry installer = Installer( io, command.env, poetry.package, poetry.locker, poetry.pool, poetry.config, ) installer.use_executor(poetry.config.get("experimental.new-installer", False)) command.set_installer(installer)
def tester(app, poetry, config, executor, env): tester = CommandTester(app.find("add")) executor._io = tester.io installer = Installer( tester.io, env, poetry.package, poetry.locker, poetry.pool, config, executor=executor, ) installer.use_executor(True) tester._command.set_installer(installer) tester._command.set_env(env) return tester
def _make_installer(poetry, env, io): "Make a fresh installer." installer = Installer( NullIO() if not io.is_debug() else io, env, poetry.package, poetry.locker, poetry.pool, poetry.config, ) installer.dev_mode(False) installer.remove_untracked() installer.use_executor( poetry.config.get("experimental.new-installer", False)) return installer
def handle(self): from clikit.io import NullIO from poetry.installation.installer import Installer from poetry.masonry.builders import EditableBuilder from poetry.masonry.utils.module import ModuleOrPackageNotFound installer = Installer(self.io, self.env, self.poetry.package, self.poetry.locker, self.poetry.pool) extras = [] for extra in self.option("extras"): if " " in extra: extras += [e.strip() for e in extra.split(" ")] else: extras.append(extra) installer.extras(extras) installer.dev_mode(not self.option("no-dev")) installer.dry_run(self.option("dry-run")) installer.verbose(self.option("verbose")) return_code = installer.run() if return_code != 0: return return_code if self.option("no-root"): return 0 try: builder = EditableBuilder(self.poetry, self._env, NullIO()) except ModuleOrPackageNotFound: # This is likely due to the fact that the project is an application # not following the structure expected by Poetry # If this is a true error it will be picked up later by build anyway. return 0 self.line(" - Installing <c1>{}</c1> (<b>{}</b>)".format( self.poetry.package.pretty_name, self.poetry.package.pretty_version)) if self.option("dry-run"): return 0 builder.build() return 0
def handle(self): from poetry.installation.installer import Installer from poetry.semver import parse_constraint from tomlkit import inline_table packages = self.argument("name") is_dev = self.option("dev") if self.option("extras") and len(packages) > 1: raise ValueError("You can only specify one package " "when using the --extras option") section = "dependencies" if is_dev: section = "dev-dependencies" original_content = self.poetry.file.read() content = self.poetry.file.read() poetry_content = content["tool"]["poetry"] if section not in poetry_content: poetry_content[section] = {} for name in packages: for key in poetry_content[section]: if key.lower() == name.lower(): pair = self._parse_requirements([name])[0] if ("git" in pair or "url" in pair or pair.get("version") == "latest"): continue raise ValueError( "Package {} is already present".format(name)) requirements = self._determine_requirements( packages, allow_prereleases=self.option("allow-prereleases")) for _constraint in requirements: if "version" in _constraint: # Validate version constraint parse_constraint(_constraint["version"]) constraint = inline_table() for name, value in _constraint.items(): if name == "name": continue constraint[name] = value if self.option("optional"): constraint["optional"] = True if self.option("allow-prereleases"): constraint["allow-prereleases"] = True if self.option("extras"): extras = [] for extra in self.option("extras"): if " " in extra: extras += [e.strip() for e in extra.split(" ")] else: extras.append(extra) constraint["extras"] = self.option("extras") if self.option("python"): constraint["python"] = self.option("python") if self.option("platform"): constraint["platform"] = self.option("platform") if len(constraint) == 1 and "version" in constraint: constraint = constraint["version"] poetry_content[section][_constraint["name"]] = constraint # Write new content self.poetry.file.write(content) # Cosmetic new line self.line("") # Update packages self.reset_poetry() installer = Installer(self.io, self.env, self.poetry.package, self.poetry.locker, self.poetry.pool) installer.dry_run(self.option("dry-run")) installer.update(True) installer.whitelist([r["name"] for r in requirements]) try: status = installer.run() except Exception: self.poetry.file.write(original_content) raise if status != 0 or self.option("dry-run"): # Revert changes if not self.option("dry-run"): self.error("\n" "Addition failed, reverting pyproject.toml " "to its original content.") self.poetry.file.write(original_content) return status
def handle(self): from poetry.installation.installer import Installer packages = self.argument("packages") installer = Installer(self.io, self.env, self.poetry.package, self.poetry.locker, self.poetry.pool) if packages: installer.whitelist({name: "*" for name in packages}) installer.dev_mode(not self.option("no-dev")) installer.dry_run(self.option("dry-run")) installer.execute_operations(not self.option("lock")) # Force update installer.update(True) return installer.run()