borgend/instance.py

Mon, 20 Apr 2020 13:21:02 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 20 Apr 2020 13:21:02 -0500
changeset 127
9badb0fb06eb
parent 120
109eaddc16e1
permissions
-rw-r--r--

LICENSE update; option (c).

0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
1 #
89
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
2 # Borgend by Tuomo Valkonen, 2018
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
3 #
51cc2e25af38 Added author information headers and content information to source files
Tuomo Valkonen <tuomov@iki.fi>
parents: 87
diff changeset
4 # This file implements a Borg launching interface.
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
5 #
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
6
80
a409242121d5 Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 79
diff changeset
7 import os
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
8 import json
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
9 import logging
2
e343594c0014 basic config processing
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
10 from subprocess import Popen, PIPE
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
11
80
a409242121d5 Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 79
diff changeset
12 from .config import settings
a409242121d5 Better package-like organisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 79
diff changeset
13
86
2fe66644c50d Can use logging.getLogger directly now after proper packageisation
Tuomo Valkonen <tuomov@iki.fi>
parents: 80
diff changeset
14 logger=logging.getLogger(__name__)
31
b4b4bb7a2ec5 More logging detail
Tuomo Valkonen <tuomov@iki.fi>
parents: 29
diff changeset
15
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
16 necessary_opts=['--log-json', '--progress']
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
17
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
18 necessary_opts_for={
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
19 'create': ['--json'],
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
20 'info': ['--json'],
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
21 'list': ['--json'],
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
22 'prune': ['--list'],
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
23 }
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
24
74
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
25 # Conversion of config into command line
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
26 def arglistify(args):
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
27 flatten=lambda l: [item for sublist in l for item in sublist]
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
28 if args is None:
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
29 return []
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
30 else:
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
31 # Insert --key=str(value) for 'key: value' in the config.
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
32 # Boolean values are handled different, since borg does not take
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
33 # --key=true type of arguments. If the value is true --key is inserted,
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
34 # otherwise not.
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
35 def mkarg(key, value):
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
36 if isinstance(value, bool):
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
37 if value:
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
38 return ['--' + key]
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
39 else:
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
40 return []
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
41 else:
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
42 return ['--' + key, str(value)]
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
43 return flatten([mkarg(key, d[key]) for d in args for key in d])
74
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
44
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 class BorgInstance:
74
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
46 def __init__(self, operation, archive_or_repository,
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
47 common_params, op_params, paths):
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 self.operation=operation;
3
4cad934aa9ce Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
49 self.archive_or_repository=archive_or_repository;
74
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
50 self.common_params=common_params
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
51 self.op_params=op_params
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
52 self.paths=paths
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
53 self.proc=None
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
54
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
55 def construct_cmdline(self):
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
56 cmd=([settings['borg']['executable']]+necessary_opts+
74
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
57 arglistify(self.common_params)+
2
e343594c0014 basic config processing
Tuomo Valkonen <tuomov@iki.fi>
parents: 1
diff changeset
58 [self.operation])
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
59
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
60 if self.operation in necessary_opts_for:
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
61 cmd=cmd+necessary_opts_for[self.operation]
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
62
74
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
63 return (cmd+arglistify(self.op_params)
4f56142e7497 Separated repository configuration form backup configuration;
Tuomo Valkonen <tuomov@iki.fi>
parents: 34
diff changeset
64 +[self.archive_or_repository]+self.paths)
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
65
21
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
66 def launch(self, passphrase=None):
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
67 cmd=self.construct_cmdline()
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
68
31
b4b4bb7a2ec5 More logging detail
Tuomo Valkonen <tuomov@iki.fi>
parents: 29
diff changeset
69 logger.info('Launching ' + str(cmd))
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
70
32
06fc14211ba9 Error handling improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 31
diff changeset
71 # Set passphrase if not, or set to empty if not known, so borg
06fc14211ba9 Error handling improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 31
diff changeset
72 # won't hang waiting for it, which seems to happen even if we
06fc14211ba9 Error handling improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 31
diff changeset
73 # close stdin.
29
9dd2e5d57a7c Something weird going on
Tuomo Valkonen <tuomov@iki.fi>
parents: 24
diff changeset
74 env=os.environ.copy()
32
06fc14211ba9 Error handling improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 31
diff changeset
75 env['BORG_PASSPHRASE']=passphrase or ''
20
fdfbe5d7b677 Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents: 12
diff changeset
76
24
94d58d514d69 Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents: 23
diff changeset
77 # Workaround: if launched is a standalone app created with py2app,
94d58d514d69 Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents: 23
diff changeset
78 # borg will fail unless Python environment is reset.
94d58d514d69 Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents: 23
diff changeset
79 # TODO: Of course, this will fail if the system needs the variables
94d58d514d69 Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents: 23
diff changeset
80 # PYTHONPATH or PYTHONHOME set to certain values.
29
9dd2e5d57a7c Something weird going on
Tuomo Valkonen <tuomov@iki.fi>
parents: 24
diff changeset
81 if '_PY2APP_LAUNCHED_' in env:
9dd2e5d57a7c Something weird going on
Tuomo Valkonen <tuomov@iki.fi>
parents: 24
diff changeset
82 val=env['_PY2APP_LAUNCHED_']
9dd2e5d57a7c Something weird going on
Tuomo Valkonen <tuomov@iki.fi>
parents: 24
diff changeset
83 if val=='1':
120
109eaddc16e1 py2app is a waste of my life.
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
84 if 'PYTHONPATH' in env:
109eaddc16e1 py2app is a waste of my life.
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
85 del env['PYTHONPATH']
109eaddc16e1 py2app is a waste of my life.
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
86 if 'PYTHONHOME' in env:
109eaddc16e1 py2app is a waste of my life.
Tuomo Valkonen <tuomov@iki.fi>
parents: 97
diff changeset
87 del env['PYTHONHOME']
24
94d58d514d69 Workaround to PYTHONPATH and PYTHONHOME being messed up by py2app.
Tuomo Valkonen <tuomov@iki.fi>
parents: 23
diff changeset
88
20
fdfbe5d7b677 Keychain support and random fixes
Tuomo Valkonen <tuomov@iki.fi>
parents: 12
diff changeset
89 self.proc=Popen(cmd, env=env, stdout=PIPE, stderr=PIPE, stdin=PIPE)
12
16a8c63344c0 Fixed retry scheduling
Tuomo Valkonen <tuomov@iki.fi>
parents: 10
diff changeset
90
21
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
91 # We don't do passphrase input etc.
12
16a8c63344c0 Fixed retry scheduling
Tuomo Valkonen <tuomov@iki.fi>
parents: 10
diff changeset
92 self.proc.stdin.close()
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
93
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
94 def read_result(self):
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
95 stream=self.proc.stdout
95
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
96 try:
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
97 line=stream.read(-1)
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
98 except Exception:
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
99 logger.exception('Borg stdout pipe read failed')
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
100
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
101 if line==b'':
95
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
102 #logger.debug('Borg stdout pipe EOF?')
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
103 return None
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
104
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
105 try:
95
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
106 return json.loads(line.decode())
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
107 except Exception:
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
108 logger.exception('JSON parse failed on stdout: %s' % str(line))
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
109 return None
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
110
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
111 def read_log(self):
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
112 stream=self.proc.stderr
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
113 try:
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
114 line=stream.readline()
95
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
115 except Exception:
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
116 logger.exception('Pipe stderr pipe read failed')
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
117
21
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
118 return {'type': 'log_message',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
119 'levelname': 'CRITICAL',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
120 'name': 'borgend.instance.BorgInstance',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
121 'msgid': 'Borgend.Exception',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
122 'message': err}
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
123
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
124 if line==b'':
95
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
125 #logger.debug('Borg stderr pipe EOF?')
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
126 return None
3
4cad934aa9ce Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
127
95
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
128
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
129 try:
95
41bd7025532f Fixes to borg output processing after testing on a Linux system
Tuomo Valkonen <tuomov@iki.fi>
parents: 89
diff changeset
130 res=json.loads(line.decode())
6
46c89e5a219f subprocess improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 4
diff changeset
131 if 'type' not in res:
46c89e5a219f subprocess improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 4
diff changeset
132 res['type']='UNKNOWN'
46c89e5a219f subprocess improvements
Tuomo Valkonen <tuomov@iki.fi>
parents: 4
diff changeset
133 return res
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
134 except:
97
96d5adbe0205 Pruning support
Tuomo Valkonen <tuomov@iki.fi>
parents: 95
diff changeset
135 logger.exception('JSON parse failed on stderr: %s' % str(line))
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
136
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
137 errmsg=line
7
e189d4a6cb8c Also listen to stdout
Tuomo Valkonen <tuomov@iki.fi>
parents: 6
diff changeset
138 for line in iter(stream.readline, b''):
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
139 errmsg=errmsg+line
3
4cad934aa9ce Can launch borg now; output not yet processed
Tuomo Valkonen <tuomov@iki.fi>
parents: 2
diff changeset
140
21
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
141 return {'type': 'log_message',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
142 'levelname': 'ERROR',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
143 'name': 'borgend.instance.BorgInstance',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
144 'msgid': 'Borgend.JSONFail',
c36e549a7f12 Errors as rumps notifications
Tuomo Valkonen <tuomov@iki.fi>
parents: 20
diff changeset
145 'message': str(errmsg)}
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
146
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
147 def terminate(self):
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
148 if self.proc:
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
149 self.proc.terminate()
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
151 # Returns True if has terminated
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
152 def wait(self, timeout=None):
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
153 if self.proc:
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
154 return self.proc.wait(timeout=timeout) is not None
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
155 else:
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
156 return True
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
157
4
d72c4844e791 Better borg output processing and some logging
Tuomo Valkonen <tuomov@iki.fi>
parents: 3
diff changeset
158 def has_terminated(self):
87
a214d475aa28 Better recovery from errors; fixes to potential race conditions in scheduler and repository queue
Tuomo Valkonen <tuomov@iki.fi>
parents: 86
diff changeset
159 return not self.proc or (self.proc.poll() is not None)
0
f5aecaad0bcf Some rough drafting
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160

mercurial