instance.py

changeset 3
4cad934aa9ce
parent 2
e343594c0014
child 4
d72c4844e791
equal deleted inserted replaced
2:e343594c0014 3:4cad934aa9ce
3 # 3 #
4 4
5 import json 5 import json
6 from subprocess import Popen, PIPE 6 from subprocess import Popen, PIPE
7 from config import settings, arglistify 7 from config import settings, arglistify
8 from queue import Queue
9 from threading import Thread
10
11 def linereader(stream, instance, queue):
12 # What to do on error?
13 for line in iter(stream.readline, b''):
14 status=json.loads(line)
15 queue.put({'identifier': instance.identifier,
16 'operation': instance.operation,
17 'status': status})
18 out.close()
19 8
20 class BorgInstance: 9 class BorgInstance:
21 10
22 def __init__(self, identifier, operation, args, archive, argsl): 11 def __init__(self, operation, archive_or_repository, args, argsl):
23 self.identifier=identifier;
24 self.operation=operation; 12 self.operation=operation;
25 self.args=args; 13 self.args=args;
26 self.archive=archive; 14 self.archive_or_repository=archive_or_repository;
27 self.argsl=argsl; 15 self.argsl=argsl;
28 16
29 def construct_cmdline(self): 17 def construct_cmdline(self):
30 cmd=([settings['borg']['executable'], '--log-json']+ 18 cmd=([settings['borg']['executable'], '--log-json']+
31 arglistify(settings['borg']['common_parameters'])+ 19 arglistify(settings['borg']['common_parameters'])+
32 [self.operation]) 20 [self.operation])
33 tmp1=self.operation+'_parameters' 21 tmp1=self.operation+'_parameters'
34 if tmp1 in settings['borg']: 22 if tmp1 in settings['borg']:
35 cmd=cmd+arglistify(settings['borg'][tmp1]) 23 cmd=cmd+arglistify(settings['borg'][tmp1])
36 return cmd+arglistify(self.args)+[self.archive]+self.argsl 24 cmd=cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl
25 print(cmd)
26 return cmd
37 27
38 def launch(self, queue): 28 def launch(self):
39 # What to do with stderr? Is it needed? 29 # What to do with stderr? Is it needed?
40 self.proc=Popen(self.construct_cmdline(), stdout=PIPE, stderr=PIPE) 30 self.proc=Popen(self.construct_cmdline(), stdout=PIPE, stderr=PIPE)
41 linereaderargs=(self.proc.stdout, self, queue)
42 self.t=Thread(target=linereader, args=linereaderargs)
43 t.daemon=True
44 t.start()
45 31
46 def read_output(): 32 def read(self):
47 try: 33 line=self.proc.stdout.readline()
48 obj=self.queue.get_nowait() 34 if line==b'':
49 except Empty: 35 line=self.proc.stderr.readline()
50 obj=Empty 36 if line==b'':
51 return obj 37 return None
38 print('EEE'+str(line))
39 return 'error'
40 else:
41 print('###' + str(line))
42 # # What to do on error? stderr?
43 status=json.loads(line)
44 return status
45
46 # for line in iter(stream.readline, b''):
47 # status=json.loads(line)
48 # queue.put({'identifier': instance.identifier,
49 # 'operation': instance.operation,
50 # 'status': status})
51 # out.close()
52
53 # def read_output():
54 # try:
55 # obj=self.queue.get_nowait()
56 # except Empty:
57 # obj=Empty
58 # return obj
52 59
53 60
54 61
55 62

mercurial