Skip to content

Commit 7da2ec6

Browse files
Adding force overwrite command line flag.
1 parent 1d1c817 commit 7da2ec6

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/sorcha/utilities/sorcha_copy_configs.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from sorcha.modules.PPConfigParser import PPFindDirectoryOrExit
88

99

10-
def copy_demo_configs(copy_location, which_configs):
10+
def copy_demo_configs(copy_location, which_configs, force_overwrite):
1111
"""
1212
Copies the example Sorcha configuration files to a user-specified location.
1313
@@ -19,6 +19,9 @@ def copy_demo_configs(copy_location, which_configs):
1919
which_configs : string
2020
String indicating which configuration files to retrieve. Should be "rubin", "demo" or "all".
2121
22+
force_overwrite: boolean
23+
Flag for determining whether existing files should be overwritten.
24+
2225
Returns
2326
-----------
2427
None
@@ -49,6 +52,9 @@ def copy_demo_configs(copy_location, which_configs):
4952
)
5053

5154
for config in config_locations:
55+
if not force_overwrite and os.path.isfile(config):
56+
sys.exit("Identical file exists at location. Re-run with -f or --force to force overwrite.")
57+
5258
shutil.copy(config, copy_location)
5359

5460
print("Example configuration files {} copied to {}.".format(config_locations, copy_location))
@@ -91,10 +97,10 @@ def main():
9197
to a user-specified location. Filepath to copy files to is specified by command-line
9298
flag. Selection of configuration files is done via user input.
9399
94-
usage: sorcha_copy_configs [-h] [-f FILEPATH]
100+
usage: sorcha_copy_configs [-h] [-p PATH]
95101
arguments:
96102
-h, --help Show this help message and exit.
97-
[-f FILEPATH, --filename FILEPATH] Filepath where you want to copy the config files. Default is current working directory.
103+
[-p PATH, --path PATH] Filepath where you want to copy the config files. Default is current working directory.
98104
99105
Parameters
100106
-----------
@@ -111,13 +117,21 @@ def main():
111117
)
112118

113119
parser.add_argument(
114-
"-f",
115-
"--filepath",
120+
"-p",
121+
"--path",
116122
help="Filepath where you want to copy the config files. Default is current working directory.",
117123
type=str,
118124
default="./",
119125
)
120126

127+
parser.add_argument(
128+
"-f",
129+
"--force",
130+
help="Force deletion/overwrite of existing config file(s). Default False.",
131+
action="store_true",
132+
default=False,
133+
)
134+
121135
args = parser.parse_args()
122136

123137
print("\nWhich configuration file(s) would you like to copy?:\n")
@@ -128,8 +142,8 @@ def main():
128142

129143
which_configs = parse_file_selection(file_select)
130144

131-
copy_location = os.path.abspath(args.filepath)
132-
copy_demo_configs(copy_location, which_configs)
145+
copy_location = os.path.abspath(args.path)
146+
copy_demo_configs(copy_location, which_configs, args.force)
133147

134148

135149
if __name__ == "__main__":

tests/sorcha/test_sorcha_copy_configs.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ def test_sorcha_copy_configs(tmp_path):
66
from sorcha.utilities.sorcha_copy_configs import copy_demo_configs
77

88
# test that the Rubin files are successfully copied
9-
copy_demo_configs(tmp_path, "rubin_circle")
9+
copy_demo_configs(tmp_path, "rubin_circle", True)
1010

1111
assert os.path.isfile(os.path.join(tmp_path, "Rubin_circular_approximation.ini"))
1212

13-
copy_demo_configs(tmp_path, "rubin_footprint")
13+
copy_demo_configs(tmp_path, "rubin_footprint", True)
1414

1515
assert os.path.isfile(os.path.join(tmp_path, "Rubin_full_footprint.ini"))
1616

@@ -19,29 +19,36 @@ def test_sorcha_copy_configs(tmp_path):
1919
os.remove(os.path.join(tmp_path, "Rubin_full_footprint.ini"))
2020

2121
# test that all the configs are successfully copied
22-
copy_demo_configs(tmp_path, "all")
22+
copy_demo_configs(tmp_path, "all", True)
2323

2424
assert os.path.isfile(os.path.join(tmp_path, "Rubin_circular_approximation.ini"))
2525
assert os.path.isfile(os.path.join(tmp_path, "Rubin_full_footprint.ini"))
2626

2727
# test the error message if user supplies non-existent directory
2828
dummy_folder = os.path.join(tmp_path, "dummy_folder")
2929
with pytest.raises(SystemExit) as e:
30-
copy_demo_configs(dummy_folder, "all")
30+
copy_demo_configs(dummy_folder, "all", True)
3131

3232
assert e.value.code == "ERROR: filepath {} supplied for filepath argument does not exist.".format(
3333
dummy_folder
3434
)
3535

3636
# test the error message if user supplies unrecognised keyword for which_configs variable
3737
with pytest.raises(SystemExit) as e2:
38-
copy_demo_configs(tmp_path, "laphroaig")
38+
copy_demo_configs(tmp_path, "laphroaig", True)
3939

4040
assert (
4141
e2.value.code
4242
== "String 'laphroaig' not recognised for 'configs' variable. Must be 'rubin_circle', 'rubin_footprint' or 'all'."
4343
)
4444

45+
# test tthe error message if file exists and overwrite isn't forced
46+
47+
with pytest.raises(SystemExit) as e3:
48+
copy_demo_configs(tmp_path, "rubin_footprint", False)
49+
50+
assert e3.value.code == "Identical file exists at location. Re-run with -f or --force to force overwrite."
51+
4552

4653
def test_parse_file_selection():
4754
from sorcha.utilities.sorcha_copy_configs import parse_file_selection

0 commit comments

Comments
 (0)