Skip to content

Commit

Permalink
[dev] Moveout some functions into dev/common.py
Browse files Browse the repository at this point in the history
  • Loading branch information
svalat committed Oct 2, 2024
1 parent 30575f2 commit b6faf61
Showing 1 changed file with 1 addition and 52 deletions.
53 changes: 1 addition & 52 deletions dev/gen-archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b6faf61

Please sign in to comment.