commitsdb.py

changeset 124
7b2cd8b1ba86
parent 123
aeb0d0788869
child 125
c44b1aa85257
equal deleted inserted replaced
123:aeb0d0788869 124:7b2cd8b1ba86
1 class CommitsDb (object):
2 def __init__(self):
3 needNew = not os.path.isfile ('commits.db')
4 self.db = sqlite3.connect ('commits.db')
5
6 if needNew:
7 self.create_new()
8
9 def create_new (self):
10 self.db.executescript ('''
11 DROP TABLE IF EXISTS COMMITS;
12 DROP TABLE IF EXISTS REPOS;
13 DROP TABLE IF EXISTS REPOCOMMITS;
14 CREATE TABLE IF NOT EXISTS COMMITS
15 (
16 Node text NOT NULL,
17 Dateversion text NOT NULL,
18 PRIMARY KEY (Node)
19 );
20 CREATE TABLE IF NOT EXISTS REPOS
21 (
22 Name text NOT NULL,
23 PRIMARY KEY (Name)
24 );
25 CREATE TABLE IF NOT EXISTS REPOCOMMITS
26 (
27 Reponame text,
28 Node text,
29 FOREIGN KEY (Reponame) REFERENCES REPOS(Name),
30 FOREIGN KEY (Node) REFERENCES COMMITS(Node)
31 );
32 ''')
33
34 print 'Building commits.db...'
35 for repo in all_repo_names():
36 print 'Adding commits from %s...' % repo
37
38 data = subprocess.check_output (['hg', '--cwd', repo, 'log', '--template',
39 '{node} {date|hgdate}\n']).splitlines()
40
41 for line in data:
42 changeset, timestamp, tz = line.split(' ')
43 self.add_commit (repo, changeset, int (timestamp))
44
45 self.commit()
46
47 def add_commit (self, repo, changeset, timestamp):
48 dateversion = datetime.utcfromtimestamp (timestamp).strftime ('%y%m%d-%H%M')
49 self.db.execute ('''
50 INSERT OR IGNORE INTO REPOS
51 VALUES (?)
52 ''', (repo,))
53
54 self.db.execute ('''
55 INSERT OR IGNORE INTO COMMITS
56 VALUES (?, ?)
57 ''', (changeset, dateversion))
58
59 self.db.execute ('''
60 INSERT INTO REPOCOMMITS
61 VALUES (?, ?)
62 ''', (repo, changeset))
63
64 def get_commit_repos (self, node):
65 cursor = self.db.execute ('''
66 SELECT Reponame
67 FROM REPOCOMMITS
68 WHERE Node LIKE ?
69 ''', (node + '%',))
70
71 results = cursor.fetchall()
72 return list (set (zip (*results)[0])) if results else []
73
74 def find_commit_by_dateversion (self, dateversion):
75 cursor = self.db.execute ('''
76 SELECT Node
77 FROM COMMITS
78 WHERE Dateversion = ?
79 ''', (dateversion,))
80 result = cursor.fetchone()
81 return result[0] if result else None
82
83 def commit(self):
84 self.db.commit()

mercurial