comparison silvermirror/unify.py @ 42:7d28efc8dfa1

fix + bump
author Jeff Hammel <k0scist@gmail.com>
date Thu, 23 Feb 2017 09:35:11 -0800
parents d081ca2e8696
children 436d8e5ea294
comparison
equal deleted inserted replaced
41:c9d222b85e63 42:7d28efc8dfa1
4 unify virtual filesystems 4 unify virtual filesystems
5 """ 5 """
6 6
7 import argparse 7 import argparse
8 import getpass 8 import getpass
9 import json
9 import os 10 import os
10 import socket 11 import socket
11 import subprocess 12 import subprocess
12 import sys 13 import sys
13 from martini.config import ConfigMunger 14 from martini.config import ConfigMunger
41 try: 42 try:
42 main['password'] = truth[password.lower()] 43 main['password'] = truth[password.lower()]
43 except KeyError: 44 except KeyError:
44 raise KeyError("password must be True or False (You gave: '%s')" % password) 45 raise KeyError("password must be True or False (You gave: '%s')" % password)
45 46
46 ### resources 47 # resources
47 for resource in config: 48 for resource in config:
48 49
49 # directory of resource 50 # directory of resource
50 directory = config[resource].get('directory', resource) 51 directory = config[resource].get('directory', resource)
51 if not os.path.isabs(directory): 52 if not os.path.isabs(directory):
60 ignore = main['ignore'][:] 61 ignore = main['ignore'][:]
61 if config[resource].has_key('ignore'): 62 if config[resource].has_key('ignore'):
62 ignore += config[resource]['ignore'].split() 63 ignore += config[resource]['ignore'].split()
63 config[resource]['ignore'] = ignore 64 config[resource]['ignore'] = ignore
64 65
65 ### 66 # return configuration
66 config = { 'main': main, 'resources': config } 67 return {'main': main, 'resources': config}
67 return config
68 68
69 def unify(conf, _resources, test=False, verbose=True, notification_prefix='\n*** SilverMirror; '): 69
70 def unify(conf,
71 _resources,
72 test=False,
73 verbose=True,
74 notification_prefix='\n*** SilverMirror; '):
70 """unify virtual filesystems given configuration""" 75 """unify virtual filesystems given configuration"""
71
72 # TODO: -> OO
73 76
74 # log function 77 # log function
75 def log(message): 78 def log(message):
76 if verbose: 79 if verbose:
77 print ("%s%s" % (notification_prefix, message)) 80 print ("%s%s" % (notification_prefix, message))
107 # XXX: hosts should actually be manageable on a per-resource basis 110 # XXX: hosts should actually be manageable on a per-resource basis
108 111
109 ### determine resources to sync 112 ### determine resources to sync
110 cwd = os.path.realpath(os.getcwd()) 113 cwd = os.path.realpath(os.getcwd())
111 resources = conf['resources'] 114 resources = conf['resources']
112 if 'all' not in _resources: 115 if (resources is None) or ('all' not in _resources):
113 if _resources: 116 if _resources:
114 resources = dict([(key, value) for key, value in resources.items() 117 resources = dict([(key, value) for key, value in resources.items()
115 if key in _resources]) 118 if key in _resources])
116 else: 119 else:
117 for key, value in resources.items(): 120 for key, value in resources.items():
120 resources = { key: value } 123 resources = { key: value }
121 break 124 break
122 if test: 125 if test:
123 log("Resources:\n") 126 log("Resources:\n")
124 pprint(resources) 127 pprint(resources)
128 return
125 129
126 # choose reflector backend 130 # choose reflector backend
127 reflectors = dict([(i.name, i.load()) for i in iter_entry_points('silvermirror.reflectors')]) 131 reflectors = dict([(i.name, i.load()) for i in iter_entry_points('silvermirror.reflectors')])
128 reflector = reflectors['unison']() # only one right now 132 reflector = reflectors['unison']() # only one right now
129 133
139 for host in hosts: 143 for host in hosts:
140 reflector.sync(host, resource, resources[resource]['ignore'], pw.get('host'), test) 144 reflector.sync(host, resource, resources[resource]['ignore'], pw.get('host'), test)
141 finally: 145 finally:
142 os.chdir(cwd) 146 os.chdir(cwd)
143 147
148
144 def main(args=sys.argv[1:]): 149 def main(args=sys.argv[1:]):
145 """CLI""" 150 """CLI"""
146 151
147 # parse command line 152 # parse command line
148 parser = argparse.ArgumentParser(description=__doc__) 153 parser = argparse.ArgumentParser(description=__doc__)
153 action='store_false', default=True) 158 action='store_false', default=True)
154 parser.add_argument('--test', dest='test', 159 parser.add_argument('--test', dest='test',
155 action='store_true', default=False) 160 action='store_true', default=False)
156 parser.add_argument('-v', '--verbose', 161 parser.add_argument('-v', '--verbose',
157 action='store_true', default=False) 162 action='store_true', default=False)
158 parser.add_argument('resources', nargs='*', 163 parser.add_argument('resources', nargs='*', default=None,
159 help="resources to sync, or all if omitted") 164 help="resources to sync, or all if omitted")
160 options = parser.parse_args(args) 165 options = parser.parse_args(args)
161 166
162 167
163 # configuration 168 # configuration
177 # fix up configuration from command line options 182 # fix up configuration from command line options
178 conf['hosts'] = set(options.hosts or conf['main']['hosts']) 183 conf['hosts'] = set(options.hosts or conf['main']['hosts'])
179 conf['main']['password'] = options.password and conf['main']['password'] 184 conf['main']['password'] = options.password and conf['main']['password']
180 185
181 # mirror all the things 186 # mirror all the things
182 unify(conf, args, options.test) 187 unify(conf, options.resources, test=options.test, verbose=options.verbose)
183 188
184 if __name__ == '__main__': 189 if __name__ == '__main__':
185 unify() 190 unify()