20
|
1 #!/usr/bin/env python
|
|
2
|
|
3 import optparse
|
|
4 import os
|
|
5 import sys
|
|
6
|
|
7 from .utils import home
|
|
8 from martini.config import ConfigMunger
|
|
9
|
|
10 class SilvermirrorConfiguration(object):
|
|
11 """
|
|
12 SilverMirror configuration class
|
|
13
|
|
14 The current form is an .ini file
|
|
15 [TODO: use `install_requires='configuration'`]:
|
|
16
|
|
17 [::SilverMirror::]
|
|
18 ...
|
|
19 """
|
|
20
|
|
21 main_section = '::SilverMirror::'
|
|
22
|
|
23 def __init__(self, filename=None):
|
|
24 if filename is not None:
|
|
25 self.read_config(filename)
|
|
26 def read_config(self, filename):
|
|
27
|
|
28 # read file
|
|
29 config = ConfigMunger(filename).dict()
|
|
30
|
|
31 # main configuration
|
|
32 main = config.pop(, {})
|
|
33 main.setdefault('basedir', home())
|
|
34 main['ignore'] = main.get('ignore', '').split() # patterns to ignore -
|
|
35 main['hosts'] = main.get('hosts', '').split()
|
|
36 main['timeout'] = float(main.get('timeout', '10.'))
|
|
37
|
|
38 # password prompt
|
|
39 truth = dict([(str(i).lower(), i) for i in (True, False)])
|
|
40 password = main.get('password', 'true')
|
|
41 try:
|
|
42 main['password'] = truth[password.lower()]
|
|
43 except KeyError:
|
|
44 raise KeyError("password must be True or False (You gave: '%s')" % password)
|
|
45
|
|
46 # resources
|
|
47 for resource in config:
|
|
48
|
|
49 # directory of resource
|
|
50 directory = config[resource].get('directory', resource)
|
|
51 if os.path.isabs(directory):
|
|
52 raise NotImplementedError("absolute directories will not work for now so....don't do this!")
|
|
53
|
|
54 else:
|
|
55 directory = os.path.join(main['basedir'], directory)
|
|
56 config[resource]['directory'] = directory.rstrip(os.path.sep)
|
|
57
|
|
58 # per-resource files to ignore
|
|
59 # XXX regexps for now (see `man unison`)
|
|
60 # - this is bad as whitespace patterns cannot be ignored
|
|
61 config[resource]['ignore'] = main['ignore'][:] + config[resource].get('ignore', '').split()
|
|
62
|
|
63 # set + return ditionary of config
|
|
64 self.main = main
|
|
65 self.resources = config
|
|
66 config = (main, config)
|
|
67
|
|
68 def main(args=sys.argv[1:]):
|
|
69
|
|
70 usage = '%prog [options]'
|
|
71 parser = optparse.OptionParser(usage=usage, description=__doc__)
|
|
72 options, args = parser.parse_args(args)
|
|
73
|
|
74 if __name__ == '__main__':
|
|
75 main()
|