Skip to content

Commit

Permalink
Add dedicated option for sudo execution. Will be helpful later to add…
Browse files Browse the repository at this point in the history
…ress #33.
  • Loading branch information
kdeldycke committed Apr 23, 2022
1 parent 2f0d812 commit 5415a64
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 20 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This version is not released yet and is under active development.
- \[mpm\] Remove `-c`/`--cli-format` option.
- \[mpm\] Use short-form selection option and fully-qualified path in
`mpm`-based upgrade-all CLIs produced by `outdated` command.
- \[mpm\] Add dedicated execution path for running sudo-prefixed commands.
- \[mpm\] Fix local overriding of CLI parameters leading to missing `sudo`
pre-command. Closes {issue}`579`.
- \[mpm\] Use string highlighting code from `click-extra >= 2.1.0`.
Expand Down
21 changes: 18 additions & 3 deletions meta_package_manager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ def build_cli(
override_cli_path=False,
override_pre_args=False,
override_post_args=False,
sudo=False,
):
"""Build the package manager CLI by combining the custom ``*args`` with the PM's
global parameters.
Expand All @@ -402,7 +403,7 @@ def build_cli(
.. code-block:: shell-session
$ <self.pre_cmds> <self.cli_path> <self.pre_args> <*args> <self.post_args>
$ [<self.pre_cmds>|sudo] <self.cli_path> <self.pre_args> <*args> <self.post_args>
* ``self.pre_cmds`` is added before the CLI path.
Expand All @@ -418,13 +419,25 @@ def build_cli(
Each global set of elements can be locally overriden with:
* ``override_pre_cmds=tuple()``
* ``override_cli_path=str``
* ``override_pre_args=tuple()``
* ``override_post_args=tuple()``
On linux, the command can be ran with `sudo` if the parameter of the same name is set to `True`.
In which case `override_pre_cmds` are not allowed and `auto_pre_cmds` is forced to `False`.
"""
params = []

# Prepare the full list of CLI arguments.
if override_pre_cmds:
# Sudo replaces any pre-command, be it overriden or automatic.
if sudo:
if not is_linux():
raise NotImplementedError("sudo only supported on Linux.")
if override_pre_cmds:
raise ValueError("Pre-commands not allowed if sudo is requested.")
if auto_pre_cmds:
auto_pre_cmds = False
params.add("sudo")
elif override_pre_cmds:
assert isinstance(override_pre_cmds, tuple)
params.extend(override_pre_cmds)
elif auto_pre_cmds:
Expand Down Expand Up @@ -466,6 +479,7 @@ def run_cli(
override_pre_args=False,
override_post_args=False,
force_exec=False,
sudo=False,
):
"""Build and run the package manager CLI by combining the custom ``*args`` with
the PM's global parameters.
Expand All @@ -490,6 +504,7 @@ def run_cli(
override_cli_path=override_cli_path,
override_pre_args=override_pre_args,
override_post_args=override_post_args,
sudo=sudo,
)

# Prepare the full list of CLI arguments.
Expand Down
12 changes: 5 additions & 7 deletions meta_package_manager/managers/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def install(self, package_id):
► sudo apt install git --yes --quiet
"""
super().install(package_id)
return self.run_cli("install", package_id, override_pre_cmds=("sudo",))
return self.run_cli("install", package_id, sudo=True)

def upgrade_cli(self, package_id=None):
"""Generates the CLI to upgrade all packages (default) or only the one provided as parameter.
Expand All @@ -237,10 +237,8 @@ def upgrade_cli(self, package_id=None):
► sudo apt install --only-upgrade git --yes --quiet
"""
if package_id:
return self.build_cli(
"install", "--only-upgrade", package_id, override_pre_cmds=("sudo",)
)
return self.build_cli("upgrade", override_pre_cmds=("sudo",))
return self.build_cli("install", "--only-upgrade", package_id, sudo=True)
return self.build_cli("upgrade", sudo=True)

def sync(self):
"""Sync package metadata.
Expand All @@ -258,7 +256,7 @@ def sync(self):
Reading state information...
"""
super().sync()
self.run_cli("update", override_pre_cmds=("sudo",))
self.run_cli("update", sudo=True)

def cleanup(self):
"""Removes things we don't need anymore.
Expand All @@ -270,7 +268,7 @@ def cleanup(self):
"""
super().cleanup()
for command in ("autoremove", "clean"):
self.run_cli(command, override_pre_cmds=("sudo",))
self.run_cli(command, sudo=True)


class APT_Mint(APT):
Expand Down
8 changes: 3 additions & 5 deletions meta_package_manager/managers/dnf.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ def install(self, package_id):
► sudo dnf --color=never --assumeyes install pip
"""
super().install(package_id)
return self.run_cli(
"--assumeyes", "install", package_id, override_pre_cmds=("sudo",)
)
return self.run_cli("--assumeyes", "install", package_id, sudo=True)

def upgrade_cli(self, package_id=None):
"""Generates the CLI to upgrade all packages (default) or only the one provided as parameter.
Expand All @@ -181,7 +179,7 @@ def upgrade_cli(self, package_id=None):
► sudo dnf --color=never --assumeyes upgrade pip
"""
return self.build_cli("upgrade", package_id)
return self.build_cli("upgrade", package_id, sudo=True)

def sync(self):
"""Sync package metadata.
Expand All @@ -202,5 +200,5 @@ def cleanup(self):
► dnf --color=never clean all
"""
super().cleanup()
self.run_cli("--assumeyes", "autoremove", override_pre_cmds=("sudo",))
self.run_cli("--assumeyes", "autoremove", sudo=True)
self.run_cli("clean", "all")
11 changes: 6 additions & 5 deletions meta_package_manager/managers/emerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def install(self, package_id):
► sudo emerge --color n --nospinner dev-vcs/git
"""
super().install(package_id)
return self.run_cli("--color", "n", "--nospinner", package_id, override_pre_cmds=("sudo",))
return self.run_cli("--color", "n", "--nospinner", package_id, sudo=True)

def upgrade_cli(self, package_id="@world"):
"""Generates the CLI to upgrade all packages (default) or only the one provided as parameter.
Expand All @@ -233,7 +233,7 @@ def upgrade_cli(self, package_id="@world"):
if package_id == "@world":
update_params = ("--newuse", "--deep")

return self.build_cli("--update", *update_params, "--color", "n", "--nospinner", package_id, override_pre_cmds=("sudo",))
return self.build_cli("--update", *update_params, "--color", "n", "--nospinner", package_id, sudo=True)

def sync(self):
"""Sync package metadata.
Expand All @@ -243,7 +243,7 @@ def sync(self):
► sudo emerge --sync --color n --nospinner
"""
super().sync()
self.run_cli("--sync", "--color", "n", "--nospinner", override_pre_cmds=("sudo",))
self.run_cli("--sync", "--color", "n", "--nospinner", sudo=True)

def cleanup(self):
"""Removes things we don't need anymore.
Expand All @@ -263,5 +263,6 @@ def cleanup(self):
super().cleanup()
# Forces an upgrade first, as recommended by emerge documentation.
self.upgrade()
self.run_cli("--depclean", override_pre_cmds=("sudo",))
self.run_cli("distfiles", override_cli_path="eclean", override_pre_cmds=("sudo",))
self.run_cli("--depclean", sudo=True)
# XXX
self.run_cli("distfiles", override_cli_path="eclean", sudo=True)

0 comments on commit 5415a64

Please sign in to comment.