From 974c2df0d7dabb4a7c2981954206dea8dd97f992 Mon Sep 17 00:00:00 2001 From: Maurizio Lombardi Date: Mon, 29 May 2023 14:28:34 +0200 Subject: [PATCH] Revert "fix setting preference rollback error" This reverts commit 963e4e9d5fbbda2f6bd4865e8f2e71d8253d8879. --- scripts/targetcli | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/scripts/targetcli b/scripts/targetcli index 4ac469d..89a8188 100755 --- a/scripts/targetcli +++ b/scripts/targetcli @@ -23,7 +23,7 @@ from __future__ import print_function from os import getuid, getenv from targetcli import UIRoot from rtslib_fb import RTSLibError -from configshell_fb import Console, ConfigShell, ExecutionError +from configshell_fb import ConfigShell, ExecutionError from targetcli import __version__ as targetcli_version import sys @@ -88,27 +88,30 @@ def usage_version(cmd): if cmd in ("version", "--version", "-v"): version() -def try_op_lock(console, lkfd): +def try_op_lock(shell, lkfd): ''' acquire a blocking lock on lockfile, to serialize multiple requests ''' try: fcntl.flock(lkfd, fcntl.LOCK_EX) # wait here until ongoing request is finished except Exception as e: - text = "taking lock on lockfile failed: %s" %str(e) - console.display(console.render_text(text, 'red')) + shell.con.display( + shell.con.render_text( + "taking lock on lockfile failed: %s" %str(e), + 'red')) sys.exit(1) -def release_op_lock(console, lkfd): +def release_op_lock(shell, lkfd): ''' release blocking lock on lockfile, which can allow other requests process ''' try: fcntl.flock(lkfd, fcntl.LOCK_UN) # allow other requests now except Exception as e: - text = "unlock on lockfile failed: %s" %str(e) - console.display(console.render_text(text, 'red')) - lkfd.close() + shell.con.display( + shell.con.render_text( + "unlock on lockfile failed: %s" %str(e), + 'red')) sys.exit(1) lkfd.close() @@ -248,23 +251,21 @@ def main(): ''' Start the targetcli shell. ''' - console = Console() + shell = TargetCLI(getenv("TARGETCLI_HOME", '~/.targetcli')) is_root = False if getuid() == 0: is_root = True try: - lkfd = open(lock_file, 'w+') + lkfd = open(lock_file, 'w+'); except IOError as e: - text = "opening lockfile failed: %s" %str(e) - console.display(console.render_text(text, 'red')) + shell.con.display( + shell.con.render_text("opening lockfile failed: %s" %str(e), + 'red')) sys.exit(1) - try_op_lock(console, lkfd) - - targetcli_dir = getenv("TARGETCLI_HOME", '~/.targetcli') - shell = TargetCLI(targetcli_dir, console) + try_op_lock(shell, lkfd) use_daemon = False if shell.prefs['auto_use_daemon']: @@ -291,7 +292,6 @@ def main(): shell.con.display(shell.con.render_text(str(error), 'red')) if not is_root: shell.con.display(shell.con.render_text("Retry as root.", 'red')) - release_op_lock(console, lkfd) sys.exit(-1) if len(sys.argv) > 1: @@ -302,9 +302,7 @@ def main(): shell.run_cmdline(" ".join(sys.argv[1:])) except Exception as e: print(str(e), file=sys.stderr) - release_op_lock(console, lkfd) sys.exit(1) - release_op_lock(console, lkfd) sys.exit(0) shell.con.display("targetcli shell version %s\n" @@ -324,7 +322,7 @@ def main(): shell.log.info("Global pref auto_save_on_exit=true") root_node.ui_command_saveconfig() - release_op_lock(console, lkfd) + release_op_lock(shell, lkfd) if __name__ == "__main__":