instance.py

changeset 31
b4b4bb7a2ec5
parent 29
9dd2e5d57a7c
child 32
06fc14211ba9
equal deleted inserted replaced
30:3dd525652dc8 31:b4b4bb7a2ec5
5 import json 5 import json
6 import logging 6 import logging
7 import os 7 import os
8 from subprocess import Popen, PIPE 8 from subprocess import Popen, PIPE
9 from config import settings, arglistify 9 from config import settings, arglistify
10
11 logger=logging.getLogger(__name__)
10 12
11 necessary_opts=['--log-json', '--progress'] 13 necessary_opts=['--log-json', '--progress']
12 14
13 necessary_opts_for={ 15 necessary_opts_for={
14 'create': ['--json'], 16 'create': ['--json'],
37 return cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl 39 return cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl
38 40
39 def launch(self, passphrase=None): 41 def launch(self, passphrase=None):
40 cmd=self.construct_cmdline() 42 cmd=self.construct_cmdline()
41 43
42 logging.info('Launching ' + str(cmd)) 44 logger.info('Launching ' + str(cmd))
43 45
44 env=os.environ.copy() 46 env=os.environ.copy()
45 if passphrase: 47 if passphrase:
46 env['BORG_PASSPHRASE']=passphrase 48 env['BORG_PASSPHRASE']=passphrase
47 49
62 64
63 def read_result(self): 65 def read_result(self):
64 stream=self.proc.stdout 66 stream=self.proc.stdout
65 line=stream.read(-1) 67 line=stream.read(-1)
66 if line==b'': 68 if line==b'':
67 logging.debug('Borg stdout pipe EOF?') 69 logger.debug('Borg stdout pipe EOF?')
68 return None 70 return None
69 71
70 try: 72 try:
71 return json.loads(line) 73 return json.loads(line)
72 except: 74 except:
73 logging.warning('JSON parse failed on: "%s"' % line) 75 logger.warning('JSON parse failed on: "%s"' % line)
74 return None 76 return None
75 77
76 def read_log(self): 78 def read_log(self):
77 stream=self.proc.stderr 79 stream=self.proc.stderr
78 try: 80 try:
79 line=stream.readline() 81 line=stream.readline()
80 except err: 82 except err:
81 logging.debug('Pipe read failed: %s' % str(err)) 83 logger.debug('Pipe read failed: %s' % str(err))
82 84
83 return {'type': 'log_message', 85 return {'type': 'log_message',
84 'levelname': 'CRITICAL', 86 'levelname': 'CRITICAL',
85 'name': 'borgend.instance.BorgInstance', 87 'name': 'borgend.instance.BorgInstance',
86 'msgid': 'Borgend.Exception', 88 'msgid': 'Borgend.Exception',
87 'message': err} 89 'message': err}
88 90
89 if line==b'': 91 if line==b'':
90 92
91 logging.debug('Borg stderr pipe EOF?') 93 logger.debug('Borg stderr pipe EOF?')
92 94
93 return None 95 return None
94 96
95 try: 97 try:
96 res=json.loads(line) 98 res=json.loads(line)
97 if 'type' not in res: 99 if 'type' not in res:
98 res['type']='UNKNOWN' 100 res['type']='UNKNOWN'
99 return res 101 return res
100 except: 102 except:
101 logging.debug('JSON parse failed on: "%s"' % str(line)) 103 logger.debug('JSON parse failed on: "%s"' % str(line))
102 104
103 errmsg=line 105 errmsg=line
104 for line in iter(stream.readline, b''): 106 for line in iter(stream.readline, b''):
105 errmsg=errmsg+line 107 errmsg=errmsg+line
106 108

mercurial