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
+++ b/silvermirror/hg.py
@@ -1,37 +1,36 @@
 #!/usr/bin/env python
 
 """
 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):
     """
     get changes from host on path
     """
     _ui = ui.ui()
     url = '%s/%s' % (host, path)
     try:
         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)
         return
 
     commands.pull(_ui, repo, url, rev=None, force=False, update=True)
 
 # TODO:
@@ -59,41 +58,43 @@ def repositories(url):
     """
     element = lxml.html.parse(url)
     tds = element.xpath('//tr[position() > 1]/td[1]')
     repos = [i.text_content() for i in tds]
     return repos
 
 
 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)
 
 if __name__ == '__main__':
     main()