Skip to content

Commit 06b4a93

Browse files
committed
Replace direct raise with utils function.
1 parent ebcd89d commit 06b4a93

File tree

1 file changed

+39
-27
lines changed

1 file changed

+39
-27
lines changed

datashuttle/utils/rclone_encryption.py

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ def set_password_windows(cfg: Configs) -> None:
4646
password_filepath.unlink()
4747

4848
shell = shutil.which("powershell")
49-
if not shell:
50-
raise RuntimeError(
51-
"powershell.exe not found in PATH (need Windows PowerShell 5.1)."
49+
if shell is None:
50+
utils.log_and_raise_error(
51+
"powershell.exe not found in PATH (need Windows PowerShell 5.1).",
52+
RuntimeError,
5253
)
5354

5455
ps_cmd = (
@@ -59,15 +60,16 @@ def set_password_windows(cfg: Configs) -> None:
5960
)
6061

6162
output = subprocess.run(
62-
[shell, "-NoProfile", "-Command", ps_cmd],
63+
[shell, "-NoProfile", "-Command", ps_cmd], # type: ignore
6364
capture_output=True,
6465
text=True,
6566
)
6667
if output.returncode != 0:
67-
raise RuntimeError(
68-
f"\n--- STDOUT ---\n{output.stdout}",
69-
f"\n--- STDERR ---\n{output.stderr}",
68+
utils.log_and_raise_error(
69+
f"\n--- STDOUT ---\n{output.stdout}"
70+
f"\n--- STDERR ---\n{output.stderr}"
7071
"Could not set the PSCredential with System.web. See the error message above.",
72+
RuntimeError,
7173
)
7274

7375

@@ -88,8 +90,9 @@ def set_password_linux(cfg: Configs) -> None:
8890
text=True,
8991
)
9092
if output.returncode != 0:
91-
raise RuntimeError(
92-
"`pass` is required to set password. Install e.g. sudo apt install pass."
93+
utils.log_and_raise_error(
94+
"`pass` is required to set password. Install e.g. sudo apt install pass.",
95+
RuntimeError,
9396
)
9497

9598
output = subprocess.run(
@@ -100,15 +103,17 @@ def set_password_linux(cfg: Configs) -> None:
100103
)
101104
if output.returncode != 0:
102105
if "pass init" in output.stderr:
103-
raise RuntimeError(
106+
utils.log_and_raise_error(
104107
"Password store is not initialized. "
105-
"Run `pass init <gpg-id>` before using `pass`."
108+
"Run `pass init <gpg-id>` before using `pass`.",
109+
RuntimeError,
106110
)
107111
else:
108-
raise RuntimeError(
112+
utils.log_and_raise_error(
109113
f"\n--- STDOUT ---\n{output.stdout}"
110114
f"\n--- STDERR ---\n{output.stderr}"
111115
"Could not set up password with `pass`. See the error message above.",
116+
RuntimeError,
112117
)
113118

114119
output = subprocess.run(
@@ -118,10 +123,11 @@ def set_password_linux(cfg: Configs) -> None:
118123
text=True,
119124
)
120125
if output.returncode != 0:
121-
raise RuntimeError(
122-
f"\n--- STDOUT ---\n{output.stdout}",
123-
f"\n--- STDERR ---\n{output.stderr}",
126+
utils.log_and_raise_error(
127+
f"\n--- STDOUT ---\n{output.stdout}"
128+
f"\n--- STDERR ---\n{output.stderr}"
124129
"Could not remove the password from the RClone config. See the error message above.",
130+
RuntimeError,
125131
)
126132

127133

@@ -142,10 +148,11 @@ def set_password_macos(cfg: Configs) -> None:
142148
)
143149

144150
if output.returncode != 0:
145-
raise RuntimeError(
146-
f"\n--- STDOUT ---\n{output.stdout}",
147-
f"\n--- STDERR ---\n{output.stderr}",
148-
"Could not remove the password from the RClone config. See the error message above.",
151+
utils.log_and_raise_error(
152+
f"\n--- STDOUT ---\n{output.stdout}"
153+
f"\n--- STDERR ---\n{output.stderr}"
154+
"Could not encrypt the RClone config. See the error message above.",
155+
RuntimeError,
149156
)
150157

151158

@@ -172,7 +179,9 @@ def set_credentials_as_password_command(cfg: Configs) -> None:
172179

173180
shell = shutil.which("powershell")
174181
if not shell:
175-
raise RuntimeError("powershell.exe not found in PATH")
182+
utils.log_and_raise_error(
183+
"powershell.exe not found in PATH", RuntimeError
184+
)
176185

177186
# Escape single quotes inside PowerShell string by doubling them
178187
cmd = (
@@ -215,9 +224,10 @@ def run_rclone_config_encrypt(cfg: Configs) -> None:
215224
if not rclone_config_path.exists():
216225
connection_method = cfg["connection_method"]
217226

218-
raise RuntimeError(
227+
utils.log_and_raise_error(
219228
f"Rclone config file for: {connection_method} was not found. "
220-
f"Make sure you set up the connection first with `setup_{connection_method}_connection()`"
229+
f"Make sure you set up the connection first with `setup_{connection_method}_connection()`",
230+
RuntimeError,
221231
)
222232

223233
save_credentials_password(cfg)
@@ -231,10 +241,11 @@ def run_rclone_config_encrypt(cfg: Configs) -> None:
231241
text=True,
232242
)
233243
if output.returncode != 0:
234-
raise RuntimeError(
244+
utils.log_and_raise_error(
235245
f"\n--- STDOUT ---\n{output.stdout}\n"
236246
f"\n--- STDERR ---\n{output.stderr}\n"
237-
"Could not encrypt the RClone config. See the error message above."
247+
"Could not encrypt the RClone config. See the error message above.",
248+
RuntimeError,
238249
)
239250

240251
remove_credentials_as_password_command()
@@ -260,10 +271,11 @@ def remove_rclone_encryption(cfg: Configs) -> None:
260271
text=True,
261272
)
262273
if output.returncode != 0:
263-
raise RuntimeError(
264-
f"\n--- STDOUT ---\n{output.stdout}",
265-
f"\n--- STDERR ---\n{output.stderr}",
274+
utils.log_and_raise_error(
275+
f"\n--- STDOUT ---\n{output.stdout}"
276+
f"\n--- STDERR ---\n{output.stderr}"
266277
"Could not remove the password from the RClone config. See the error message above.",
278+
RuntimeError,
267279
)
268280

269281
remove_credentials_as_password_command()

0 commit comments

Comments
 (0)