instance.py

changeset 4
d72c4844e791
parent 3
4cad934aa9ce
child 6
46c89e5a219f
--- a/instance.py	Fri Jan 19 14:42:27 2018 +0000
+++ b/instance.py	Fri Jan 19 15:41:45 2018 +0000
@@ -3,11 +3,13 @@
 #
 
 import json
+import logging
 from subprocess import Popen, PIPE
 from config import settings, arglistify
 
+necessary_opts=['--log-json', '--progress']
+
 class BorgInstance:
-
     def __init__(self, operation, archive_or_repository, args, argsl):
         self.operation=operation;
         self.args=args;
@@ -15,48 +17,54 @@
         self.argsl=argsl;
 
     def construct_cmdline(self):
-        cmd=([settings['borg']['executable'], '--log-json']+
+        cmd=([settings['borg']['executable']]+necessary_opts+
              arglistify(settings['borg']['common_parameters'])+
              [self.operation])
         tmp1=self.operation+'_parameters'
         if tmp1 in settings['borg']:
             cmd=cmd+arglistify(settings['borg'][tmp1])
-        cmd=cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl
-        print(cmd)
-        return cmd
+
+        return cmd+arglistify(self.args)+[self.archive_or_repository]+self.argsl
 
     def launch(self):
-        # What to do with stderr? Is it needed?
-        self.proc=Popen(self.construct_cmdline(), stdout=PIPE, stderr=PIPE)
+        # Borg prints json to stderr, so pipe it
+        cmd=self.construct_cmdline()
+
+        logging.info('Launching ' + str(cmd))
+
+        self.proc=Popen(cmd, stderr=PIPE)
 
     def read(self):
-        line=self.proc.stdout.readline()
-        if line==b'':
+        try:
             line=self.proc.stderr.readline()
-            if line==b'':
-                return None
-            print('EEE'+str(line))
-            return 'error'
-        else:
-            print('###' + str(line))
-            # # What to do on error? stderr?
-            status=json.loads(line)
-            return status
+        except err:
+            logging.info('Pipe read failed: %s' % str(err))
+
+            return {'type': 'exception', 'exception': err}
+
+        if line==b'':
+
+            logging.info('Pipe EOF?')
+
+            return None
 
-    # for line in iter(stream.readline, b''):
-    #     status=json.loads(line)
-    #     queue.put({'identifier': instance.identifier,
-    #                'operation': instance.operation,
-    #                'status': status})
-    # out.close()
+        try:
+            return json.loads(line)
+        except:
+            logging.warning('JSON parse failed on: "%s"' % line)
+
+            errmsg=line
+            for line in iter(self.proc.stderr.readline, b''):
+                errmsg=errmsg+line
 
-    # def read_output():
-    #     try:
-    #         obj=self.queue.get_nowait()
-    #     except Empty:
-    #         obj=Empty
-    #     return obj
+            return {'type': 'unparsed_error', 'message': str(errmsg)}
+
+    def terminate(self):
+        self.proc.terminate()
 
-
+    def wait(self):
+        return self.proc.wait() is not None
 
+    def has_terminated(self):
+        return self.proc.poll() is not None
 

mercurial