source: codereviewerplugin/0.12/coderev/util/sync.py

Last change on this file was 14033, checked in by Ryan J Ollos, 9 years ago

Changed license to 3-Clause BSD with permission of author. Refs #11832.

  • Property svn:executable set to *
File size: 2.2 KB
Line 
1#! /usr/bin/python
2# -*- coding: utf-8 -*-
3#
4# Copyright (C) 2012 Rob Guttman <guttman@alum.mit.edu>
5# All rights reserved.
6#
7# This software is licensed as described in the file COPYING, which
8# you should have received as part of this distribution.
9#
10
11import os
12import re
13import sys
14import sqlite3
15from subprocess import Popen, STDOUT, PIPE
16
17EPOCH_MULTIPLIER = 1000000.0
18
19
20def sync(db_path, repo_dir):
21    db = sqlite3.connect(db_path)
22    reponame = os.path.basename(repo_dir.rstrip('/')).lower()
23
24    # extract changesets to sync
25    changeset_lines = get_changeset_lines(repo_dir)
26    print "processing %d changesets" % len(changeset_lines)
27
28    # purge table for this repo
29    cursor = db.cursor()
30    cursor.execute("""
31        DELETE FROM codereviewer_map
32        WHERE repo='%s'
33    """ % reponame)
34
35    # insert a row per ticket in commit message
36    for changeset_line in changeset_lines:
37        print '.',
38        rev,when,msg = changeset_line.split('|',2)
39        when = long(when) * EPOCH_MULTIPLIER
40        ticket_re = re.compile('#([0-9]+)')
41        for ticket in ticket_re.findall(msg):
42            try:
43                cursor.execute("""
44                    INSERT INTO codereviewer_map
45                        (repo,changeset,ticket,time)
46                    VALUES ('%s','%s','%s',%s);
47                    """ % (reponame,rev,ticket,when))
48            except sqlite3.IntegrityError:
49                print "\nduplicate %s, %s, #%s" % (reponame,rev,ticket)
50    db.commit()
51
52def get_changeset_lines(repo_dir):
53    """Return all changesets including their commit time and message
54    pipe-delimited in chronological order."""
55    cmds = ['cd %s' % repo_dir, 'git log --reverse --format="%H|%ct|%s"']
56    return execute(' && '.join(cmds)).splitlines()
57
58def execute(cmd):
59    p = Popen(cmd, shell=True, stderr=STDOUT, stdout=PIPE)
60    out = p.communicate()[0]
61    if p.returncode != 0:
62        raise Exception('cmd: %s\n%s' % (cmd,out))
63    return out
64
65
66if __name__ == "__main__":
67    if len(sys.argv) < 3:
68        file = os.path.basename(sys.argv[0])
69        print "usage: %s <db_path> <repo_dir>" % file
70        sys.exit(1)
71
72    # sync
73    db_path = sys.argv[1]
74    repo_dir = sys.argv[2]
75    sync(db_path,repo_dir)
Note: See TracBrowser for help on using the repository browser.