instance.py

changeset 2
e343594c0014
parent 1
4cdc9c1f6b28
child 3
4cad934aa9ce
equal deleted inserted replaced
1:4cdc9c1f6b28 2:e343594c0014
1 # 1 #
2 # Borgend borg launcher / processor 2 # Borgend borg launcher / processor
3 # 3 #
4 4
5 import json 5 import json
6 import subprocess 6 from subprocess import Popen, PIPE
7 import config 7 from config import settings, arglistify
8 import Queue 8 from queue import Queue
9 import Thread 9 from threading import Thread
10 10
11 def linereader(stream, instance, queue) 11 def linereader(stream, instance, queue):
12 # What to do on error? 12 # What to do on error?
13 for line in iter(stream.readline, b''): 13 for line in iter(stream.readline, b''):
14 status=json.loads(line) 14 status=json.loads(line)
15 queue.put({'identifier': instance.identifier, 15 queue.put({'identifier': instance.identifier,
16 'operation': instance.operation, 16 'operation': instance.operation,
17 'status': status}) 17 'status': status})
18 out.close() 18 out.close()
19 19
20 class BorgInstance: 20 class BorgInstance:
21 21
22 def __init__(self, identifier, operation, args): 22 def __init__(self, identifier, operation, args, archive, argsl):
23 self.identifier=identifier; 23 self.identifier=identifier;
24 self.operation=operation; 24 self.operation=operation;
25 self.args=args; 25 self.args=args;
26 self.archive=archive;
27 self.argsl=argsl;
26 28
27 def construct_cmdline(self): 29 def construct_cmdline(self):
28 ??? 30 cmd=([settings['borg']['executable'], '--log-json']+
31 arglistify(settings['borg']['common_parameters'])+
32 [self.operation])
33 tmp1=self.operation+'_parameters'
34 if tmp1 in settings['borg']:
35 cmd=cmd+arglistify(settings['borg'][tmp1])
36 return cmd+arglistify(self.args)+[self.archive]+self.argsl
29 37
30 def launch(self, queue): 38 def launch(self, queue):
31 # What to do with stderr? Is it needed? 39 # What to do with stderr? Is it needed?
32 self.proc=subprocess.Popen(self.construct_cmdline(), 40 self.proc=Popen(self.construct_cmdline(), stdout=PIPE, stderr=PIPE)
33 stdout=subprocess.PIPE,
34 stderr=subprocess.PIPE)
35 linereaderargs=(self.proc.stdout, self, queue) 41 linereaderargs=(self.proc.stdout, self, queue)
36 self.t=Thread(target=linereader, args=linereaderargs) 42 self.t=Thread(target=linereader, args=linereaderargs)
37 t.daemon=True 43 t.daemon=True
38 t.start() 44 t.start()
39 45

mercurial