Fri, 19 Jan 2018 10:41:01 +0000
basic config processing
| 0 | 1 | # |
| 2 | # Borgend borg launcher / processor | |
| 3 | # | |
| 4 | ||
| 5 | import json | |
| 2 | 6 | from subprocess import Popen, PIPE |
| 7 | from config import settings, arglistify | |
| 8 | from queue import Queue | |
| 9 | from threading import Thread | |
| 0 | 10 | |
| 2 | 11 | def linereader(stream, instance, queue): |
| 0 | 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 | ||
| 20 | class BorgInstance: | |
| 21 | ||
| 2 | 22 | def __init__(self, identifier, operation, args, archive, argsl): |
| 0 | 23 | self.identifier=identifier; |
| 24 | self.operation=operation; | |
| 25 | self.args=args; | |
| 2 | 26 | self.archive=archive; |
| 27 | self.argsl=argsl; | |
| 0 | 28 | |
| 29 | def construct_cmdline(self): | |
| 2 | 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 | |
| 0 | 37 | |
| 38 | def launch(self, queue): | |
| 39 | # What to do with stderr? Is it needed? | |
| 2 | 40 | self.proc=Popen(self.construct_cmdline(), stdout=PIPE, stderr=PIPE) |
| 0 | 41 | linereaderargs=(self.proc.stdout, self, queue) |
| 42 | self.t=Thread(target=linereader, args=linereaderargs) | |
| 43 | t.daemon=True | |
| 44 | t.start() | |
| 45 | ||
| 46 | def read_output(): | |
| 47 | try: | |
| 48 | obj=self.queue.get_nowait() | |
| 49 | except Empty: | |
| 50 | obj=Empty | |
| 51 | return obj | |
| 52 | ||
| 53 | ||
| 54 | ||
| 55 |