Skip to content

Commit

Permalink
refactor: completely remove old SudoLoop (re: #729)
Browse files Browse the repository at this point in the history
  • Loading branch information
actionless committed Jul 15, 2023
1 parent 4fc1950 commit 524f6df
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 67 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,6 @@ Comma-separated list of packages names or globs, which upgrade should have addit
##### PacmanPath (default: pacman)
Path to pacman executable.

##### SudoLoopInterval (default: 59)
Interval in seconds in which `sudo` command will be spawned in the background
to avoid asking for sudo password more than once
(`-1` to disable sudo loop at all).

##### PrivilegeEscalationTool (default: sudo)
A tool used to escalate user privileges. If using `doas` then `persistent` option is required in `doas.conf`. For example:
```
Expand Down
4 changes: 0 additions & 4 deletions pikaur/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,6 @@ class DiffPagerValues:
},
},
"misc": {
"SudoLoopInterval": {
"data_type": INT,
"default": "59",
},
"PacmanPath": {
"data_type": STR,
"default": "pacman",
Expand Down
44 changes: 2 additions & 42 deletions pikaur/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import subprocess # nosec B404
import sys
import tempfile
from multiprocessing.pool import ThreadPool
from pathlib import Path
from time import sleep
from typing import TYPE_CHECKING

import pyalpm
Expand All @@ -22,15 +20,14 @@

if TYPE_CHECKING:
# pylint: disable=cyclic-import
from collections.abc import Callable, Sequence
from typing import IO, Any, Final, TypeVar
from collections.abc import Sequence
from typing import IO, Any, Final

from typing_extensions import NotRequired, TypedDict

from .aur import AURPackageInfo

IOStream = IO[bytes] | int | None
SudoLoopResultT = TypeVar("SudoLoopResultT")

class SpawnArgs(TypedDict):
stdout: NotRequired["IOStream"]
Expand Down Expand Up @@ -176,11 +173,6 @@ def sudo(cmd: list[str]) -> list[str]:
return [PikaurConfig().misc.PrivilegeEscalationTool.get_str(), *cmd]


def get_sudo_refresh_command() -> list[str]:
pacman_path = PikaurConfig().misc.PacmanPath.get_str()
return sudo([pacman_path, "-T"])


class InteractiveSpawn(subprocess.Popen[bytes]):

stdout_text: str | None
Expand All @@ -193,7 +185,6 @@ def communicate(
if (
parse_args().print_commands
and not self._terminated
and self.args != get_sudo_refresh_command()
):
print_stderr(
color_line("=> ", ColorsHighlight.cyan) +
Expand Down Expand Up @@ -399,37 +390,6 @@ def dirname(path: str | Path) -> Path:
return Path(path).parent if path else Path(".")


def sudo_loop(*, once: bool = False) -> None:
"""Get sudo for further questions."""
sudo_loop_interval = PikaurConfig().misc.SudoLoopInterval.get_int()
if sudo_loop_interval == -1:
return
while True:
interactive_spawn(get_sudo_refresh_command())
if once:
break
sleep(sudo_loop_interval)


def run_with_sudo_loop(function: "Callable[..., SudoLoopResultT]") -> "SudoLoopResultT | None":
sudo_loop(once=True)
with ThreadPool(processes=2) as pool:
main_thread = pool.apply_async(function, ())
pool.apply_async(sudo_loop)
pool.close()
catched_exc: Exception | None = None
result: "SudoLoopResultT | None" = None
try:
result = main_thread.get()
except Exception as exc:
catched_exc = exc
finally:
pool.terminate()
if catched_exc:
raise catched_exc
return result


def check_systemd_dynamic_users() -> bool: # pragma: no cover
try:
out = subprocess.check_output(
Expand Down
6 changes: 1 addition & 5 deletions pikaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
check_runtime_deps,
interactive_spawn,
isolate_root_cmd,
run_with_sudo_loop,
running_as_root,
spawn,
sudo,
Expand Down Expand Up @@ -242,12 +241,9 @@ def _pikaur_operation(
sys.exit(interactive_spawn(
sudo(restart_args),
).returncode)
elif not require_sudo or running_as_root():
else:
# Just run the operation normally
pikaur_operation()
else:
# Or use sudo loop if not running as root but need to have it later
run_with_sudo_loop(pikaur_operation)


def cli_entry_point() -> None: # pylint: disable=too-many-statements
Expand Down
12 changes: 1 addition & 11 deletions pikaur/pikspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import shutil
import signal
import struct
import subprocess # nosec B404
import sys
import termios
import tty
Expand All @@ -27,8 +26,7 @@
from typing import TYPE_CHECKING

from .args import parse_args
from .config import PikaurConfig
from .core import DEFAULT_INPUT_ENCODING, get_sudo_refresh_command
from .core import DEFAULT_INPUT_ENCODING
from .i18n import translate
from .logging import create_logger
from .pacman_i18n import _p
Expand Down Expand Up @@ -366,14 +364,6 @@ def run(self) -> None:
try:
with NestedTerminal() as real_term_geometry:
self.real_term_geometry = real_term_geometry
if (
PikaurConfig().misc.PrivilegeEscalationTool.get_str() in self.args
): # pragma: no cover
subprocess.run( # nosec B603
get_sudo_refresh_command(), # noqa: S603
check=True,
)

result = spawn(
self.args,
master_read=self.cmd_output_reader,
Expand Down

0 comments on commit 524f6df

Please sign in to comment.