diff options
author | Daniel Narvaez <dwnarvaez@gmail.com> | 2012-12-25 18:20:39 (GMT) |
---|---|---|
committer | Daniel Narvaez <dwnarvaez@gmail.com> | 2012-12-25 18:20:39 (GMT) |
commit | 69d077f81fd00cb9b462030ffb6b8cc8cd9b25c4 (patch) | |
tree | 1ae3272ff66abe1ade6e29ea2ca98caab598b6aa /devbot/state.py | |
parent | d2a05dc3eef299f41988c1b54be43ee62274c6e3 (diff) | |
parent | c05e755ced4ce4d528ffc5b34ddb44892e2f938f (diff) |
Merge branch 'testing'
Diffstat (limited to 'devbot/state.py')
-rw-r--r-- | devbot/state.py | 104 |
1 files changed, 74 insertions, 30 deletions
diff --git a/devbot/state.py b/devbot/state.py index 258c594..b44652f 100644 --- a/devbot/state.py +++ b/devbot/state.py @@ -1,50 +1,93 @@ +import hashlib import os import json from devbot import config +from devbot import git -_state = None +_BUILT_MODULES = "builtmodules" +_FULL_BUILD = "fullbuild" +_SYSTEM_CHECK = "syscheck" -def _get_state_path(): - return os.path.join(config.build_state_dir, "state.json") +def _get_state_path(name): + return os.path.join(config.build_state_dir, "%s.json" % name) -def _get_state(): - global _state +def _load_state(name, default=None): + state = default - if _state is not None: - return _state + try: + with open(_get_state_path(name)) as f: + state = json.load(f) + except IOError: + pass + + return state + +def _save_state(name, state): + with open(_get_state_path(name), "w+") as f: + json.dump(state, f, indent=4) + f.write('\n') + +def _get_diff_hash(git_module): + diff = git_module.diff().strip() + if diff: + return hashlib.sha256(diff).hexdigest() + else: + return None - state_path = _get_state_path() - if os.path.exists(state_path): - _state = json.load(open(state_path)) +def _get_root_commit_id(): + git_module = git.get_root_module() + if git_module: + commit_id = git_module.get_commit_id() else: - _state = { "built_modules": {} } + commit_id = "snapshot" + + return commit_id + +def built_module_touch(module): + git_module = module.get_git_module() + built_modules = _load_state(_BUILT_MODULES, {}) + + info = {"commit": git_module.get_commit_id(), + "diff_hash": _get_diff_hash(git_module)} + built_modules[module.name] = info + + _save_state(_BUILT_MODULES, built_modules) + +def built_module_is_unchanged(module): + git_module = module.get_git_module() + built_modules = _load_state(_BUILT_MODULES, {}) + if module.name not in built_modules: + return False - return _state + info = built_modules[module.name] -def _state_changed(): - json.dump(_state, open(_get_state_path(), "w+")) + return info["diff_hash"] == _get_diff_hash(git_module) and \ + info["commit"] == git_module.get_commit_id() -def touch_built_commit_id(module): - _get_state()["built_modules"][module.name] = module.get_commit_id() - _state_changed() +def system_check_is_unchanged(): + system_check = _load_state(_SYSTEM_CHECK) + if not system_check: + return False -def remove_built_commit_id(module): - state = _get_state() + return system_check["commit"] == _get_root_commit_id() - if module.name in state["built_modules"]: - del state["built_modules"][module.name] - _state_changed() +def system_check_touch(): + system_check = _load_state(_SYSTEM_CHECK, {}) + system_check["commit"] = _get_root_commit_id() + _save_state(_SYSTEM_CHECK, system_check) -def get_built_commit_id(module): - return _get_state()["built_modules"].get(module.name, None) +def full_build_is_required(): + full_build = _load_state(_FULL_BUILD) + if not full_build: + return True -def get_last_system_check(): - return _get_state().get("last_system_check", None) + return not (full_build["last"] == config.get_full_build()) -def touch_last_system_check(): - _get_state()["last_system_check"] = config.get_commit_id() - _state_changed() +def full_build_touch(): + full_build = _load_state(_FULL_BUILD, {}) + full_build["last"] = config.get_full_build() + _save_state(_FULL_BUILD, full_build) def clean(): _state = None @@ -52,6 +95,7 @@ def clean(): print "Deleting state" try: - os.unlink(_get_state_path()) + for name in _BUILT_MODULES, _SYSTEM_CHECK, _FULL_BUILD: + os.unlink(_get_state_path(name)) except OSError: pass |