changeset 27:a648f57b1921

STUB: silvermirror/hg.py
author Jeff Hammel <k0scist@gmail.com>
date Fri, 31 Jan 2014 19:26:34 -0800
parents 16a12d660609
children 03911cb46f53
files silvermirror/hg.py
diffstat 1 files changed, 25 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/silvermirror/hg.py	Fri Jan 31 18:56:29 2014 -0800
+++ b/silvermirror/hg.py	Fri Jan 31 19:26:34 2014 -0800
@@ -4,18 +4,16 @@
 stub for the hg backend of silvermirror
 """
 
+import os
+import sys
+import argparse
+
+_import_error = None
 try:
     import lxml.html
-    import mercurial
-    from mercurial import commands, hg, ui
-    requirements_fulfilled = True
-except ImportError as e:
-    _import_error = e
-    requirements_fulfilled = False
+except ImportError as _import_error:
+    pass
 
-import os
-import sys
-from optparse import OptionParser
 
 def update(host, path):
     """
@@ -27,6 +25,7 @@
         repo = hg.repository(_ui, path)
         print 'Updating %s:' % path
     except Exception, e:
+        import pdb;pdb.set_trace()
         repo = hg.repository(_ui, url)
         print 'Cloning %s:' % path
         commands.clone(_ui, repo, pull=False, uncompressed=False, rev=None, noupdate=False)
@@ -64,34 +63,36 @@
 
 
 def main(args=sys.argv[1:]):
-    parser = OptionParser()
-    parser.add_option('-H', '--host', dest='host')
-    parser.add_option('-d', '--directory', dest='directory',
+    """CLI"""
+
+    # parse command line
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('host', dest='host',
+                      help="URL of mercurial repository index page")
+    parser.add_argument('-d', '--directory', dest='directory',
                       default=os.path.join(os.environ['HOME'], 'hg'),
-                      help="directory to clone/update to")
-    parser.add_option('--list', dest='list',
-                      action='store_true', default=False)
-    options, args = parser.parse_args(args)
-    if not requirements_fulfilled:
-        # Hack; this should all be better o_O
-        parser.error("Must have mercurial and lxml packages to use, sorry: {}".format(e))
-    if not options.host:
-        parser.print_usage()
-        parser.exit()
+                      help="base directory to clone/update to [DEFAULT: %default]")
+    parser.add_argument('--list', dest='list',
+                      action='store_true', default=False,
+                      help="list repositories and exit")
+    options = parser.parse_args(args)
+    if _import_error is not None:
+        # XXX better error handling
+        parser.error("Must have hglib and lxml package to use, sorry: {}".format(_import_error))
 
     # kill trailing slash
     options.host = options.host.rstrip('/')
 
+    # get repositories
     repos = repositories(options.host)
     if options.list:
         for repo in repos:
             print repo
         sys.exit(0)
 
+    # clone/update repos to directory
     if not os.path.exists(options.directory):
         os.mkdir(options.directory)
-
-    os.chdir(options.directory)
     for repo in repos:
         update(options.host, repo)