From 0d0a7194226215703c5536fdae4993a4d4a81816 Mon Sep 17 00:00:00 2001 From: Daniel Narvaez Date: Tue, 25 Dec 2012 02:34:09 +0000 Subject: Refactor to put get_commit_id in the git module --- (limited to 'devbot') diff --git a/devbot/build.py b/devbot/build.py index 6a7f387..485d5bb 100644 --- a/devbot/build.py +++ b/devbot/build.py @@ -52,7 +52,7 @@ def build(): skipped = [] for module in modules[:]: - if state.check_built_module(module): + if state.built_module_is_unchanged(module): modules.pop(0) skipped.append(module.name) else: @@ -63,7 +63,7 @@ def build(): print "\n".join(skipped) for module in modules: - state.remove_built_module(module) + state.built_module_remove(module) for module in modules: if not _build_module(module, config.get_log_path("build")): @@ -221,7 +221,7 @@ def _build_module(module, log=None): except subprocess.CalledProcessError: return False - state.touch_built_module(module) + state.built_module_touch(module) return True diff --git a/devbot/config.py b/devbot/config.py index 93c82f7..dba6709 100644 --- a/devbot/config.py +++ b/devbot/config.py @@ -5,7 +5,6 @@ import pkgutil import tempfile from devbot import distro -from devbot import utils from devbot import plugins from devbot import git @@ -51,12 +50,6 @@ class Module: def get_build_dir(self): return os.path.join(get_build_dir(), self.name) - def get_commit_id(self, tag="HEAD"): - if not os.path.exists(self.get_source_dir()): - return None - - return utils.get_commit_id(self.get_source_dir()) - def get_git_module(self): return git.Module(path=get_source_dir(), name=self.name, remote=self.repo, branch=self.branch, tag=self.tag, @@ -77,13 +70,6 @@ def _ensure_dir(dir): if not os.path.exists(dir): os.mkdir(dir) -def get_commit_id(): - commit_id = utils.get_commit_id(config_dir) - if commit_id is None: - commit_id = "snapshot" - - return commit_id - def setup(**kwargs): _load_plugins() diff --git a/devbot/git.py b/devbot/git.py index 8f6699c..4a86bd6 100644 --- a/devbot/git.py +++ b/devbot/git.py @@ -2,6 +2,9 @@ import os import subprocess from devbot import command +from devbot import utils + +_root_path = None def _chdir(func): def wrapped(*args, **kwargs): @@ -76,6 +79,17 @@ class Module: return subprocess.check_output(["git", "diff"]) @_chdir + def is_valid(self): + result = subprocess.call(["git", "rev-parse", "HEAD"], + stdout=utils.devnull, + stderr=utils.devnull) + return result == 0 + + @_chdir + def get_commit_id(self): + return subprocess.check_output(["git", "rev-parse", "HEAD"]).strip() + + @_chdir def get_annotation(self, tag): # FIXME this is fragile, there must be a better way @@ -105,3 +119,18 @@ class Module: command.run(["git", "clean", "-fdx"]) return True + +def set_root_path(path): + global _root_path + _root_path = path + +def get_root_module(): + remote = "git://git.sugarlabs.org/sugar-build/sugar-build.git" + + module = Module(name=os.path.basename(_root_path), + remote=remote, + path=os.path.dirname(_root_path)) + if not module.is_valid(): + return None + + return module diff --git a/devbot/state.py b/devbot/state.py index ae7fafd..83bbe9a 100644 --- a/devbot/state.py +++ b/devbot/state.py @@ -3,6 +3,7 @@ import os import json from devbot import config +from devbot import git _BUILT_MODULES = "builtmodules" _SYSTEM_CHECK = "syscheck" @@ -33,24 +34,33 @@ def _get_diff_hash(git_module): else: return None -def touch_built_module(module): +def _get_root_commit_id(): + git_module = git.get_root_module() + if git_module: + commit_id = git_module.get_commit_id() + else: + 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": module.get_commit_id(), + 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 remove_built_module(module): +def built_module_remove(module): built_modules = _load_state(_BUILT_MODULES) if built_modules and module.name in built_modules: del built_modules[module.name] _save_state(_BUILT_MODULES, built_modules) -def check_built_module(module): +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: @@ -59,17 +69,18 @@ def check_built_module(module): info = built_modules[module.name] return info["diff_hash"] == _get_diff_hash(git_module) and \ - info["commit"] == module.get_commit_id() - -def get_last_system_check(): - system_check = _load_state(_SYSTEM_CHECK, {}) - return system_check.get("commit", None) + info["commit"] == git_module.get_commit_id() -def touch_last_system_check(): - system_check = _load_state(_SYSTEM_CHECK, {}) +def system_check_is_unchanged(): + system_check = _load_state(_SYSTEM_CHECK) + if not system_check: + return False - system_check["commit"] = config.get_commit_id() + return system_check["commit"] == _get_root_commit_id() +def system_check_touch(): + system_check = _load_state(_SYSTEM_CHECK, {}) + system_check["commit"] = _get_root_commit_id() _save_state(_SYSTEM_CHECK, system_check) def clean(): diff --git a/devbot/system.py b/devbot/system.py index 85e2349..bf4932e 100644 --- a/devbot/system.py +++ b/devbot/system.py @@ -4,6 +4,7 @@ import subprocess import sys from devbot import config +from devbot import git from devbot import distro from devbot import command from devbot import state @@ -156,7 +157,7 @@ def remove_packages(package_manager, packages): def check(remove=False, update=False, test=False, interactive=True, skip_if_unchanged=False): if skip_if_unchanged: - if config.get_commit_id() == state.get_last_system_check(): + if state.system_check_is_unchanged(): return package_manager = \ @@ -185,4 +186,4 @@ def check(remove=False, update=False, test=False, interactive=True, if remove: remove_packages(package_manager, packages) - state.touch_last_system_check() + state.system_check_touch() diff --git a/devbot/utils.py b/devbot/utils.py index 2864948..aed5de0 100644 --- a/devbot/utils.py +++ b/devbot/utils.py @@ -1,18 +1 @@ -import os -import subprocess - devnull = open("/dev/null", "w") - -def get_commit_id(dir): - orig_cwd = os.getcwd() - os.chdir(dir) - - try: - commit_id = subprocess.check_output(["git", "rev-parse", "HEAD"], - stderr=devnull).strip() - except subprocess.CalledProcessError: - commit_id = None - - os.chdir(orig_cwd) - - return commit_id -- cgit v0.9.1