Skip to content

Commit f4e5132

Browse files
committed
rework exceptions for pylint
1 parent 211111e commit f4e5132

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed

cime_config/buildlib

+48-24
Original file line numberDiff line numberDiff line change
@@ -283,14 +283,28 @@ def _build_musica(clone_dest: str) -> str:
283283
"-D MUSICA_BUILD_FORTRAN_INTERFACE=ON",
284284
".."
285285
]
286-
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False)
287-
if result.returncode != 0:
288-
raise Exception(f"Unable to configure the MUSICA CMake project. Error: {result.stderr}")
286+
try:
287+
subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
288+
text=True, check=False)
289+
except subprocess.CalledProcessError as e:
290+
raise subprocess.CalledProcessError(f"The subprocess for cmake \
291+
to configure the MUSICA CMake project failed.") from e
292+
except FileNotFoundError as e:
293+
raise FileNotFoundError(f"The 'cmake' command was not found.") from e
294+
except OSError as e:
295+
raise OSError(f"An error occurred while executing the 'cmake' command.") from e
289296

290297
command = ["cmake", "--build", ".", "--target", "install"]
291-
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False)
292-
if result.returncode != 0:
293-
raise Exception(f"Unable to build the MUSICA library. Error: {result.stderr}")
298+
try:
299+
subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
300+
text=True, check=False)
301+
except subprocess.CalledProcessError as e:
302+
raise subprocess.CalledProcessError(f"The subprocess for cmake \
303+
to build the MUSICA library failed.") from e
304+
except FileNotFoundError as e:
305+
raise FileNotFoundError(f"The 'cmake' command was not found.") from e
306+
except OSError as e:
307+
raise OSError(f"An error occurred while executing the 'cmake' command.") from e
294308

295309
os.chdir(current_dir)
296310
musica_install_path = os.path.join(bld_path, install_dir)
@@ -321,13 +335,13 @@ def _download_musica_configuration(download_dest: str) -> None:
321335
try:
322336
os.rename(original_dir, renamed_dir)
323337
except FileNotFoundError as e:
324-
raise FileNotFoundError(f"The directory '{original_dir}' was not found. Error: {e}")
338+
raise FileNotFoundError(f"The directory '{original_dir}' was not found.") from e
325339
except FileExistsError as e:
326-
raise FileExistsError(f"The destination directory '{renamed_dir}' already exists. Error: {e}")
340+
raise FileExistsError(f"The destination directory '{renamed_dir}' already exists.") from e
327341
except PermissionError as e:
328-
raise PermissionError(f"Permission denied to rename '{original_dir}'. Error: {e}")
342+
raise PermissionError(f"Permission denied to rename '{original_dir}'.") from e
329343
except OSError as e:
330-
raise OSError(f"An error occurred while renaming: {e}")
344+
raise OSError(f"An error occurred while renaming.") from e
331345

332346
musica_config_path = os.path.join(download_dest, musica_config_dir_name)
333347
if os.path.exists(musica_config_path):
@@ -366,11 +380,11 @@ def _set_musica_lib_path(musica_install_path: str, caseroot: str) -> None:
366380
text=True, check=False)
367381
except subprocess.CalledProcessError as e:
368382
raise subprocess.CalledProcessError(f"The subprocess for xmlchange \
369-
to set the MUSICA library path failed with an error.: {e}")
383+
to set the MUSICA library path failed.") from e
370384
except FileNotFoundError as e:
371-
raise FileNotFoundError(f"The 'xmlchange' command was not found: {e}")
385+
raise FileNotFoundError(f"The 'xmlchange' command was not found.") from e
372386
except OSError as e:
373-
raise OSError(f"An error occurred while running 'xmlchange' command: {e}")
387+
raise OSError(f"An error occurred while executing the 'xmlchange' command.") from e
374388

375389
os.chdir(current_dir)
376390

@@ -395,17 +409,27 @@ def _clone_and_checkout(repo_url: str, tag_name: str, clone_dest: str) -> None:
395409
if os.path.exists(repo_path):
396410
shutil.rmtree(repo_path)
397411

398-
result = subprocess.run(["git", "clone", repo_url, repo_path],
399-
stderr=subprocess.PIPE, text=True, check=False)
400-
if result.returncode != 0:
401-
raise Exception(f"Unable to clone the repository: {repo_url}. \
402-
Error: {result.stderr}")
403-
404-
result = subprocess.run(["git", "-C", repo_path, "checkout", tag_name],
405-
stderr=subprocess.PIPE, text=True, check=False)
406-
if result.returncode != 0:
407-
raise Exception(f"Unable to checkout the branch: {tag_name}. \
408-
Error: {result.stderr}")
412+
try:
413+
subprocess.run(["git", "clone", repo_url, repo_path],
414+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False)
415+
except subprocess.CalledProcessError as e:
416+
raise subprocess.CalledProcessError(f"The subprocess for git \
417+
to clone the repository {repo_url} failed.") from e
418+
except FileNotFoundError as e:
419+
raise FileNotFoundError(f"The 'git' command was not found.") from e
420+
except OSError as e:
421+
raise OSError(f"An error occurred while executing the 'git' command.") from e
422+
423+
try:
424+
subprocess.run(["git", "-C", repo_path, "checkout", tag_name],
425+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False)
426+
except subprocess.CalledProcessError as e:
427+
raise subprocess.CalledProcessError(f"The subprocess for git \
428+
to checkout the branch {tag_name} failed.") from e
429+
except FileNotFoundError as e:
430+
raise FileNotFoundError(f"The 'git' command was not found.") from e
431+
except OSError as e:
432+
raise OSError(f"An error occurred while executing the 'git' command.") from e
409433

410434
###############################################################################
411435

0 commit comments

Comments
 (0)