From 781d2f6e39a5c06b4eb3e64c99b13c8ccdc2116d Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Tue, 15 Mar 2022 21:22:53 -0400 Subject: [PATCH] Add "--overwrite" option when calling "sbase mkrec FILE" --- seleniumbase/console_scripts/ReadMe.md | 1 + seleniumbase/console_scripts/run.py | 2 ++ seleniumbase/console_scripts/sb_mkrec.py | 30 ++++++++++++++++-------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/seleniumbase/console_scripts/ReadMe.md b/seleniumbase/console_scripts/ReadMe.md index 6a17322b..53a83e4a 100755 --- a/seleniumbase/console_scripts/ReadMe.md +++ b/seleniumbase/console_scripts/ReadMe.md @@ -240,6 +240,7 @@ is included. ``--url=URL`` (Sets the initial start page URL.) ``--edge`` (Use Edge browser instead of Chrome.) ``--gui`` / ``--headed`` (Use headed mode on Linux.) +``--overwrite`` (Overwrite file when it exists.) * Output: Creates a new SeleniumBase test using the Recorder. diff --git a/seleniumbase/console_scripts/run.py b/seleniumbase/console_scripts/run.py index c34993fe..3cf268f3 100644 --- a/seleniumbase/console_scripts/run.py +++ b/seleniumbase/console_scripts/run.py @@ -217,6 +217,7 @@ def show_mkrec_usage(): print(" --url=URL (Sets the initial start page URL.)") print(" --edge (Use Edge browser instead of Chrome.)") print(" --gui / --headed (Use headed mode on Linux.)") + print(" --overwrite (Overwrite file when it exists.)") print(" Output:") print(" Creates a new SeleniumBase test using the Recorder.") print(" If the filename already exists, an error is raised.") @@ -240,6 +241,7 @@ def show_codegen_usage(): print(" --url=URL (Sets the initial start page URL.)") print(" --edge (Use Edge browser instead of Chrome.)") print(" --gui / --headed (Use headed mode on Linux.)") + print(" --overwrite (Overwrite file when it exists.)") print(" Output:") print(" Creates a new SeleniumBase test using the Recorder.") print(" If the filename already exists, an error is raised.") diff --git a/seleniumbase/console_scripts/sb_mkrec.py b/seleniumbase/console_scripts/sb_mkrec.py index 7df82bfd..248e49a5 100755 --- a/seleniumbase/console_scripts/sb_mkrec.py +++ b/seleniumbase/console_scripts/sb_mkrec.py @@ -22,6 +22,7 @@ Options: --url=URL (Sets the initial start page URL.) --edge (Use Edge browser instead of Chrome.) --gui / --headed (Use headed mode on Linux.) + --overwrite (Overwrite file when it exists.) Output: Creates a new SeleniumBase test using the Recorder. @@ -36,19 +37,18 @@ import sys def invalid_run_command(msg=None): - exp = " ** mkrec / codegen **\n\n" + exp = " ** mkrec / record / codegen **\n\n" exp += " Usage:\n" exp += " seleniumbase mkrec [FILE.py]\n" - exp += " sbase mkrec [FILE.py]\n" - exp += " seleniumbase codegen [FILE.py]\n" - exp += " sbase codegen [FILE.py]\n" + exp += " OR: sbase mkrec [FILE.py]\n" exp += " Examples:\n" exp += " sbase mkrec new_test.py\n" - exp += " sbase codegen new_test.py\n" + exp += " sbase mkrec new_test.py --url=wikipedia.org\n" exp += " Options:\n" exp += " --url=URL (Sets the initial start page URL.)\n" exp += " --edge (Use Edge browser instead of Chrome.)\n" exp += " --gui / --headed (Use headed mode on Linux.)\n" + exp += " --overwrite (Overwrite file when it exists.)\n" exp += " Output:\n" exp += " Creates a new SeleniumBase test using the Recorder.\n" exp += " If the filename already exists, an error is raised.\n" @@ -108,12 +108,23 @@ def main(): error_msg = "File must be created in the current directory!" elif file_name == "abc.py": error_msg = '"abc.py" is a reserved Python module! Use another name!' - elif os.path.exists(os.getcwd() + "/" + file_name): - error_msg = 'File "%s" already exists in this directory!' % file_name if error_msg: error_msg = c5 + "ERROR: " + error_msg + cr invalid_run_command(error_msg) + dir_name = os.getcwd() + file_path = os.path.join(dir_name, file_name) + + if ( + "--overwrite" in ' '.join(command_args).lower() + and os.path.exists(file_path) + ): + os.remove(file_path) + if os.path.exists(file_path): + error_msg = 'File "%s" already exists in this directory!' % file_name + error_msg = c5 + "ERROR: " + error_msg + cr + invalid_run_command(error_msg) + if len(command_args) >= 2: options = command_args[1:] for option in options: @@ -133,6 +144,8 @@ def main(): elif next_is_url: start_page = option next_is_url = False + elif option.lower() == "--overwrite": + pass # Already handled if file existed else: invalid_cmd = "\n===> INVALID OPTION: >> %s <<\n" % option invalid_cmd = invalid_cmd.replace(">> ", ">>" + c5 + " ") @@ -144,9 +157,6 @@ def main(): if help_me: invalid_run_command(invalid_cmd) - dir_name = os.getcwd() - file_path = "%s/%s" % (dir_name, file_name) - data = [] data.append("from seleniumbase import BaseCase") data.append("")