changeset 15:743c920bc041

fix password prompt
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 09 May 2013 21:42:37 -0700
parents 5f95af14b51c
children 76eac0c9953a
files setup.py silvermirror/unify.py silvermirror/unison.py
diffstat 3 files changed, 22 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/setup.py
+++ b/setup.py
@@ -1,15 +1,15 @@
 from setuptools import setup, find_packages
 
-version = '0.1.1'
+version = '0.2'
 
 setup(name='silvermirror',
       version=version,
-      description="mirror files",
+      description="mirror files across hosts",
       long_description="""\
 """,
       classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
       keywords='mirror unison',
       author='Jeff Hammel',
       author_email='k0scist@gmail.com',
       url='http://explosivedecompression.net',
       license='GPL',
--- a/silvermirror/unify.py
+++ b/silvermirror/unify.py
@@ -1,13 +1,12 @@
 #!/usr/bin/env python
 
 import getpass
 import os
-import pexpect
 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
@@ -22,19 +21,26 @@ def read_config(filename):
     config = ConfigMunger(filename).dict()
 
     ### main configuration
     main = config.pop('::SilverMirror::', {})
     if not main.get('basedir'):
         main['basedir'] = home()
     main['ignore'] = main.get('ignore', '').split() # patterns to ignore - not used
     main['hosts'] = main.get('hosts', '').split()
-    main['password'] = main.get('password', 'true') # XXX not used
     main['timeout'] = float(main.get('timeout', '10.'))
 
+    # 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
     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!
@@ -66,17 +72,17 @@ def unify(conf, _resources, test=False):
     addresses = ip_addresses().values()
     hosts = hosts.difference(addresses) # don't sync with self
     _hosts = []
     for host in hosts:
         s = socket.socket()
         s.settimeout(conf['main']['timeout'])
         if test:
             print 'Resolving %s' % host
-        try: 
+        try:
             s.connect((host, 22))
             s.close()
         except (socket.gaierror, socket.timeout, socket.error):
             continue
         _hosts.append(host)
     hosts = _hosts
     if test:
         print 'Hosts:'
@@ -110,29 +116,29 @@ def unify(conf, _resources, test=False):
     ### 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 resource in resources:
         for host in hosts:
-            reflector.sync(host, resource, resources[resource]['ignore'], pw, test)
+            reflector.sync(host, resource, resources[resource]['ignore'], pw.get('host'), test)
     os.chdir(cwd)
 
 def main(args=sys.argv[1:]):
 
     ### 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', 
+    parser.add_option('--test', dest='test',
                       action='store_true', default=False)
     (options, args) = parser.parse_args()
 
 
     ### configuration
     user_conf = os.path.join(home(), '.silvermirror')
     if options.config:
         assert os.path.exists(options.config)
--- a/silvermirror/unison.py
+++ b/silvermirror/unison.py
@@ -1,23 +1,26 @@
 """
 unison backend for silvermirror
 """
 
 import pexpect
-
+import subprocess
 from interface import Reflector
 
 class unison(Reflector):
 
     def sync(self, host, resource, ignore=(), password=None, test=False):
         command = ['unison', '-auto', '-batch', resource, 'ssh://%s/%s' % (host, resource)]
         for i in ignore:
             command.extend(('-ignore', "'Name %s'" % i))
 
         command = ' '.join(command)
         print command # XXX debug -- should go to logging
         if not test:
-            child = pexpect.spawn(command, timeout=36000, maxread=1)
-            child.expect('password: ')
-            child.sendline(password[host])
-            print child.read() # XXX -> logging
-
+            if password:
+                child = pexpect.spawn(command, timeout=36000, maxread=1)
+                child.expect('password: ')
+                child.sendline(password)
+                print child.read() # XXX -> logging
+            else:
+                # XXX should not use shell=True
+                subprocess.call(command, shell=True)