diff --git a/dev/gen-archive.py b/dev/gen-archive.py index c9d543e8..dc2d8257 100755 --- a/dev/gen-archive.py +++ b/dev/gen-archive.py @@ -16,54 +16,16 @@ ############################################################ import os -import sys import shutil -import tempfile -import traceback import subprocess -from contextlib import contextmanager from termcolor import cprint +from common import print_exception, run_shell, jump_in_dir, jump_in_tmpdir ############################################################ #extract version PACKAGE_NAME="malt" PACKAGE_VERSION="1.2.3" -############################################################ -# Run command after logging it and intercept error -def run_shell(cmd: str) -> None: - cprint(f" + {cmd}", "light_blue") - subprocess.run(cmd, shell=True, check=True) - -############################################################ -@contextmanager -def jump_in_dir(path: str): - ''' - An helper semantic to jump temporarily in a directory. As wrapped with python 'with' semantic, - went back automatically on the previous one on error or success. - ''' - # get old path - oldpwd = os.getcwd() - - # chump in - os.chdir(path) - - # handle return back afterward - try: - yield - finally: - os.chdir(oldpwd) - -############################################################ -@contextmanager -def jump_in_tmpdir(): - tmpdir = tempfile.mkdtemp() - try: - with jump_in_dir(tmpdir): - yield tmpdir - finally: - shutil.rmtree(tmpdir) - ############################################################ # Generate MALT archive def malt_dev_gen_archive(name: str = PACKAGE_NAME, version: str = PACKAGE_VERSION) -> None: @@ -98,19 +60,6 @@ def malt_dev_gen_archive(name: str = PACKAGE_NAME, version: str = PACKAGE_VERSIO print("") cprint("Done", "green") -########################################################## -def print_exception(exception: Exception) -> None: - dir = os.getcwd() - cprint("-----------------------------------------------", 'red') - cprint(''.join(traceback.format_exception(exception)), 'dark_grey') - cprint("-----------------------------------------------", 'red') - cprint(f"Error from command : {' '.join(sys.argv)}", 'red') - cprint(f"Error from workdir : {dir}", 'red') - cprint("-----------------------------------------------", 'red') - cprint(str(exception), 'red') - cprint("-----------------------------------------------", 'red') - print("") - ############################################################ if __name__ == "__main__": try: