From 87668c6ad08dc5a71a964777376106f7fa9d7c15 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Tue, 11 Jun 2019 20:52:45 -0600 Subject: [PATCH 1/6] Avoid rare crash when uploading results for review * Fixes issue #117 --- .bin/Scripts/functions/hw_diags.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.bin/Scripts/functions/hw_diags.py b/.bin/Scripts/functions/hw_diags.py index db5d01ca..ea19e90f 100644 --- a/.bin/Scripts/functions/hw_diags.py +++ b/.bin/Scripts/functions/hw_diags.py @@ -1141,7 +1141,9 @@ def run_hw_tests(state): show_results(state) # Upload for review - if ENABLED_UPLOAD_DATA and ask('Upload results for review?'): + if (ENABLED_UPLOAD_DATA + and DEBUG_MODE + and ask('Upload results for review?')): try_and_print( message='Saving debug reports...', function=save_debug_reports, From 441a6ad66fdc3e83f2f638478aaf7e4e79394278 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 12 Jun 2019 14:05:45 -0600 Subject: [PATCH 2/6] Prevent ddrescue-tui crash when LogDir is missing * Fixes issue #115 --- .bin/Scripts/functions/ddrescue.py | 1 + 1 file changed, 1 insertion(+) diff --git a/.bin/Scripts/functions/ddrescue.py b/.bin/Scripts/functions/ddrescue.py index 463adf4d..be306f2c 100644 --- a/.bin/Scripts/functions/ddrescue.py +++ b/.bin/Scripts/functions/ddrescue.py @@ -278,6 +278,7 @@ class RecoveryState(): raise GenericError('Unsupported mode') self.get_smart_source() self.set_working_dir() + os.makedirs(global_vars['LogDir'], exist_ok=True) def add_block_pair(self, source, dest): """Run safety checks and append new BlockPair to internal list.""" From 90f5285067af65204d0da321ba126c120aec0948 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 12 Jun 2019 14:58:27 -0600 Subject: [PATCH 3/6] Updated tmux menu launchers * Use current TMUX session if present * Can switch TMUX session without nesting * Fixes issue #114 --- .bin/Scripts/ddrescue-tui | 29 +++++++++++++++++++++++------ .bin/Scripts/hw-diags | 26 ++++++++++++++++++++------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/.bin/Scripts/ddrescue-tui b/.bin/Scripts/ddrescue-tui index e41c6bb1..650015af 100755 --- a/.bin/Scripts/ddrescue-tui +++ b/.bin/Scripts/ddrescue-tui @@ -8,10 +8,10 @@ MENU="ddrescue-tui-menu" function ask() { while :; do - read -p "$1 " -r answer - if echo "$answer" | egrep -iq '^(y|yes|sure)$'; then + read -p "$1 [Y/N] " -r answer + if echo "$answer" | grep -Eiq '^(y|yes|sure)$'; then return 0 - elif echo "$answer" | egrep -iq '^(n|no|nope)$'; then + elif echo "$answer" | grep -Eiq '^(n|no|nope)$'; then return 1 fi done @@ -26,7 +26,16 @@ die () { if tmux list-session | grep -q "$SESSION_NAME"; then echo "WARNING: tmux session $SESSION_NAME already exists." echo "" - if ask "Kill current session?"; then + if ask "Connect to current session?"; then + if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, switch to session + tmux switch-client -t "$SESSION_NAME" + else + # Running outside TMUX, attach to session + tmux attach-session -t "$SESSION_NAME" + fi + exit 0 + elif ask "Kill current session and start new session?"; then tmux kill-session -t "$SESSION_NAME" || \ die "Failed to kill session: $SESSION_NAME" else @@ -38,6 +47,14 @@ if tmux list-session | grep -q "$SESSION_NAME"; then fi fi -# Start session -tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" $* +# Start/Rename session +if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, rename session/window and open the menu + tmux rename-session "$SESSION_NAME" + tmux rename-window "$WINDOW_NAME" + "$MENU" "$@" +else + # Running outside TMUX, start/attach to session + tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" "$@" +fi diff --git a/.bin/Scripts/hw-diags b/.bin/Scripts/hw-diags index d3a1cb21..9de50f9c 100755 --- a/.bin/Scripts/hw-diags +++ b/.bin/Scripts/hw-diags @@ -9,9 +9,9 @@ MENU="hw-diags-menu" function ask() { while :; do read -p "$1 [Y/N] " -r answer - if echo "$answer" | egrep -iq '^(y|yes|sure)$'; then + if echo "$answer" | grep -Eiq '^(y|yes|sure)$'; then return 0 - elif echo "$answer" | egrep -iq '^(n|no|nope)$'; then + elif echo "$answer" | grep -Eiq '^(n|no|nope)$'; then return 1 fi done @@ -27,8 +27,14 @@ if tmux list-session | grep -q "$SESSION_NAME"; then echo "WARNING: tmux session $SESSION_NAME already exists." echo "" if ask "Connect to current session?"; then - # Do nothing, the command below will attach/connect - echo "" + if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, switch to session + tmux switch-client -t "$SESSION_NAME" + else + # Running outside TMUX, attach to session + tmux attach-session -t "$SESSION_NAME" + fi + exit 0 elif ask "Kill current session and start new session?"; then tmux kill-session -t "$SESSION_NAME" || \ die "Failed to kill session: $SESSION_NAME" @@ -41,6 +47,14 @@ if tmux list-session | grep -q "$SESSION_NAME"; then fi fi -# Start session -tmux new-session -A -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" $* +# Start/Rename session +if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, rename session/window and open the menu + tmux rename-session "$SESSION_NAME" + tmux rename-window "$WINDOW_NAME" + "$MENU" "$@" +else + # Running outside TMUX, start/attach to session + tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" "$@" +fi From e30e52e8807da4c3c6d3ec6ca95e2924a488da7a Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 12 Jun 2019 15:31:30 -0600 Subject: [PATCH 4/6] New launch-in-tmux script * Generic run cmd in tmux wrapper * ddrescue-tui and hw-diags source this script to run their respective menus * Reduces duplicate code --- .bin/Scripts/ddrescue-tui | 57 +++------------------------------ .bin/Scripts/hw-diags | 59 +++------------------------------- .bin/Scripts/launch-in-tmux | 64 +++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 107 deletions(-) create mode 100755 .bin/Scripts/launch-in-tmux diff --git a/.bin/Scripts/ddrescue-tui b/.bin/Scripts/ddrescue-tui index 650015af..6ee8ad57 100755 --- a/.bin/Scripts/ddrescue-tui +++ b/.bin/Scripts/ddrescue-tui @@ -2,59 +2,10 @@ # ## Wizard Kit: ddrescue TUI Launcher +source launch-in-tmux + SESSION_NAME="ddrescue-tui" WINDOW_NAME="ddrescue TUI" -MENU="ddrescue-tui-menu" - -function ask() { - while :; do - read -p "$1 [Y/N] " -r answer - if echo "$answer" | grep -Eiq '^(y|yes|sure)$'; then - return 0 - elif echo "$answer" | grep -Eiq '^(n|no|nope)$'; then - return 1 - fi - done -} - -die () { - echo "$0:" "$@" >&2 - exit 1 -} - -# Check for running session -if tmux list-session | grep -q "$SESSION_NAME"; then - echo "WARNING: tmux session $SESSION_NAME already exists." - echo "" - if ask "Connect to current session?"; then - if [[ -n "${TMUX:-}" ]]; then - # Running inside TMUX, switch to session - tmux switch-client -t "$SESSION_NAME" - else - # Running outside TMUX, attach to session - tmux attach-session -t "$SESSION_NAME" - fi - exit 0 - elif ask "Kill current session and start new session?"; then - tmux kill-session -t "$SESSION_NAME" || \ - die "Failed to kill session: $SESSION_NAME" - else - echo "Aborted." - echo "" - echo -n "Press Enter to exit... " - read -r - exit 0 - fi -fi - -# Start/Rename session -if [[ -n "${TMUX:-}" ]]; then - # Running inside TMUX, rename session/window and open the menu - tmux rename-session "$SESSION_NAME" - tmux rename-window "$WINDOW_NAME" - "$MENU" "$@" -else - # Running outside TMUX, start/attach to session - tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" "$@" -fi +TMUX_CMD="ddrescue-tui-menu" +launch_in_tmux "$@" diff --git a/.bin/Scripts/hw-diags b/.bin/Scripts/hw-diags index 9de50f9c..70f84db4 100755 --- a/.bin/Scripts/hw-diags +++ b/.bin/Scripts/hw-diags @@ -1,60 +1,11 @@ #!/bin/bash # -## Wizard Kit: HW Diagnostics - Menu Launcher +## Wizard Kit: HW Diagnostics Launcher + +source launch-in-tmux SESSION_NAME="hw-diags" WINDOW_NAME="Hardware Diagnostics" -MENU="hw-diags-menu" - -function ask() { - while :; do - read -p "$1 [Y/N] " -r answer - if echo "$answer" | grep -Eiq '^(y|yes|sure)$'; then - return 0 - elif echo "$answer" | grep -Eiq '^(n|no|nope)$'; then - return 1 - fi - done -} - -die () { - echo "$0:" "$@" >&2 - exit 1 -} - -# Check for running session -if tmux list-session | grep -q "$SESSION_NAME"; then - echo "WARNING: tmux session $SESSION_NAME already exists." - echo "" - if ask "Connect to current session?"; then - if [[ -n "${TMUX:-}" ]]; then - # Running inside TMUX, switch to session - tmux switch-client -t "$SESSION_NAME" - else - # Running outside TMUX, attach to session - tmux attach-session -t "$SESSION_NAME" - fi - exit 0 - elif ask "Kill current session and start new session?"; then - tmux kill-session -t "$SESSION_NAME" || \ - die "Failed to kill session: $SESSION_NAME" - else - echo "Aborted." - echo "" - echo -n "Press Enter to exit... " - read -r - exit 0 - fi -fi - -# Start/Rename session -if [[ -n "${TMUX:-}" ]]; then - # Running inside TMUX, rename session/window and open the menu - tmux rename-session "$SESSION_NAME" - tmux rename-window "$WINDOW_NAME" - "$MENU" "$@" -else - # Running outside TMUX, start/attach to session - tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$MENU" "$@" -fi +TMUX_CMD="hw-diags-menu" +launch_in_tmux "$@" diff --git a/.bin/Scripts/launch-in-tmux b/.bin/Scripts/launch-in-tmux new file mode 100755 index 00000000..e737b574 --- /dev/null +++ b/.bin/Scripts/launch-in-tmux @@ -0,0 +1,64 @@ +#!/bin/bash +# +## Wizard Kit: TMUX Launcher + +function ask() { + while :; do + read -p "$1 [Y/N] " -r answer + if echo "$answer" | grep -Eiq '^(y|yes|sure)$'; then + return 0 + elif echo "$answer" | grep -Eiq '^(n|no|nope)$'; then + return 1 + fi + done +} + +die () { + echo "$0:" "$@" >&2 + exit 1 +} + +function launch_in_tmux() { + # Check for required vars + [[ -n "${SESSION_NAME:-}" ]] || die "Required variable missing (SESSION_NAME)" + [[ -n "${WINDOW_NAME:-}" ]] || die "Required variable missing (WINDOW_NAME)" + [[ -n "${TMUX_CMD:-}" ]] || die "Required variable missing (TMUX_CMD)" + + # Check for running session + if tmux list-session | grep -q "$SESSION_NAME"; then + echo "WARNING: tmux session $SESSION_NAME already exists." + echo "" + if ask "Connect to current session?"; then + if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, switch to session + tmux switch-client -t "$SESSION_NAME" + else + # Running outside TMUX, attach to session + tmux attach-session -t "$SESSION_NAME" + fi + exit 0 + elif ask "Kill current session and start new session?"; then + tmux kill-session -t "$SESSION_NAME" || \ + die "Failed to kill session: $SESSION_NAME" + else + echo "Aborted." + echo "" + echo -n "Press Enter to exit... " + read -r + exit 0 + fi + fi + + # Start/Rename session + if [[ -n "${TMUX:-}" ]]; then + # Running inside TMUX, rename session/window and open the menu + tmux rename-session "$SESSION_NAME" + tmux rename-window "$WINDOW_NAME" + "$TMUX_CMD" "$@" + tmux rename-session "${SESSION_NAME}_DONE" + tmux rename-window "${WINDOW_NAME}_DONE" + else + # Running outside TMUX, start/attach to session + tmux new-session -s "$SESSION_NAME" -n "$WINDOW_NAME" "$TMUX_CMD" "$@" + fi +} From c50627867efcac6baa4225a8204a29ff1cc6174b Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 12 Jun 2019 15:37:19 -0600 Subject: [PATCH 5/6] Switch to prev tmux from hw-diags & ddrescue-tui * Only during normal exits, not done for aborts/crashes --- .bin/Scripts/ddrescue-tui-menu | 2 ++ .bin/Scripts/functions/tmux.py | 12 ++++++++++++ .bin/Scripts/hw-diags-menu | 2 ++ 3 files changed, 16 insertions(+) diff --git a/.bin/Scripts/ddrescue-tui-menu b/.bin/Scripts/ddrescue-tui-menu index f65e24e1..eab8cd3f 100755 --- a/.bin/Scripts/ddrescue-tui-menu +++ b/.bin/Scripts/ddrescue-tui-menu @@ -45,6 +45,7 @@ if __name__ == '__main__': # Done print_standard('\nDone.') pause("Press Enter to exit...") + tmux_switch_client() exit_script() except GenericAbort: abort() @@ -55,6 +56,7 @@ if __name__ == '__main__': print_error(msg) abort() except SystemExit as sys_exit: + tmux_switch_client() exit_script(sys_exit.code) except: major_exception() diff --git a/.bin/Scripts/functions/tmux.py b/.bin/Scripts/functions/tmux.py index 81522268..8c6ad327 100644 --- a/.bin/Scripts/functions/tmux.py +++ b/.bin/Scripts/functions/tmux.py @@ -141,6 +141,18 @@ def tmux_split_window( return result.stdout.decode().strip() +def tmux_switch_client(target_session=None): + """Switch to target tmux session, or previous if none specified.""" + cmd = ['tmux', 'switch-client'] + if target_session: + cmd.extend(['-t', target_session]) + else: + # Switch to previous instead + cmd.append('-p') + + run_program(cmd, check=False) + + def tmux_update_pane( pane_id, command=None, working_dir=None, text=None, watch=None, watch_cmd='cat'): diff --git a/.bin/Scripts/hw-diags-menu b/.bin/Scripts/hw-diags-menu index fc95e04a..7a122ae7 100755 --- a/.bin/Scripts/hw-diags-menu +++ b/.bin/Scripts/hw-diags-menu @@ -23,6 +23,7 @@ if __name__ == '__main__': sleep(1) pause('Press Enter to exit...') except SystemExit as sys_exit: + tmux_switch_client() exit_script(sys_exit.code) except: # Cleanup @@ -59,6 +60,7 @@ if __name__ == '__main__': # Done tmux_kill_all_panes() + tmux_switch_client() exit_script() # vim: sts=2 sw=2 ts=2 From 5b5c99e6f85fd27b683fd788a5e56905818eee09 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Wed, 12 Jun 2019 15:48:11 -0600 Subject: [PATCH 6/6] Don't kill current tmux session from ddrescue-tui * Just kill the panes and let launch-in-tmux handle the session --- .bin/Scripts/functions/ddrescue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.bin/Scripts/functions/ddrescue.py b/.bin/Scripts/functions/ddrescue.py index be306f2c..42d81b72 100644 --- a/.bin/Scripts/functions/ddrescue.py +++ b/.bin/Scripts/functions/ddrescue.py @@ -851,7 +851,7 @@ def menu_ddrescue(source_path, dest_path, run_mode): menu_main(state) # Done - run_program(['tmux', 'kill-window']) + tmux_kill_all_panes() exit_script()