From 3a0f4058fa4f6e1f1f2745cfcd7c8b92ab9f93e6 Mon Sep 17 00:00:00 2001
From: ferrari <maxence.ferrari@gmail.com>
Date: Mon, 26 Jun 2023 15:46:55 +0200
Subject: [PATCH] Add module mode

---
 setup.py             |  2 +-
 src/__init__.py      |  2 +-
 src/auto_git_info.py | 38 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 2 deletions(-)

diff --git a/setup.py b/setup.py
index 1d930f0..af34bc6 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.1',
+      version='1.1.0',
       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 5c4105c..6849410 100644
--- a/src/__init__.py
+++ b/src/__init__.py
@@ -1 +1 @@
-__version__ = "1.0.1"
+__version__ = "1.1.0"
diff --git a/src/auto_git_info.py b/src/auto_git_info.py
index fec1ec1..60de9c8 100644
--- a/src/auto_git_info.py
+++ b/src/auto_git_info.py
@@ -46,3 +46,41 @@ def log_info(args, *, path=None, **kwargs):
                'time': time}
         out.update({k: v for k, v in kwargs.items() if k not in out})
         json.dump(out, logfile)
+
+
+if __name__ == '__main__':
+    import sys
+    import subprocess
+    import getopt  # https://github.com/python/cpython/blob/main/Lib/pdb.py#L1950
+
+    opts, args = getopt.getopt(sys.argv[1:], 'h:', ['help', ])
+
+    _usage = """\
+    usage: auto_git_info.py [log_path=path.json] pyfile [arg] ...
+    automatically log the info of pyfile and its argument in a json file
+    
+    """
+
+    if not args:
+        print('_usage')
+        sys.exit(2)
+
+    if any(opt in ['-h', '--help'] for opt, optarg in opts):
+        print('_usage')
+        sys.exit()
+
+    if '=' in args[0]:
+        cmd, path = args[0].split('=')
+        if cmd not in 'log_path':
+            print('Error: {args[0]} contains an = but is not log_path option')
+            sys.exit(2)
+        args = args[1:]
+    elif args[0] in 'log_path':
+        path = args[1]
+        args = args[2:]
+    else:
+        path = None
+    sys.argv[:] = args  # Hide "pdb.py" and pdb options from argument list
+    __main__.__file__ = os.path.abspath(args[0])
+    log_info(args[1:], path=path, parsed_arguments=False)
+    subprocess.Popen(['python'] + args).wait(timeout=None)
-- 
GitLab