Tue, 23 Jan 2018 10:44:50 +0000
pyyaml was missing from listed dependencies
23 | 1 | #!/usr/local/bin/python3 |
0 | 2 | # |
3 | # Borgend configuration loader | |
4 | # | |
5 | ||
6 | import yaml | |
7 | import io | |
8 | import os | |
9 | import string | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
10 | import logging |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
11 | import borgend |
43
8f3ac19f11b6
Use platform package to detect whether to:
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
12 | import platform |
2 | 13 | from functools import reduce |
0 | 14 | |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
15 | logger=borgend.logger.getChild(__name__) |
31 | 16 | |
2 | 17 | # |
18 | # Defaults | |
19 | # | |
20 | ||
21 | defaults={ | |
21 | 22 | # borg |
23 | # Default: backup every 6 hours (21600 seconds) | |
24 | 'backup_interval': 21600, | |
25 | # Default: retry every 15 minutes if unable to connect / unfinished backup | |
26 | 'retry_interval': 900, | |
27 | # Extract passphrases at startup or on demand? | |
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
28 | 'extract_passphrases_at_startup': True, |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
29 | # Do not insert a quit menu entry (useful for installing on computers of |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
30 | # inexperienced users) |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
31 | 'no_quit_menu_entry': False, |
21 | 32 | # Borg settings |
33 | 'borg': { | |
34 | 'executable': 'borg', | |
35 | 'common_parameters': [], | |
36 | 'create_parameters': [], | |
37 | 'prune_parameters': [], | |
2 | 38 | } |
39 | } | |
40 | ||
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
41 | # |
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
42 | # Locations |
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
43 | # |
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
44 | |
43
8f3ac19f11b6
Use platform package to detect whether to:
Tuomo Valkonen <tuomov@iki.fi>
parents:
34
diff
changeset
|
45 | if platform.system()!='Darwin': |
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
46 | import xdg |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
47 | cfgfile=os.path.join(xdg.XDG_CONFIG_HOME, borgend.appname, "config.yaml") |
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
48 | logs_dir=os.path.join(xdg.XDG_DATA_HOME, borgend.appname, "logs") |
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
49 | else: |
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
50 | import rumps |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
51 | __base=rumps.application_support(borgend.appname) |
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
52 | cfgfile=os.path.join(__base, "config.yaml") |
34
9fce700d42de
Log window and other logging improvements
Tuomo Valkonen <tuomov@iki.fi>
parents:
33
diff
changeset
|
53 | logs_dir=os.path.join(__base, "logs") |
2 | 54 | |
55 | # | |
56 | # Type checking etc. | |
57 | # | |
58 | ||
59 | def error(x): | |
60 | raise AssertionError(x) | |
61 | ||
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
62 | def check_bool(cfg, field, descr, loc, default=None): |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
63 | return check_field(cfg, field, descr, loc, default, |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
64 | lambda x: isinstance(x, bool)) |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
65 | |
2 | 66 | def check_string(cfg, field, descr, loc, default=None): |
67 | return check_field(cfg, field, descr, loc, default, | |
68 | lambda x: isinstance(x, str)) | |
69 | ||
70 | def check_dict(cfg, field, descr, loc, default=None): | |
71 | return check_field(cfg, field, descr, loc, default, | |
72 | lambda x: isinstance(x, dict)) | |
73 | ||
74 | def check_list(cfg, field, descr, loc, default=None): | |
75 | return check_field(cfg, field, descr, loc, default, | |
76 | lambda x: isinstance(x, list)) | |
77 | ||
78 | def is_list_of(x, chk): | |
79 | if x is None: | |
80 | return True | |
81 | elif isinstance(x, list): | |
82 | return reduce(lambda y, z: y and chk(z), x, True) | |
83 | else: | |
84 | return False | |
85 | ||
86 | def check_list_of_dicts(cfg, field, descr, loc, default=None): | |
87 | return check_field(cfg, field, descr, loc, default, | |
88 | lambda x: is_list_of(x, lambda z: isinstance(z, dict))) | |
89 | ||
90 | def check_list_of_strings(cfg, field, descr, loc, default=None): | |
91 | return check_field(cfg, field, descr, loc, default, | |
92 | lambda x: is_list_of(x, lambda z: isinstance(z, str))) | |
93 | ||
94 | def check_nonempty_list_of_strings(cfg, field, descr, loc): | |
95 | return check_list_of_strings(cfg, field, descr, loc) and cfg[field] | |
96 | ||
97 | ||
98 | def check_nonneg_int(cfg, field, descr, loc, default=None): | |
99 | return check_field(cfg, field, descr, loc, default, | |
100 | lambda x: isinstance(x, int) and x>=0) | |
101 | ||
102 | def check_field(cfg, field, descr, loc, default, check): | |
103 | if field in cfg: | |
104 | tmp=cfg[field] | |
105 | if not check(tmp): | |
106 | error("%s is of invalid type for %s" % (field, loc)) | |
107 | return tmp | |
108 | else: | |
109 | if default is not None: | |
110 | return default | |
111 | else: | |
112 | error("%s is not configured for %s" % (field, loc)) | |
113 | ||
114 | # | |
115 | # Conversion of config into command line | |
116 | # | |
117 | ||
118 | def arglistify(args): | |
119 | flatten=lambda l: [item for sublist in l for item in sublist] | |
120 | if args is None: | |
121 | return [] | |
122 | else: | |
123 | return flatten([['--' + key, str(d[key])] for d in args for key in d]) | |
124 | ||
125 | # | |
126 | # Load config on module load | |
127 | # | |
0 | 128 | |
129 | def expand_env(cfg, env): | |
130 | if isinstance(cfg, dict): | |
131 | out={key: expand_env(val, env) for key, val in cfg.items()} | |
132 | elif isinstance(cfg, list): | |
133 | out=[expand_env(val, env) for val in cfg] | |
134 | elif isinstance(cfg, str): | |
135 | out=string.Template(cfg).substitute(os.environ) | |
136 | else: | |
2 | 137 | out=cfg |
138 | ||
0 | 139 | return out |
140 | ||
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
141 | if not (os.path.exists(cfgfile) and os.path.isfile(cfgfile)): |
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
142 | raise SystemExit("Configuration file required: %s" % cfgfile) |
0 | 143 | |
33
91421eeb4426
Use rumps.application_support instead of xdg paths. Also separated branding into config.py
Tuomo Valkonen <tuomov@iki.fi>
parents:
31
diff
changeset
|
144 | logger.info("Reading configuration %s missing" % cfgfile) |
0 | 145 | |
146 | with io.open(cfgfile, 'r') as file: | |
147 | settings=expand_env(yaml.load(file), os.environ); | |
1
4cdc9c1f6b28
basic scheduler structure draft, etc.
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
148 | |
2 | 149 | # |
150 | # Verify basic settings | |
151 | # | |
152 | ||
21 | 153 | def check_and_set(cfg, field, loc, defa, fn): |
154 | cfg[field]=fn(cfg, field, field, loc, defa[field]) | |
155 | ||
156 | def check_parameters(cmd): | |
157 | check_and_set(settings['borg'], cmd+'_parameters', | |
158 | 'borg', defaults['borg'], | |
159 | check_list_of_dicts) | |
2 | 160 | |
21 | 161 | check_and_set(settings, 'backup_interval', 'top-level', defaults, check_nonneg_int) |
162 | check_and_set(settings, 'retry_interval', 'top-level', defaults, check_nonneg_int) | |
30
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
163 | check_and_set(settings, 'extract_passphrases_at_startup', 'top-level', defaults, check_nonneg_int) |
3dd525652dc8
Added no_quit_menu_entry option
Tuomo Valkonen <tuomov@iki.fi>
parents:
23
diff
changeset
|
164 | check_and_set(settings, 'no_quit_menu_entry', 'top-level', defaults, check_bool) |
21 | 165 | check_and_set(settings, 'borg', 'top-level', defaults, check_dict) |
166 | # Check parameters within 'borg' | |
167 | if True: | |
168 | check_and_set(settings['borg'], 'executable', 'borg', | |
169 | defaults['borg'], check_string) | |
2 | 170 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
171 | check_parameters('common') |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
172 | check_parameters('create') |
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
173 | check_parameters('prune') |
2 | 174 | |
175 |