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