instance.py

changeset 7
e189d4a6cb8c
parent 6
46c89e5a219f
child 10
76dbfb06eba0
--- a/instance.py	Fri Jan 19 16:53:13 2018 +0000
+++ b/instance.py	Sat Jan 20 14:04:51 2018 +0000
@@ -9,6 +9,12 @@
 
 necessary_opts=['--log-json', '--progress']
 
+necessary_opts_for={
+    'create': ['--json'],
+    'info': ['--json'],
+    'list': ['--json'],
+}
+
 class BorgInstance:
     def __init__(self, operation, archive_or_repository, args, argsl):
         self.operation=operation;
@@ -24,27 +30,43 @@
         if tmp1 in settings['borg']:
             cmd=cmd+arglistify(settings['borg'][tmp1])
 
+        if self.operation in necessary_opts_for:
+            cmd=cmd+necessary_opts_for[self.operation]
+
         return cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl
 
     def launch(self):
-        # Borg prints json to stderr, so pipe it
         cmd=self.construct_cmdline()
 
         logging.info('Launching ' + str(cmd))
 
-        self.proc=Popen(cmd, stderr=PIPE)
+        self.proc=Popen(cmd, stdout=PIPE, stderr=PIPE)
 
-    def read(self):
+    def read_result(self):
+        stream=self.proc.stdout
+        line=stream.read(-1)
+        if line==b'':
+            logging.debug('Borg stdout pipe EOF?')
+            return None
+
         try:
-            line=self.proc.stderr.readline()
+            return json.loads(line)
+        except:
+            logging.warning('JSON parse failed on: "%s"' % line)
+            return None
+
+    def read_log(self):
+        stream=self.proc.stderr
+        try:
+            line=stream.readline()
         except err:
-            logging.info('Pipe read failed: %s' % str(err))
+            logging.debug('Pipe read failed: %s' % str(err))
 
             return {'type': 'exception', 'exception': err}
 
         if line==b'':
 
-            logging.info('Pipe EOF?')
+            logging.debug('Borg stderr pipe EOF?')
 
             return None
 
@@ -54,10 +76,10 @@
                 res['type']='UNKNOWN'
             return res
         except:
-            logging.warning('JSON parse failed on: "%s"' % line)
+            logging.debug('JSON parse failed on: "%s"' % line)
 
             errmsg=line
-            for line in iter(self.proc.stderr.readline, b''):
+            for line in iter(stream.readline, b''):
                 errmsg=errmsg+line
 
             return {'type': 'unparsed_error', 'message': str(errmsg)}

mercurial