Skip to content

Commit 45b0ef3

Browse files
committed
rimport: Raise SystemExit(2) from exceptions.
1 parent 57972e5 commit 45b0ef3

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

rimport

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def stage_data(src: Path, inputdata_root: Path, staging_root: Path) -> None:
9494
* Raise if `src` is a broken symlink or is outside the inputdata root.
9595
"""
9696
if src.is_symlink() and src.exists():
97+
# TODO: This should be a regular message, not an error.
9798
raise RuntimeError("File is already published.")
9899
if src.is_symlink() and not src.exists():
99100
raise RuntimeError(f"Source is a broken symlink: {src}")
@@ -121,23 +122,23 @@ def ensure_running_as(target_user: str, argv: list[str]) -> None:
121122
"""If not running as `target_user`, re-exec via sudo -u target_user (handles 2FA via PAM)."""
122123
try:
123124
target_uid = pwd.getpwnam(target_user).pw_uid
124-
except KeyError:
125-
# TODO: Raise Python error instead of SystemExit
125+
except KeyError as exc:
126126
print(
127127
f"rimport: target user '{target_user}' not found on this system",
128128
file=sys.stderr,
129129
)
130-
raise SystemExit(2)
130+
raise SystemExit(2) from exc
131131

132132
if os.geteuid() != target_uid:
133-
if not sys.stdin.isatty():
133+
try:
134+
assert sys.stdin.isatty()
135+
except AssertionError as exc:
134136
print(
135137
f"rimport: need interactive TTY to authenticate as '{target_user}' (2FA).\n"
136138
f" Try: sudo -u {target_user} rimport …",
137139
file=sys.stderr,
138140
)
139-
# TODO: Raise Python error instead of SystemExit
140-
raise SystemExit(2)
141+
raise SystemExit(2) from exc
141142
# Re-exec under target user; this invokes sudo’s normal password/2FA flow.
142143
os.execvp("sudo", ["sudo", "-u", target_user, "--"] + argv)
143144

0 commit comments

Comments
 (0)