"""Shared bits of the dprompt and dbanner command lines.""" import os import sys from .i18n import t def die(prog, uk, en, code=1): print(f"{prog}: {t(uk, en)}", file=sys.stderr) sys.exit(code) def need_root(argv): """Runs the command again through sudo with the same arguments.""" if os.geteuid() == 0: return script = os.path.abspath(sys.argv[0]) try: os.execvp("sudo", ["sudo", sys.executable, script] + argv) except OSError as e: print(f"sudo: {e}", file=sys.stderr) sys.exit(1) def resolve(spec): """A file argument as an absolute path (sudo starts elsewhere); names stay names.""" if os.sep in spec or spec.endswith(".json"): return os.path.abspath(spec) return spec def run_tui(prog, loader): try: app = loader() except ImportError as e: die(prog, f"немає бібліотеки Textual ({e}); встановіть python3-textual", f"the Textual library is missing ({e}); install python3-textual") app.run() return 0 def write_file(path, text, mode=0o644): os.makedirs(os.path.dirname(path), exist_ok=True) tmp = path + ".tmp" with open(tmp, "w", encoding="utf-8") as f: f.write(text) os.chmod(tmp, mode) os.replace(tmp, path)