summaryrefslogtreecommitdiff
path: root/lorrycontroller/workingstate.py
blob: 2064005f20ae6b26492fe752017321153c38d9e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (C) 2012  Codethink Limited
# 

import json
import os
import logging
import string

class LorryFileRunner(object):
    def __init__(self, mgr, lorryname):
        self.mgr = mgr
        self.lorryname = lorryname
        self.lorryfile = os.path.join(self.mgr.workdir,
                                      self._esc(lorryname) + ".lorry")

    def _esc(self, name):
        valid_chars = string.digits + string.letters + '%_'
        transl = lambda x: x if x in valid_chars else '_'
        return ''.join([transl(x) for x in name])

    def __enter__(self):
        lorry_obj = { self.lorryname:
                      self.mgr.lorry_state[self.lorryname]['lorry'] }
        with open(self.lorryfile, "w") as fh:
            json.dump(lorry_obj, fh)
            fh.write("\n")
        return self

    def __exit__(self, exctype, excvalue, exctraceback):
        os.unlink(self.lorryfile)

    def run_lorry(self, *args):
        cmdargs = list(args)
        cmdargs.append(self.lorryfile)
        exit, out, err = self.mgr.app.maybe_runcmd(cmdargs)
        if exit == 0:
            logging.info("Lorry of %s succeeded: %s" % (self.lorryname, out))
        else:
            logging.warn("Lorry of %s failed: %s" % (self.lorryname, err))

class WorkingStateManager(object):
    '''Manage the working state of lorry-controller'''

    def __init__(self, app):
        self.app = app
        self.workdir = os.path.join(self.app.settings['work-area'], 'work')

    def __enter__(self):
        self._load_state()
        return self

    def __exit__(self, exctype, excvalue, exctraceback):
        if not self.app.settings['dry-run']:
            self.save_state()
        else:
            logging.debug("DRY-RUN: Not saving state again")

    def _load_state(self):
        self.lorry_state_file = os.path.join(self.workdir,
                                             "last-lorry-state.json")
        if os.path.exists(self.lorry_state_file):
            logging.debug("Loading state file: %s" % self.lorry_state_file)
            with open(self.lorry_state_file, "r") as fh:
                self.lorry_state = json.load(fh)
        else:
            self.lorry_state = dict()

    def save_state(self):
        logging.debug("Serialising state: %s" % self.lorry_state_file)
        with open(self.lorry_state_file, "w") as fh:
            json.dump(self.lorry_state, fh, sort_keys=True, indent=4)
            fh.write("\n")

    def runner(self, lorryname):
        return LorryFileRunner(self, lorryname)