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