diff options
author | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2012-10-01 17:24:51 +0100 |
---|---|---|
committer | Daniel Silverstone <daniel.silverstone@codethink.co.uk> | 2012-10-01 17:24:51 +0100 |
commit | 6a51c368a9cf0a39bd5c1f5edd56eeb9c3d73de2 (patch) | |
tree | 1d27f0a4aa15a7ed15b6ed4fbf7ee95c819fff2f | |
parent | c82b611fef420479a19e11cd59e6b4f8c040de0b (diff) | |
download | lorry-controller-6a51c368a9cf0a39bd5c1f5edd56eeb9c3d73de2.tar.gz |
Basic working state manager
-rwxr-xr-x | lorry-controller | 9 | ||||
-rw-r--r-- | lorrycontroller/__init__.py | 1 | ||||
-rw-r--r-- | lorrycontroller/workingstate.py | 37 |
3 files changed, 44 insertions, 3 deletions
diff --git a/lorry-controller b/lorry-controller index 1b2fb74..10bad19 100755 --- a/lorry-controller +++ b/lorry-controller @@ -12,6 +12,7 @@ defaults = { } from lorrycontroller.confparser import LorryControllerConfig +from lorrycontroller.workingstate import WorkingStateManager class LorryController(cliapp.Application): @@ -44,9 +45,11 @@ class LorryController(cliapp.Application): logging.error("Unable to find lorry-controller.conf in git") raise SystemExit(4) - conf = LorryControllerConfig(self.settings, - 'git/lorry-controller.conf') - + self.conf = LorryControllerConfig(self.settings, + 'git/lorry-controller.conf') + + with WorkingStateManager(self) as mgr: + pass def rungit(self, args): self.runcmd(['git']+args, cwd=os.path.join(self.settings['work-area'], diff --git a/lorrycontroller/__init__.py b/lorrycontroller/__init__.py index 3c29d56..f70b747 100644 --- a/lorrycontroller/__init__.py +++ b/lorrycontroller/__init__.py @@ -2,3 +2,4 @@ # import confparser +import workingstate diff --git a/lorrycontroller/workingstate.py b/lorrycontroller/workingstate.py new file mode 100644 index 0000000..39d0065 --- /dev/null +++ b/lorrycontroller/workingstate.py @@ -0,0 +1,37 @@ +# Copyright (C) 2012 Codethink Limited +# + +import json +import os +import logging + +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): + self.save_state() + + 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") + |