Sat, 20 Jan 2018 23:00:43 +0000
Added offline symbol B⦙ (no offline detection yet)
0 | 1 | # |
2 | # Borgend borg launcher / processor | |
3 | # | |
4 | ||
5 | import json | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
6 | import logging |
2 | 7 | from subprocess import Popen, PIPE |
8 | from config import settings, arglistify | |
0 | 9 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
10 | necessary_opts=['--log-json', '--progress'] |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
11 | |
7 | 12 | necessary_opts_for={ |
13 | 'create': ['--json'], | |
14 | 'info': ['--json'], | |
15 | 'list': ['--json'], | |
16 | } | |
17 | ||
0 | 18 | class BorgInstance: |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
19 | def __init__(self, operation, archive_or_repository, args, argsl): |
0 | 20 | self.operation=operation; |
21 | self.args=args; | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
22 | self.archive_or_repository=archive_or_repository; |
2 | 23 | self.argsl=argsl; |
0 | 24 | |
25 | def construct_cmdline(self): | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
26 | cmd=([settings['borg']['executable']]+necessary_opts+ |
2 | 27 | arglistify(settings['borg']['common_parameters'])+ |
28 | [self.operation]) | |
29 | tmp1=self.operation+'_parameters' | |
30 | if tmp1 in settings['borg']: | |
31 | cmd=cmd+arglistify(settings['borg'][tmp1]) | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
32 | |
7 | 33 | if self.operation in necessary_opts_for: |
34 | cmd=cmd+necessary_opts_for[self.operation] | |
35 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
36 | return cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl |
0 | 37 | |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
38 | def launch(self): |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
39 | cmd=self.construct_cmdline() |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
40 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
41 | logging.info('Launching ' + str(cmd)) |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
42 | |
12 | 43 | self.proc=Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE) |
44 | ||
45 | # We don't do password input etc. | |
46 | self.proc.stdin.close() | |
0 | 47 | |
7 | 48 | def read_result(self): |
49 | stream=self.proc.stdout | |
50 | line=stream.read(-1) | |
51 | if line==b'': | |
52 | logging.debug('Borg stdout pipe EOF?') | |
53 | return None | |
54 | ||
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
55 | try: |
7 | 56 | return json.loads(line) |
57 | except: | |
58 | logging.warning('JSON parse failed on: "%s"' % line) | |
59 | return None | |
60 | ||
61 | def read_log(self): | |
62 | stream=self.proc.stderr | |
63 | try: | |
64 | line=stream.readline() | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
65 | except err: |
7 | 66 | logging.debug('Pipe read failed: %s' % str(err)) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
67 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
68 | return {'type': 'exception', 'exception': err} |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
69 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
70 | if line==b'': |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
71 | |
7 | 72 | logging.debug('Borg stderr pipe EOF?') |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
73 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
74 | return None |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
75 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
76 | try: |
6 | 77 | res=json.loads(line) |
78 | if 'type' not in res: | |
79 | res['type']='UNKNOWN' | |
80 | return res | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
81 | except: |
7 | 82 | logging.debug('JSON parse failed on: "%s"' % line) |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
83 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
84 | errmsg=line |
7 | 85 | for line in iter(stream.readline, b''): |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
86 | errmsg=errmsg+line |
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
87 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
88 | return {'type': 'unparsed_error', 'message': str(errmsg)} |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
89 | |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
90 | def terminate(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
91 | self.proc.terminate() |
0 | 92 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
93 | def wait(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
94 | return self.proc.wait() is not None |
0 | 95 | |
4
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
96 | def has_terminated(self): |
d72c4844e791
Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
97 | return self.proc.poll() is not None |
0 | 98 |