4 |
4 |
5 import json |
5 import json |
6 import logging |
6 import logging |
7 import os |
7 import os |
8 import borgend |
8 import borgend |
|
9 from config import settings |
9 from subprocess import Popen, PIPE |
10 from subprocess import Popen, PIPE |
10 from config import settings, arglistify |
|
11 |
11 |
12 logger=borgend.logger.getChild(__name__) |
12 logger=borgend.logger.getChild(__name__) |
13 |
13 |
14 necessary_opts=['--log-json', '--progress'] |
14 necessary_opts=['--log-json', '--progress'] |
15 |
15 |
17 'create': ['--json'], |
17 'create': ['--json'], |
18 'info': ['--json'], |
18 'info': ['--json'], |
19 'list': ['--json'], |
19 'list': ['--json'], |
20 } |
20 } |
21 |
21 |
|
22 # Conversion of config into command line |
|
23 def arglistify(args): |
|
24 flatten=lambda l: [item for sublist in l for item in sublist] |
|
25 if args is None: |
|
26 return [] |
|
27 else: |
|
28 return flatten([['--' + key, str(d[key])] for d in args for key in d]) |
|
29 |
22 class BorgInstance: |
30 class BorgInstance: |
23 def __init__(self, operation, archive_or_repository, args, argsl): |
31 def __init__(self, operation, archive_or_repository, |
|
32 common_params, op_params, paths): |
24 self.operation=operation; |
33 self.operation=operation; |
25 self.args=args; |
|
26 self.archive_or_repository=archive_or_repository; |
34 self.archive_or_repository=archive_or_repository; |
27 self.argsl=argsl; |
35 self.common_params=common_params |
|
36 self.op_params=op_params |
|
37 self.paths=paths |
28 |
38 |
29 def construct_cmdline(self): |
39 def construct_cmdline(self): |
30 cmd=([settings['borg']['executable']]+necessary_opts+ |
40 cmd=([settings['borg']['executable']]+necessary_opts+ |
31 arglistify(settings['borg']['common_parameters'])+ |
41 arglistify(self.common_params)+ |
32 [self.operation]) |
42 [self.operation]) |
33 tmp1=self.operation+'_parameters' |
|
34 if tmp1 in settings['borg']: |
|
35 cmd=cmd+arglistify(settings['borg'][tmp1]) |
|
36 |
43 |
37 if self.operation in necessary_opts_for: |
44 if self.operation in necessary_opts_for: |
38 cmd=cmd+necessary_opts_for[self.operation] |
45 cmd=cmd+necessary_opts_for[self.operation] |
39 |
46 |
40 return cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl |
47 return (cmd+arglistify(self.op_params) |
|
48 +[self.archive_or_repository]+self.paths) |
41 |
49 |
42 def launch(self, passphrase=None): |
50 def launch(self, passphrase=None): |
43 cmd=self.construct_cmdline() |
51 cmd=self.construct_cmdline() |
44 |
52 |
45 logger.info('Launching ' + str(cmd)) |
53 logger.info('Launching ' + str(cmd)) |