import git import json import os import datetime import argparse import warnings import __main__ def log_info(args, path=None): time = datetime.datetime.now().isoformat() if isinstance(args, argparse.Namespace): args = args.__dict__ elif isinstance(args, argparse.ArgumentParser): args = args.parse_args().__dict__ if path: if '/' in path: os.makedirs(path.rsplit('/', 1)[0], exist_ok=True) else: if not os.path.isdir('run_log'): os.makedirs('run_log') path = 'run_log/' + time + '.json' with open(path, 'w') as logfile: repo = git.Repo(os.path.dirname(os.path.abspath(__main__.__file__)), search_parent_directories=True) sha = repo.head.object.hexsha changes = repo.index.diff(None) if len(changes): warnings.simplefilter('always', DeprecationWarning) warnings.warn(f'{[c.a_path for c in changes]} have differences with current commit', DeprecationWarning, stacklevel=2) warnings.simplefilter('default', DeprecationWarning) json.dump({'script': __main__.__file__, 'commit': sha, 'parameters': args, 'time': time}, logfile)