]> asedeno.scripts.mit.edu Git - git-svn-keywords.git/commitdiff
Polish
authorAlejandro R. Sedeño <asedeno@mit.edu>
Fri, 25 Sep 2009 18:54:20 +0000 (14:54 -0400)
committerAlejandro R. Sedeño <asedeno@mit.edu>
Fri, 25 Sep 2009 19:04:48 +0000 (15:04 -0400)
version bump to 0.9
parse command line arguments

git-svn-keywords.py

index d1e6f21887e50f432aa3718b748aa4f0468e79b6..4f08171f36e5b9c622219614c6c93d5e6fce2f28 100755 (executable)
 # git svn keyword parsing, populating, and clearing.
 
 from __future__ import with_statement
-import ConfigParser, errno, os, re, urllib
+import errno, os, re, urllib
+from ConfigParser import ConfigParser
+from optparse import OptionParser
 import git
 
-VERSION = 0
+VERSION = "0.9"
 
 # Where we keep data in the repo.
 def gsk(g):
     return os.path.join(g.path, 'svn_keywords')
 
 #Configuration Data
-CONFIG = ConfigParser.ConfigParser()
-FILES = ConfigParser.ConfigParser()
-FILEINFO = ConfigParser.ConfigParser()
+CONFIG = ConfigParser()
+FILES = ConfigParser()
+FILEINFO = ConfigParser()
 
 CONFIG_PATH = ''
 FILES_PATH = ''
@@ -73,7 +75,7 @@ def parse_svn_unhandled(g):
 
     ver = -1
     if CONFIG.has_option('core', 'version'):
-        ver = CONFIG.getint('core', 'version')
+        ver = CONFIG.get('core', 'version')
 
     lastrev = None
     if ver == VERSION:
@@ -155,7 +157,7 @@ def get_path_info(g, path):
     return info_dict
 
 # Do the work.
-def smudge(g, clean=False):
+def smudge(g, options):
     parse_svn_unhandled(g)
     rev_head = int(g.git.svn('find-rev', 'HEAD'))
     url_base = g.git.svn('info', '--url')
@@ -172,7 +174,7 @@ def smudge(g, clean=False):
         kw_rev = max(filter(lambda x: x <= rev_head, map(int, FILES.options(path))))
 
         info_dict = {}
-        if not clean:
+        if not options.clean:
             info_dict.update(get_path_info(g, path))
             info_dict['URL'] = '/'.join([url_base, path])
             info_dict['Name'] = os.path.basename(path)
@@ -186,7 +188,7 @@ def smudge(g, clean=False):
         for k in keywords:
             for sk in svn_keywords:
                 if k in svn_keywords[sk]:
-                    if clean:
+                    if options.clean:
                         buf = re.sub(get_svn_keyword_re(sk), '$\\1$', buf)
                     elif sk == 'Id':
                         id_str = ' '.join([info_dict['Name'],
@@ -199,27 +201,40 @@ def smudge(g, clean=False):
 
         with open(os.path.join(g.wd, path), 'w') as f:
             f.write(buf)
-
-        #print path + ' [' + ', '.join(keywords) + '] [len: ' + str(len(buf)) +']'
-
-def clean(g):
-    smudge(g,True)
+        if options.verbose:
+            print path + ' [' + ', '.join(keywords) + '] [len: ' + str(len(buf)) +']'
 
 if __name__ == '__main__':
-    try:
-        g = git.Repo()
-    except git.errors.InvalidGitRepositoryError:
-        print "You are not in a git repository or working directory."
-        exit(1)
-
-    CONFIG_PATH = os.path.join(gsk(g), 'conf.ini')
-    FILES_PATH = os.path.join(gsk(g), 'files.ini')
-    FILEINFO_PATH = os.path.join(gsk(g), 'fileinfo.ini')
-
-    CONFIG.read(CONFIG_PATH)
-    for section in ['core','CommitToRev','BlobToCommit', 'RevInfo']:
-        if not CONFIG.has_section(section):
-            CONFIG.add_section(section)
-
-    smudge(g)
-    #clean(g)
+
+    parser = OptionParser(version="%prog "+str(VERSION))
+    parser.set_defaults(clean=None)
+    parser.add_option("-s", "--smudge",
+                      action="store_false", dest="clean",
+                      help="Populate svn:keywords.")
+    parser.add_option("-c", "--clean",
+                      action="store_true", dest="clean",
+                      help="Return svn:keywords to pristene state.")
+    parser.add_option("-v", "--verbose",
+                      action="store_true", dest="verbose", default=False)
+    (options, args) = parser.parse_args()
+
+    if (options.clean is None):
+        parser.print_help()
+        exit(0)
+    else:
+        try:
+            g = git.Repo()
+        except git.errors.InvalidGitRepositoryError:
+            print "You are not in a git repository or working directory."
+            exit(1)
+
+        CONFIG_PATH = os.path.join(gsk(g), 'conf.ini')
+        FILES_PATH = os.path.join(gsk(g), 'files.ini')
+        FILEINFO_PATH = os.path.join(gsk(g), 'fileinfo.ini')
+
+        CONFIG.read(CONFIG_PATH)
+        for section in ['core','CommitToRev','BlobToCommit', 'RevInfo']:
+            if not CONFIG.has_section(section):
+                CONFIG.add_section(section)
+
+        smudge(g, options)