borgend/instance.py

changeset 97
96d5adbe0205
parent 95
41bd7025532f
child 120
109eaddc16e1
equal deleted inserted replaced
96:de8ac6c470d8 97:96d5adbe0205
17 17
18 necessary_opts_for={ 18 necessary_opts_for={
19 'create': ['--json'], 19 'create': ['--json'],
20 'info': ['--json'], 20 'info': ['--json'],
21 'list': ['--json'], 21 'list': ['--json'],
22 'prune': ['--list'],
22 } 23 }
23 24
24 # Conversion of config into command line 25 # Conversion of config into command line
25 def arglistify(args): 26 def arglistify(args):
26 flatten=lambda l: [item for sublist in l for item in sublist] 27 flatten=lambda l: [item for sublist in l for item in sublist]
27 if args is None: 28 if args is None:
28 return [] 29 return []
29 else: 30 else:
30 return flatten([['--' + key, str(d[key])] for d in args for key in d]) 31 # Insert --key=str(value) for 'key: value' in the config.
32 # Boolean values are handled different, since borg does not take
33 # --key=true type of arguments. If the value is true --key is inserted,
34 # otherwise not.
35 def mkarg(key, value):
36 if isinstance(value, bool):
37 if value:
38 return ['--' + key]
39 else:
40 return []
41 else:
42 return ['--' + key, str(value)]
43 return flatten([mkarg(key, d[key]) for d in args for key in d])
31 44
32 class BorgInstance: 45 class BorgInstance:
33 def __init__(self, operation, archive_or_repository, 46 def __init__(self, operation, archive_or_repository,
34 common_params, op_params, paths): 47 common_params, op_params, paths):
35 self.operation=operation; 48 self.operation=operation;
88 return None 101 return None
89 102
90 try: 103 try:
91 return json.loads(line.decode()) 104 return json.loads(line.decode())
92 except Exception: 105 except Exception:
93 logger.exception('JSON parse failed on: %s' % str(line)) 106 logger.exception('JSON parse failed on stdout: %s' % str(line))
94 return None 107 return None
95 108
96 def read_log(self): 109 def read_log(self):
97 stream=self.proc.stderr 110 stream=self.proc.stderr
98 try: 111 try:
115 res=json.loads(line.decode()) 128 res=json.loads(line.decode())
116 if 'type' not in res: 129 if 'type' not in res:
117 res['type']='UNKNOWN' 130 res['type']='UNKNOWN'
118 return res 131 return res
119 except: 132 except:
120 logger.exception('JSON parse failed on: %s' % str(line)) 133 logger.exception('JSON parse failed on stderr: %s' % str(line))
121 134
122 errmsg=line 135 errmsg=line
123 for line in iter(stream.readline, b''): 136 for line in iter(stream.readline, b''):
124 errmsg=errmsg+line 137 errmsg=errmsg+line
125 138

mercurial