changeset 35:e86d475435ee

use argparse and try to diagnose strange error
author Jeff Hammel <k0scist@gmail.com>
date Sun, 04 May 2014 23:14:15 -0700
parents f0b30886801f
children 52937c25edf0
files setup.py silvermirror/unify.py
diffstat 2 files changed, 41 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py	Fri Jan 31 23:25:46 2014 -0800
+++ b/setup.py	Sun May 04 23:14:15 2014 -0700
@@ -1,6 +1,6 @@
-from setuptools import setup, find_packages
+from setuptools import setup
 
-version = '0.2.1'
+version = '0.2.2'
 
 setup(name='silvermirror',
       version=version,
--- a/silvermirror/unify.py	Fri Jan 31 23:25:46 2014 -0800
+++ b/silvermirror/unify.py	Sun May 04 23:14:15 2014 -0700
@@ -1,13 +1,16 @@
 #!/usr/bin/env python
 
+"""
+unify virtual filesystems
+"""
+
+import argparse
 import getpass
 import os
 import socket
 import subprocess
 import sys
-
 from martini.config import ConfigMunger
-from optparse import OptionParser
 from pkg_resources import iter_entry_points
 from pprint import pprint
 from utils import home
@@ -61,6 +64,8 @@
     return config
 
 def unify(conf, _resources, test=False, verbose=True, notification_prefix='\n*** SilverMirror; '):
+    """unify virtual filesystems given configuration"""
+
     # TODO: -> OO
 
     # log function
@@ -80,7 +85,7 @@
         s = socket.socket()
         s.settimeout(conf['main']['timeout'])
         if test:
-            print 'Resolving %s' % host
+            print ('Resolving %s' % host)
         try:
             s.connect((host, 22))
             s.close()
@@ -115,43 +120,51 @@
         log("Resources:\n")
         pprint(resources)
 
-    ### choose reflector backend
+    # choose reflector backend
     reflectors = dict([(i.name, i.load()) for i in iter_entry_points('silvermirror.reflectors')])
     reflector = reflectors['unison']() # only one right now
 
-    ### sync with hosts
-    os.chdir(conf['main']['basedir'])
-    for index, resource in enumerate(resources):
+    # sync with hosts
+    try:
+        os.chdir(conf['main']['basedir'])
+        for index, resource in enumerate(resources):
 
-        # echo resource
-        log("syncing:'%s' [%d/%d]" % (resource, index+1, len(resources)))
+            # echo resource
+            log("syncing:'%s' [%d/%d]" % (resource, index+1, len(resources)))
 
-        # loop over hosts
-        for host in hosts:
-            reflector.sync(host, resource, resources[resource]['ignore'], pw.get('host'), test)
-    os.chdir(cwd)
+            # loop over hosts
+            for host in hosts:
+                reflector.sync(host, resource, resources[resource]['ignore'], pw.get('host'), test)
+    finally:
+        os.chdir(cwd)
 
 def main(args=sys.argv[1:]):
+    """CLI"""
 
-    ### command line options
-    parser = OptionParser()
-    parser.add_option('-c', '--config')
-    parser.add_option('-H', '--host', dest='hosts',
-                      action='append', default=None)
-    parser.add_option('--no-password', dest='password',
-                      action='store_false', default=True)
-    parser.add_option('--test', dest='test',
-                      action='store_true', default=False)
-    (options, args) = parser.parse_args()
+    # parse command line
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('-c', '--config')
+    parser.add_argument('-H', '--host', dest='hosts', action='append',
+                        help="hosts to sync with")
+    parser.add_argument('--no-password', dest='password',
+                        action='store_false', default=True)
+    parser.add_argument('--test', dest='test',
+                        action='store_true', default=False)
+    parser.add_argument('-v', '--verbose',
+                        action='store_true', default=False)
+    parser.add_argument('resources', nargs='*',
+                        help="resources to sync, or all if omitted")
+    options = parser.parse_args(args)
 
 
-    ### configuration
+    # configuration
     user_conf = os.path.join(home(), '.silvermirror')
     if options.config:
-        assert os.path.exists(options.config)
+        if not os.path.exists(options.config):
+            parser.error("Configuration file '" + options.config + "' does not exist")
         conf = read_config(options.config)
     else:
-        for i in user_conf, '/etc/silvermirror':
+        for i in (user_conf, '/etc/silvermirror'):
             if os.path.exists(i):
                 conf = read_config(i)
                 break