diff options
author | Andy Doan <andy.doan@linaro.org> | 2015-06-16 12:45:35 -0500 |
---|---|---|
committer | Andy Doan <andy.doan@linaro.org> | 2015-07-07 11:07:45 -0500 |
commit | 6135faba8a3366adf623d92802c59f74e226cc85 (patch) | |
tree | 10da4cf1a8b968c666b1459e8f57f00c620fd562 /update_commited_patches.py | |
parent | 9e8a6eb780b7f0d95edc29d5940314f56621d415 (diff) |
move repository logic to its own module
Looking at update-committed-patches in the linaro-patchmetrics file and
seeing where this is headed, I think now is the time to split
functionality up so its easier to manage.
This also improves the logic of managing the last analyzed commit by
introducing a process_unchecked_commits method. The result of the change
also simplifies/improves some of the logic needed for unit-testing.
Change-Id: I1e28f3fe3b27e1afd414939d28acc5eb5f6387cf
Diffstat (limited to 'update_commited_patches.py')
-rwxr-xr-x | update_commited_patches.py | 57 |
1 files changed, 8 insertions, 49 deletions
diff --git a/update_commited_patches.py b/update_commited_patches.py index a342028..5fba8a4 100755 --- a/update_commited_patches.py +++ b/update_commited_patches.py @@ -8,6 +8,8 @@ django_setup() # must be called to get sys.path and django settings in place from patchwork.models import Patch, Project, State +import gitrepo + log = get_logger('update_commited_patches') _here = os.path.abspath(os.path.dirname(__file__)) @@ -18,45 +20,10 @@ def _assert_repo_dir(path): os.mkdir(path) -def _clone_repo(repo_dir, giturl, linkname): - args = ['git', 'clone', giturl, linkname] - subprocess.check_call(args, cwd=repo_dir) - - -def _pull(gitdir): - timeout = str(getattr(settings, 'REPO_TIMEOUT', 120)) - subprocess.check_call(['timeout', timeout, 'git', 'fetch'], cwd=gitdir) - commit = subprocess.check_output( - ['git', 'merge-base', 'origin', 'master'], cwd=gitdir) - subprocess.check_call( - ['git', 'reset', '--hard', commit.strip()], cwd=gitdir) - subprocess.check_call(['timeout', timeout, 'git', 'pull'], cwd=gitdir) - - -def _commits_to_check(gitdir): - start_at = os.path.join(gitdir, 'patchwork-last-commit') - if not os.path.exists(start_at): - start_at = 'HEAD~100' - else: - with open(start_at) as f: - start_at = f.read().strip() - - args = ['git', 'rev-list', '--reverse', start_at + '..HEAD'] - with open('/dev/null', 'w') as f: - rc = subprocess.call( - ['git', 'show', start_at], cwd=gitdir, stdout=f, stderr=f) - if rc != 0: - # we may have had a branch who's history was re-written - # just try and get changes for past day - args = ['git', 'rev-list', '--reverse', '--since', '1 day ago', 'HEAD'] - commits = subprocess.check_output(args, cwd=gitdir).split('\n') - return [x for x in commits if len(x)] - - -def _update_commit(project, gitdir, commit): +def _update_commit(project, repo, commit): parser = os.path.join(_here, '../project/apps/patchwork/parser.py') cmd = 'git show %s | python %s -#' % (commit, parser) - hash = subprocess.check_output([cmd], cwd=gitdir, shell=True).strip() + hash = subprocess.check_output([cmd], cwd=repo.path, shell=True).strip() s = State.objects.get(name='Superseded') patches = Patch.objects.filter(project=project, hash=hash).exclude(state=s) @@ -69,19 +36,11 @@ def _update_commit(project, gitdir, commit): def _update_project(repo_dir, project): log.info('Checking for updates to %s', project.linkname) - path = os.path.join(repo_dir, project.linkname) - if not os.path.exists(path): - _clone_repo(repo_dir, project.scm_url, project.linkname) - else: - _pull(path) - - for commit in _commits_to_check(path): - _update_commit(project, path, commit) + repo = gitrepo.Repo(repo_dir, project.linkname, project.scm_url) + repo.update() - with open(os.path.join(path, '.git/refs/heads/master')) as f: - head = f.read() - with open(os.path.join(path, 'patchwork-last-commit'), 'w') as f: - f.write(head) + for commit in repo.process_unchecked_commits(): + _update_commit(project, repo, commit) if __name__ == '__main__': |