instance.py

Fri, 19 Jan 2018 10:41:01 +0000

author
Tuomo Valkonen <tuomov@iki.fi>
date
Fri, 19 Jan 2018 10:41:01 +0000
changeset 2
e343594c0014
parent 1
4cdc9c1f6b28
child 3
4cad934aa9ce
permissions
-rw-r--r--

basic config processing

#
# Borgend borg launcher / processor
#

import json
from subprocess import Popen, PIPE
from config import settings, arglistify
from queue import Queue
from threading import Thread

def linereader(stream, instance, queue):
    # What to do on error?
    for line in iter(stream.readline, b''):
        status=json.loads(line)
        queue.put({'identifier': instance.identifier,
                   'operation': instance.operation,
                   'status': status})
    out.close()

class BorgInstance:

    def __init__(self, identifier, operation, args, archive, argsl):
        self.identifier=identifier;
        self.operation=operation;
        self.args=args;
        self.archive=archive;
        self.argsl=argsl;

    def construct_cmdline(self):
        cmd=([settings['borg']['executable'], '--log-json']+
             arglistify(settings['borg']['common_parameters'])+
             [self.operation])
        tmp1=self.operation+'_parameters'
        if tmp1 in settings['borg']:
            cmd=cmd+arglistify(settings['borg'][tmp1])
        return cmd+arglistify(self.args)+[self.archive]+self.argsl

    def launch(self, queue):
        # What to do with stderr? Is it needed?
        self.proc=Popen(self.construct_cmdline(), stdout=PIPE, stderr=PIPE)
        linereaderargs=(self.proc.stdout, self, queue)
        self.t=Thread(target=linereader, args=linereaderargs)
        t.daemon=True
        t.start()

    def read_output():
        try:
            obj=self.queue.get_nowait()
        except Empty:
            obj=Empty
        return obj



mercurial