What my function does:
- creates a war file by processing the folder contents (/tmp/A, /tmp/B and so on)
- Does some file path and folder path manipulations to get the final version from the war file.
- store the file name in one variable and the version in another.
- Push the war file to the Repository using curl.
I'm using multiple try & except blocks to catch the exception for each action and looks very un-pythonic.
Is there an elegant and simple way to approach this ? thanks in advance.
import shutil import traceback import subprocess import os import glob def my_function(path_a, path_b, tmp_dir) try: <shutil.copy to the tmp dir> except: traceback.print_exc() try: war_process = subprocess.run([WAR GENERATION COMMAND], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(war_process.stdout.decode("utf-8")) except subprocess.CalledProcessError as e: exit_code = e.returncode stderror = e.stderr print(exit_code, stderror) print(war_process.stderr.decode("utf-8")) try: output_folder = os.path.join("/tmp/dir/work", FILE_PATH, ARTIFACT_DATE, FILE_WO_EXTENSION) except: traceback.print_exc() try: file_name = list(glob.glob(os.path.join(output_folder, "*.war"))) except: traceback.print_exc() try: file_path = os.path.join(output_folder, file_name) except: traceback.print_exc() try: os.rename(file_path, file_path.split('war')[0] + ".tgz") except: traceback.print_exc() try: file_version = os.path.basename(file_path) except: traceback.print_exc() cmd = "curl -u username -T ....)" try: curl_output = subprocess.run([cmd], shell=True, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print(curl_output.stdout.decode("utf-8")) except subprocess.CalledProcessError as er: print(proc_c.stderr.decode("utf-8")) exit_c = er.returncode std = er.stderr print(exit_c, std)