orca/scripts/utils.py

33 lines
725 B
Python

import glob
import os
import subprocess
import traceback
from contextlib import contextmanager
@contextmanager
def pushd(new_dir):
previous_dir = os.getcwd()
os.chdir(new_dir)
try:
yield
finally:
os.chdir(previous_dir)
def removeall(dir):
[os.remove(f) for f in glob.iglob("{}/*".format(dir), recursive=True)]
os.removedirs(dir)
def shellish(func):
def shellfunc(*args, **kwargs):
try:
func(*args, **kwargs)
except subprocess.CalledProcessError as err:
print()
print(f"ERROR (code {err.returncode}): The following command failed:")
print(" ".join(err.cmd))
exit(err.returncode)
return shellfunc