Skip to content

Commit

Permalink
delay backend import to show backend errors in root mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Enchufa2 committed Aug 7, 2024
1 parent 9cef27a commit 3ba0056
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: bspm
Type: Package
Title: Bridge to System Package Manager
Version: 0.5.7.1
Version: 0.5.7.2
Authors@R: c(
person("Iñaki", "Ucar", email="[email protected]",
role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550")))
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# bspm devel

- Fix "invalid escape sequence" SyntaxWarning (@pekkarr in #84).
- Catch and show early errors (e.g. no backend found) in root mode.

# bspm 0.5.7

Expand Down
28 changes: 14 additions & 14 deletions inst/service/bspm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/python3

import backend

from os import path
WDIR = path.dirname(path.realpath(__file__))
exec(open(WDIR + "/dbus-paths").read())
Expand All @@ -13,6 +11,7 @@ def read_conf(force_discover=False):

force_discover = force_discover and not path.exists(WDIR + "/nodiscover")
if force_discover or not path.exists(pref) or not path.exists(excl):
import backend
conf = backend.discover()
with open(pref, "w") as fpref, open(excl, "w") as fexcl:
for i in conf["prefixes"]:
Expand All @@ -24,6 +23,7 @@ def read_conf(force_discover=False):
EXCL = [line.rstrip() for line in fexcl]

def call_backend(cmd, pkgs=None, root=False):
import backend
msg = cmd.capitalize() + " system packages"
if root:
msg = msg + " as root"
Expand Down Expand Up @@ -115,25 +115,26 @@ def sigterm_handler(_signo, _stack_frame):
mainloop.run()

def run_as_root(args):
if args.cmd == "discover":
read_conf(True)
else:
try:
if args.cmd == "discover":
read_conf(True)
return
read_conf()
pkgs = None
if hasattr(args, "pkg"):
pkgs = args.pkg
try:
pkgs = call_backend(args.cmd, pkgs, root=True)
except Exception as err:
if args.o is not None:
with open(args.o, "a") as f:
print(str(err), file=f)
raise err
pkgs = call_backend(args.cmd, pkgs, root=True)
if args.o is not None:
with open(args.o, "a") as f:
for pkg in pkgs:
print(pkg, file=f)
else:
print("Result:", pkgs)
except Exception as err:
if args.o is not None:
with open(args.o, "a") as f:
print(str(err), file=f)
raise err

if __name__ == "__main__":
import argparse
Expand All @@ -147,9 +148,8 @@ def run_as_root(args):
parser_install.add_argument("-o", metavar="file", type=str, help="output file")

args = parser.parse_args()
read_conf()

if args.cmd is None:
read_conf()
run_as_service()
else:
run_as_root(args)

0 comments on commit 3ba0056

Please sign in to comment.