subprocess improvements

Fri, 19 Jan 2018 16:53:13 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 19 Jan 2018 16:53:13 +0000
changeset 6
46c89e5a219f
parent 5
4c5514b2fa76
child 7
e189d4a6cb8c

subprocess improvements

backup.py file | annotate | diff | comparison | revisions
instance.py file | annotate | diff | comparison | revisions
--- a/backup.py	Fri Jan 19 16:00:36 2018 +0000
+++ b/backup.py	Fri Jan 19 16:53:13 2018 +0000
@@ -9,6 +9,20 @@
 from queue import Queue
 from threading import Thread, Lock
 
+loglevel_translation={
+    'CRITICAL': logging.CRITICAL,
+    'ERROR': logging.ERROR,
+    'WARNING': logging.WARNING,
+    'DEBUG': logging.DEBUG,
+    'INFO': logging.INFO
+}
+
+def translate_loglevel(x):
+    if x in loglevel_translation:
+        return loglevel_translation[x]
+    else:
+        return logging.ERROR
+
 class Backup:
 
     def __decode_config(self, cfg):
@@ -37,12 +51,16 @@
 
         self.paths=config.check_nonempty_list_of_strings(cfg, 'paths', 'Paths', self.loc)
 
-        self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters',
+        self.common_parameters=config.check_list_of_dicts(cfg, 'common_parameters',
                                                          'Borg parameters', self.loc,
                                                          default=[])
 
+        self.create_parameters=config.check_list_of_dicts(cfg, 'create_parameters',
+                                                         'Create parameters', self.loc,
+                                                         default=[])
+
         self.prune_parameters=config.check_list_of_dicts(cfg, 'prune_parameters',
-                                                         'Borg parameters', self.loc,
+                                                         'Prune parameters', self.loc,
                                                          default=[])
 
 
@@ -65,32 +83,43 @@
         assert(not_running)
 
     def __listener(self):
-        success=True
+        success=False
         for status in iter(self.borg_instance.read, None):
+            logging.info(str(status))
             t=status['type']
             if t=='progress_percent':
                 pass
             elif t=='archive_progress':
                 pass
             elif t=='progress_message':
-                # handle errors
-                pass
+                if 'finished' in status:
+                    logging.info('Borg subprocess finished succesfully')
+                    success=status['finished']
             elif t=='progress_percent':
+                # Temporary output
+                print('%d / %d', status['current'], status['total'])
                 pass
             elif t=='file_status':
                 pass
             elif t=='log_message':
-                pass
+                if 'levelname' not in status:
+                    status['levelname']='ERROR'
+                if 'message' not in status:
+                    status['message']='UNKNOWN'
+                if 'name' not in status:
+                    status['name']='borg'
+                logging.log(translate_loglevel(status['levelname']),
+                            status['name'] + ': ' + status['message'])
             elif t=='exception':
-                success=False
                 pass
             elif t=='unparsed_error':
-                success=False
                 pass
-            # What to do?
-            print(status)
+
+        logging.info('Waiting for borg subprocess to terminate')
 
-        logging.info('Borg subprocess finished; terminating listener thread')
+        self.borg_instance.wait()
+
+        logging.info('Borg subprocess terminated; terminating listener thread')
 
         with self.lock:
             if self.current_operation=='create':
@@ -125,12 +154,15 @@
                               self.archive_template)
 
         self.__launch(queue, 'create', archive,
-                      self.create_parameters, self.paths)
+                      self.common_parameters+self.create_parameters,
+                      self.paths)
 
     def prune(self, queue):
         self.__block_when_running()
         self.__launch(queue, 'prune', self.repository,
-                      [{'prefix': self.archive_prefix}] + self.prune_parameters)
+                      ([{'prefix': self.archive_prefix}] + 
+                       self.common_parameters +
+                       self.prune_parameters))
 
     # TODO: Decide exact (manual) abort mechanism. Perhaps two stages
     def abort(self):
--- a/instance.py	Fri Jan 19 16:00:36 2018 +0000
+++ b/instance.py	Fri Jan 19 16:53:13 2018 +0000
@@ -49,7 +49,10 @@
             return None
 
         try:
-            return json.loads(line)
+            res=json.loads(line)
+            if 'type' not in res:
+                res['type']='UNKNOWN'
+            return res
         except:
             logging.warning('JSON parse failed on: "%s"' % line)
 

mercurial