Fri, 19 Jan 2018 14:42:27 +0000
Can launch borg now; output not yet processed
| 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 | |
| 0 | 8 | |
| 9 | class BorgInstance: | |
| 10 | ||
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
11 | def __init__(self, operation, archive_or_repository, args, argsl): |
| 0 | 12 | self.operation=operation; |
| 13 | self.args=args; | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
14 | self.archive_or_repository=archive_or_repository; |
| 2 | 15 | self.argsl=argsl; |
| 0 | 16 | |
| 17 | def construct_cmdline(self): | |
| 2 | 18 | cmd=([settings['borg']['executable'], '--log-json']+ |
| 19 | arglistify(settings['borg']['common_parameters'])+ | |
| 20 | [self.operation]) | |
| 21 | tmp1=self.operation+'_parameters' | |
| 22 | if tmp1 in settings['borg']: | |
| 23 | cmd=cmd+arglistify(settings['borg'][tmp1]) | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
24 | cmd=cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
25 | print(cmd) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
26 | return cmd |
| 0 | 27 | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
28 | def launch(self): |
| 0 | 29 | # What to do with stderr? Is it needed? |
| 2 | 30 | self.proc=Popen(self.construct_cmdline(), stdout=PIPE, stderr=PIPE) |
| 0 | 31 | |
|
3
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
32 | def read(self): |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
33 | line=self.proc.stdout.readline() |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
34 | if line==b'': |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
35 | line=self.proc.stderr.readline() |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
36 | if line==b'': |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
37 | return None |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
38 | print('EEE'+str(line)) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
39 | return 'error' |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
40 | else: |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
41 | print('###' + str(line)) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
42 | # # What to do on error? stderr? |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
43 | status=json.loads(line) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
44 | return status |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
45 | |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
46 | # for line in iter(stream.readline, b''): |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
47 | # status=json.loads(line) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
48 | # queue.put({'identifier': instance.identifier, |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
49 | # 'operation': instance.operation, |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
50 | # 'status': status}) |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
51 | # out.close() |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
52 | |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
53 | # def read_output(): |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
54 | # try: |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
55 | # obj=self.queue.get_nowait() |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
56 | # except Empty: |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
57 | # obj=Empty |
|
4cad934aa9ce
Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
58 | # return obj |
| 0 | 59 | |
| 60 | ||
| 61 | ||
| 62 |