-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: introduce self add/remove/install commands
- Loading branch information
Showing
10 changed files
with
256 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from __future__ import annotations | ||
|
||
from poetry.console.commands.add import AddCommand | ||
from poetry.console.commands.self.self_command import SelfCommand | ||
|
||
|
||
class SelfAddCommand(SelfCommand, AddCommand): | ||
name = "self add" | ||
description = "Add additional packages to Poetry's runtime environment." | ||
options = [ | ||
o | ||
for o in AddCommand.options | ||
if o.name in {"editable", "extras", "source", "dry-run", "allow-prereleases"} | ||
] | ||
help = f"""\ | ||
The <c1>self add</c1> command installs additional package's to Poetry's runtime \ | ||
environment. | ||
This is managed in the <comment>{str(SelfCommand.system_pyproject)}</> file. | ||
{AddCommand.examples} | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from __future__ import annotations | ||
|
||
from poetry.console.commands.install import InstallCommand | ||
from poetry.console.commands.self.self_command import SelfCommand | ||
|
||
|
||
class SelfInstallCommand(SelfCommand, InstallCommand): | ||
name = "self install" | ||
description = "Add additional packages to Poetry's runtime environment." | ||
options = [o for o in InstallCommand.options if o.name in {"sync", "dry-run"}] | ||
help = f"""\ | ||
The <c1>self install</c1> command all additional packages specified are installed in \ | ||
the current runtime environment. | ||
This is managed in the <comment>{str(SelfCommand.system_pyproject)}</> file. | ||
You can add more packages using the <c1>self add</c1> command and remove them using \ | ||
the <c1>self remove</c1> command. | ||
""" | ||
|
||
@property | ||
def activated_groups(self) -> set[str]: | ||
return {"default", self.default_group} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from __future__ import annotations | ||
|
||
from poetry.console.commands.remove import RemoveCommand | ||
from poetry.console.commands.self.self_command import SelfCommand | ||
|
||
|
||
class SelfRemoveCommand(SelfCommand, RemoveCommand): | ||
name = "self remove" | ||
description = "Remove additional packages from Poetry's runtime environment." | ||
options = [o for o in RemoveCommand.options if o.name in {"dry-run"}] | ||
help = f"""\ | ||
The <c1>self remove</c1> command removes additional package's to Poetry's runtime \ | ||
environment. | ||
This is managed in the <comment>{str(SelfCommand.system_pyproject)}</> file. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
from contextlib import contextmanager | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
from poetry.core.packages.dependency import Dependency | ||
from poetry.core.pyproject.toml import PyProjectTOML | ||
|
||
from poetry.__version__ import __version__ | ||
from poetry.console.commands.installer_command import InstallerCommand | ||
from poetry.factory import Factory | ||
from poetry.packages.project_package import ProjectPackage | ||
from poetry.utils.env import EnvManager | ||
from poetry.utils.env import SystemEnv | ||
|
||
|
||
if TYPE_CHECKING: | ||
from typing import Iterator | ||
|
||
from poetry.poetry import Poetry | ||
from poetry.utils.env import Env | ||
|
||
|
||
class SelfCommand(InstallerCommand): | ||
@property | ||
def system_pyproject(self) -> Path: | ||
from poetry.locations import CONFIG_DIR | ||
|
||
return Path(CONFIG_DIR).joinpath("pyproject.toml") | ||
|
||
def reset_env(self) -> None: | ||
self._env = EnvManager.get_system_env(naive=True) | ||
|
||
@property | ||
def env(self) -> Env: | ||
if self._env is None or not isinstance(self._env, SystemEnv): | ||
self.reset_env() | ||
return self._env | ||
|
||
@property | ||
def default_group(self) -> str: | ||
return "additional" | ||
|
||
@property | ||
def activated_groups(self) -> set[str]: | ||
return {self.default_group} | ||
|
||
@contextmanager | ||
def with_system_project(self) -> Iterator[Path]: | ||
from poetry.locations import CONFIG_DIR | ||
|
||
cwd = Path.cwd() | ||
try: | ||
os.chdir(CONFIG_DIR) | ||
yield Path(CONFIG_DIR) | ||
finally: | ||
os.chdir(cwd) | ||
|
||
def generate_system_pyproject(self) -> None: | ||
preserved = {} | ||
|
||
if self.system_pyproject.exists(): | ||
content = PyProjectTOML(self.system_pyproject).poetry_config | ||
|
||
for key in {"group", "source"}: | ||
if key in content: | ||
preserved[key] = content[key] | ||
|
||
package = ProjectPackage(name="poetry-instance", version=__version__) | ||
package.add_dependency(Dependency(name="poetry", constraint=__version__)) | ||
|
||
package.python_versions = ".".join(str(v) for v in self.env.version_info[:3]) | ||
|
||
content = Factory.create_pyproject_from_package(package=package) | ||
|
||
for key in preserved: | ||
content[key] = preserved[key] | ||
|
||
self.system_pyproject.write_text(content.as_string(), encoding="utf-8") | ||
|
||
def reset_poetry(self) -> None: | ||
with self.with_system_project(): | ||
self.generate_system_pyproject() | ||
self._poetry = Factory().create_poetry( | ||
self.system_pyproject.parent, io=self._io, disable_plugins=True | ||
) | ||
|
||
@property | ||
def poetry(self) -> Poetry: | ||
if self._poetry is None: | ||
self.reset_poetry() | ||
assert self._poetry | ||
return self._poetry | ||
|
||
def _system_project_handle(self) -> int: | ||
return super().handle() | ||
|
||
def handle(self) -> int: | ||
self.reset_env() | ||
self.reset_poetry() | ||
|
||
with self.with_system_project(): | ||
return self._system_project_handle() |
Oops, something went wrong.