diff --git a/README.md b/README.md index d204568a75e29e96cda55dfd6c4068b59cfa0567..2ab74628f60ce2318984d65b089c692fcbd8a1b9 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,8 @@ This will create a json file in `run_log/` by default, with the current time as The json file created will be: ```json {"script": "/home/xxx/Documents/Python/auto_git_info/test_auto_git.py", - "commit": "7ce86099e62e7ffdfe77fbbcafbe48b862c269b9", + "commit": "7ce86099e62e7ffdfe77fbbcafbe48b862c269b9", + "branch": "master", "parameters": {"input": "rnd_input", "output": null}, "time": "2023-06-23T18:04:30.894271"} @@ -43,7 +44,9 @@ Advance usage #### args Instead of passing the `Namespace` object from the `ArgumentParser` parser, you can also directly pass the parser itself. Alternatively, you can pass a dictionary or any serializable object. -You can also use `None` if you don't have any arguments. +You can also use `None` if you don't have any arguments. + +Additional kwargs can be passed. The kwargs names will be the keys in the json, and their values will be the associated values. #### path diff --git a/setup.py b/setup.py index 542244b63af66979a791b23d243963283a6988da..1d930f09a317b922e46d8984ae215de6a02a715f 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ from setuptools import setup, find_packages setup(name='auto_git_info', - version='1.0', + version='1.0.1', description='Automatically log your command line arguments and commit sha', author='Maxence Ferrari', py_modules=['auto_git_info'], diff --git a/src/__init__.py b/src/__init__.py index 9bc16447f1d06e818119accdd0e29954037bf60a..5c4105cd39cc4181c773f21fc007b4d120968c8b 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -1 +1 @@ -__version__ = 1.0 \ No newline at end of file +__version__ = "1.0.1" diff --git a/src/auto_git_info.py b/src/auto_git_info.py index 8328725b130390a8e9a8a0d225d499557f2a62c1..fec1ec14c8f12b0bbf9254b8115ece1fc44c5b6f 100644 --- a/src/auto_git_info.py +++ b/src/auto_git_info.py @@ -7,13 +7,14 @@ import warnings import __main__ -def log_info(args, *, path=None): +def log_info(args, *, path=None, **kwargs): """ Create a json file with script, commit, parameters, and time keys. The commit is the commit sha and script is the main script executed args: An argparse object, None or any serializable objet path: Path to the json file. default to 'run_log/current_time.json' + **kwargs: Add serializable objet to the json with its kwargs as a key """ time = datetime.datetime.now().isoformat() @@ -31,13 +32,17 @@ def log_info(args, *, path=None): 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 + branch = repo.head.ref.name 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) + out = {'script': __main__.__file__, + 'commit': sha, + 'branch': branch, + 'parameters': args, + 'time': time} + out.update({k: v for k, v in kwargs.items() if k not in out}) + json.dump(out, logfile)