forked from bit-team/backintime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-manpage-backintime-config.py
executable file
·411 lines (338 loc) · 13.2 KB
/
create-manpage-backintime-config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#!/usr/bin/env python3
# Back In Time
# Copyright (C) 2012-2022 Germar Reitze
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""This script is a helper to create a manpage about Back In Times's config
file.
The file `common/config.py` is parsed for variable names, default values and
other information. The founder of that script @Germar gave a detailed
description about that script in #1354.
The script reads every line and tries to analyze it:
- It searches for `DEFAULT` and puts those into a `dict` for later replacing
the variable with the value.
- If that didn't match it will look for lines starting with `#?` which is
basically my own description for the manpage-entry.
Multiple lines will get merged and stored in `commentline` until the
processing of the current config option is done. That will reset
`commentline`.
- If a line starts with `#` it will be skipped.
- Next the script searches for lines which ``return`` the config value (like
`snapshots.ssh.port`. There it will extract the
key/name (`snapshots.ssh.port`), the default value (`22`),
the instance (`Int`) and if it is a profile or general value.
- If the line contains a `List` value like `snapshots.include` it will
process all values for the list like `snapshots.include.<I>.value` and
`snapshots.include.<I>.type`. Also it will add the size like
`snapshots.include.size`.
In `process_line` it will replace some information with those I wrote manually
in the `#?` description, separated by `;` there is the comment, value,
force_default and force_var. If there is no forced value it will chose the
value based on the instance with `select_values`
"""
import re
import os
import sys
from time import strftime, gmtime
PATH = os.path.join(os.getcwd(), 'common')
CONFIG = os.path.join(PATH, 'config.py')
MAN = os.path.join(PATH, 'man/C/backintime-config.1')
with open(os.path.join(PATH, '../VERSION'), 'r') as f:
VERSION = f.read().strip('\n')
SORT = True # True = sort by alphabet; False = sort by line numbering
c_list = re.compile(r'.*?self\.(?!set)((?:profile)?)(List)Value ?\( ?[\'"](.*?)[\'"], ?((?:\(.*\)|[^,]*)), ?[\'"]?([^\'",\)]*)[\'"]?')
c = re.compile(r'.*?self\.(?!set)((?:profile)?)(.*?)Value ?\( ?[\'"](.*?)[\'"] ?(%?[^,]*?), ?[\'"]?([^\'",\)]*)[\'"]?')
HEADER = r'''.TH backintime-config 1 "%s" "version %s" "USER COMMANDS"
.SH NAME
config \- BackInTime configuration files.
.SH SYNOPSIS
~/.config/backintime/config
.br
/etc/backintime/config
.SH DESCRIPTION
Back In Time was developed as pure GUI program and so most functions are only
usable with backintime-qt. But it is possible to use
Back In Time e.g. on a headless server. You have to create the configuration file
(~/.config/backintime/config) manually. Look inside /usr/share/doc/backintime\-common/examples/ for examples.
.PP
The configuration file has the following format:
.br
keyword=arguments
.PP
Arguments don't need to be quoted. All characters are allowed except '='.
.PP
Run 'backintime check-config' to verify the configfile, create the snapshot folder and crontab entries.
.SH POSSIBLE KEYWORDS
''' % (strftime('%b %Y', gmtime()), VERSION)
FOOTER = r'''.SH SEE ALSO
backintime, backintime-qt, backintime-askpass.
.PP
Back In Time also has a website: https://github.com/bit-team/backintime
.SH AUTHOR
This manual page was written by BIT Team(<[email protected]>).
'''
INSTANCE = 'instance'
NAME = 'name'
VALUES = 'values'
DEFAULT = 'default'
COMMENT = 'comment'
REFERENCE = 'reference'
LINE = 'line'
def groff_indented_paragraph(label: str, indent: int=6) -> str:
""".IP - Indented Paragraph"""
return f'.IP "{label}" {indent}'
def groff_italic(text: str) -> str:
"""\\fi - Italic"""
return f'\\fI{text}\\fR'
def groff_indented_block(text: str) -> str:
"""
.RS - Start indented block
.RE - End indented block
"""
return f'\n.RS\n{text}\n.RE\n'
def groff_linebreak() -> str:
""".br - Line break"""
return '.br\n'
def groff_paragraph_break() -> str:
""".PP - Paragraph break"""
return '.PP\n'
def output(instance='', name='', values='', default='',
comment='', reference='', line=0):
"""Generate GNU Troff (groff) markup code for the given config entry."""
if not default:
default = "''"
ret = f'Type: {instance.lower():<10}Allowed Values: {values}\n'
ret += groff_linebreak()
ret += f'{comment}\n'
ret += groff_paragraph_break()
if SORT:
ret += f'Default: {default}'
else:
ret += f'Default: {default:<18} {reference} line: {line}'
ret = groff_indented_block(ret)
ret = groff_indented_paragraph(groff_italic(name)) + ret
return ret
def select(a, b):
if a:
return a
return b
def select_values(instance, values):
if values:
return values
return {
'bool': 'true|false',
'str': 'text',
'int': '0-99999'
}[instance.lower()]
def process_line(d, key, profile, instance, name, var, default, commentline,
values, force_var, force_default, replace_default, counter):
"""Parsing the config.py Python code"""
# Ignore commentlines with #?! and 'config.version'
comment = None
if not commentline.startswith('!') and key not in d:
d[key] = {}
commentline = commentline.split(';')
try:
comment = commentline[0]
values = commentline[1]
force_default = commentline[2]
force_var = commentline[3]
except IndexError:
pass
if default.startswith('self.') and default[5:] in replace_default:
default = replace_default[default[5:]]
if isinstance(force_default, str) \
and force_default.startswith('self.') \
and force_default[5:] in replace_default:
force_default = replace_default[force_default[5:]]
if instance.lower() == 'bool':
default = default.lower()
d[key][INSTANCE] = instance
d[key][NAME] = re.sub(
r'%[\S]', '<%s>' % select(force_var, var).upper(), name
)
d[key][VALUES] = select_values(instance, values)
d[key][DEFAULT] = select(force_default, default)
d[key][COMMENT] = re.sub(r'\\n', '\n.br\n', comment)
d[key][REFERENCE] = 'config.py'
d[key][LINE] = counter
def main():
d = {
'profiles.version': {
INSTANCE: 'int',
NAME: 'profiles.version',
VALUES: '1',
DEFAULT: '1',
COMMENT: 'Internal version of profiles config.',
REFERENCE: 'configfile.py',
LINE: 419
},
'profiles': {
INSTANCE: 'str',
NAME: 'profiles',
VALUES: 'int separated by colon (e.g. 1:3:4)',
DEFAULT: '1',
COMMENT: 'All active Profiles (<N> in profile<N>.snapshots...).',
REFERENCE: 'configfile.py',
LINE: 472
},
'profile<N>.name': {
INSTANCE: 'str',
NAME: 'profile<N>.name',
VALUES: 'text',
DEFAULT: 'Main profile',
COMMENT: 'Name of this profile.',
REFERENCE: 'configfile.py',
LINE: 704
}
}
# Default variables and there values collected from config.py
replace_default = {}
# Variables named "CONFIG_VERSION" or with names starting with "DEFAULT"
regex_default = re.compile(r'(^DEFAULT[\w]*|CONFIG_VERSION)[\s]*= (.*)')
with open(CONFIG, 'r') as f:
print(f'Read and parse "{CONFIG}".')
commentline = ''
values = force_var = force_default = instance \
= name = var = default = None
for counter, line in enumerate(f, 1):
line = line.lstrip()
# parse for DEFAULT variable
m_default = regex_default.match(line)
# DEFAULT variable
if m_default:
replace_default[m_default.group(1)] \
= m_default.group(2)
continue
# Comment intended to use for the manpage
if line.startswith('#?'):
if commentline \
and ';' not in commentline \
and not commentline.endswith('\\n'):
commentline += ' '
commentline += line.lstrip('#?').rstrip('\n')
continue
# Simple comments are ignored
if line.startswith('#'):
commentline = ''
continue
# e.g. "return self.profileListValue('snapshots.include', ('str:value', 'int:type'), [], profile_id)"
# becomes
# ('profile', 'List', 'snapshots.include', "('str:value', 'int:type')", '[]')
m = c_list.match(line)
if not m:
# e.g. "return self.profileBoolValue('snapshots.use_checksum', False, profile_id)"
# becomes
# ('profile', 'Bool', 'snapshots.use_checksum', '', 'False')
m = c.match(line)
# Ignore undocumented (without "#?" comments) variables.
if m and not commentline:
continue
if m:
profile, instance, name, var, default = m.groups()
if profile == 'profile':
name = 'profile<N>.' + name
var = var.lstrip('% ')
if instance.lower() == 'list':
type_key = [x.strip('"\'') for x in re.findall(r'["\'].*?["\']', var)]
commentline_split = commentline.split('::')
for i, tk in enumerate(type_key):
t, k = tk.split(':', maxsplit=1)
process_line(
d,
key,
profile,
'int',
'%s.size' % name,
var,
r'\-1',
'Quantity of %s.<I> entries.' % name,
values,
force_var,
force_default,
replace_default,
counter)
key = '%s.%s' % (name, k)
key = key.lower()
process_line(
d,
key,
profile,
t,
'%s.<I>.%s' % (name, k),
var,
'',
commentline_split[i],
values,
force_var,
force_default,
replace_default,
counter
)
else:
key = re.sub(r'%[\S]', var, name).lower()
process_line(
d,
key,
profile,
instance,
name,
var,
default,
commentline,
values,
force_var,
force_default,
replace_default,
counter
)
values = force_var = force_default = instance \
= name = var = default = None
commentline = ''
"""
Example for content of 'd':
{
"profiles": {
"instance": "str",
"name": "profiles",
"values": "int separated by colon (e.g. 1:3:4)",
"default": "1",
"comment": "All active Profiles (<N> in profile<N>.snapshots...).",
"reference": "configfile.py",
"line": 472
},
"profile<N>.name": {
"instance": "str",
"name": "profile<N>.name",
"values": "text",
"default": "Main profile",
"comment": "Name of this profile.",
"reference": "configfile.py",
"line": 704
}
"""
with open(MAN, 'w') as f:
print(f'Write GNU Troff (groff) markup to "{MAN}". {SORT=}')
f.write(HEADER)
if SORT:
# Sort by alphabet
s = lambda x: x
else:
# Sort by line numbering (in the source file)
s = lambda x: d[x][LINE]
f.write('\n'.join(output(**d[key]) for key in sorted(d, key=s)))
f.write(FOOTER)
if __name__ == '__main__':
main()