changeset 42:7d28efc8dfa1 tip

fix + bump
author Jeff Hammel <k0scist@gmail.com>
date Thu, 23 Feb 2017 09:35:11 -0800
parents c9d222b85e63
children
files setup.py silvermirror/unify.py
diffstat 2 files changed, 16 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py
+++ b/setup.py
@@ -1,11 +1,11 @@
 from setuptools import setup
 
-version = '0.2.3'
+version = '0.3'
 
 setup(name='silvermirror',
       version=version,
       description="mirror files across hosts",
       long_description="""\
 """,
       classifiers=[],
       keywords='mirror unison',
--- a/silvermirror/unify.py
+++ b/silvermirror/unify.py
@@ -1,16 +1,17 @@
 #!/usr/bin/env python
 
 """
 unify virtual filesystems
 """
 
 import argparse
 import getpass
+import json
 import os
 import socket
 import subprocess
 import sys
 from martini.config import ConfigMunger
 from pkg_resources import iter_entry_points
 from pprint import pprint
 from .utils import home
@@ -38,17 +39,17 @@ def read_config(filename):
     # password prompt
     truth = dict([(str(i).lower(), i) for i in (True, False)])
     password = main.get('password', 'true')
     try:
         main['password'] = truth[password.lower()]
     except KeyError:
         raise KeyError("password must be True or False (You gave: '%s')" % password)
 
-    ### resources
+    # resources
     for resource in config:
 
         # directory of resource
         directory = config[resource].get('directory', resource)
         if not os.path.isabs(directory):
             # XXX note: absolute directories will not work for now
             # XXX so....don't do this!
             directory = os.path.join(main['basedir'], directory)
@@ -57,25 +58,27 @@ def read_config(filename):
         # per-resource files to ignore
         # XXX  regexps for now (see `man unison`)
         # - this is bad as whitespace patterns cannot be ignored
         ignore = main['ignore'][:]
         if config[resource].has_key('ignore'):
             ignore += config[resource]['ignore'].split()
         config[resource]['ignore'] = ignore
 
-    ###
-    config = { 'main': main, 'resources': config }
-    return config
+    # return configuration
+    return  {'main': main, 'resources': config}
 
-def unify(conf, _resources, test=False, verbose=True, notification_prefix='\n*** SilverMirror; '):
+
+def unify(conf,
+          _resources,
+          test=False,
+          verbose=True,
+          notification_prefix='\n*** SilverMirror; '):
     """unify virtual filesystems given configuration"""
 
-    # TODO: -> OO
-
     # log function
     def log(message):
         if verbose:
             print ("%s%s" % (notification_prefix, message))
 
     # passwords
     pw = {}
 
@@ -104,29 +107,30 @@ def unify(conf, _resources, test=False, 
         for host in hosts:
             pw[host] = getpass.getpass('Enter password for %s: ' % host)
     # TODO: ensure that the hosts are resolvable
     # XXX: hosts should actually be manageable on a per-resource basis
 
     ### determine resources to sync
     cwd = os.path.realpath(os.getcwd())
     resources = conf['resources']
-    if 'all' not in _resources:
+    if (resources is None) or ('all' not in _resources):
         if _resources:
             resources = dict([(key, value) for key, value in resources.items()
                               if key in _resources])
         else:
             for key, value in resources.items():
                 directory = os.path.realpath(value['directory']) + os.sep
                 if (cwd + os.sep).startswith(directory):
                     resources = { key: value }
                     break
     if test:
         log("Resources:\n")
         pprint(resources)
+        return
 
     # 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
     try:
         os.chdir(conf['main']['basedir'])
@@ -136,31 +140,32 @@ def unify(conf, _resources, test=False, 
             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)
     finally:
         os.chdir(cwd)
 
+
 def main(args=sys.argv[1:]):
     """CLI"""
 
     # 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='*',
+    parser.add_argument('resources', nargs='*', default=None,
                         help="resources to sync, or all if omitted")
     options = parser.parse_args(args)
 
 
     # configuration
     user_conf = os.path.join(home(), '.silvermirror')
     if options.config:
         if not os.path.exists(options.config):
@@ -174,12 +179,12 @@ def main(args=sys.argv[1:]):
         else:
             conf = make_config(user_conf)
 
     # fix up configuration from command line options
     conf['hosts'] = set(options.hosts or conf['main']['hosts'])
     conf['main']['password'] = options.password and conf['main']['password']
 
     # mirror all the things
-    unify(conf, args, options.test)
+    unify(conf, options.resources, test=options.test, verbose=options.verbose)
 
 if __name__ == '__main__':
     unify()