blob: 0d3e9081b036a87ca42ea42741a25f79c2fa9510 [file] [log] [blame]
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -08001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-"
3#
4# Copyright 2020 The Chromium OS Authors. All rights reserved.
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8"""Module rebuilding database with metadata about chromeos patches."""
9
10from __future__ import print_function
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080011import sqlite3
12import os
13import re
14import subprocess
Hirthanan Subenderan00f18042020-02-11 17:24:38 -080015from common import CHROMEOS_PATH, SUPPORTED_KERNELS, \
16 WORKDIR, CHERRYPICK, STABLE, STABLE2, make_downstream_table, \
17 stabledb, chromeosdb, chromeos_branch, createdb
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080018
19UPSTREAM = re.compile(r'(ANDROID: *|UPSTREAM: *|FROMGIT: *|BACKPORT: *)+(.*)')
20CHROMIUM = re.compile(r'(CHROMIUM: *|FROMLIST: *)+(.*)')
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080021CHANGEID = re.compile(r'^( )*Change-Id: [a-zA-Z0-9]*$')
22
23
24def parse_changeID(chromeos_sha):
25 """String searches for Change-Id in a chromeos git commit.
26
27 Returns Change-Id or None if commit doesn't have associated Change-Id
28 """
29 commit = subprocess.check_output(['git', 'show', \
30 chromeos_sha]).decode('utf-8', errors='ignore')
31
32 for line in commit.splitlines():
33 if CHANGEID.match(line):
34 # removes whitespace prefixing Change-Id
35 line = line.lstrip()
36 commit_changeID = line[(line.index(' ') + 1):]
37 return commit_changeID
38
39 return None
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080040
41
42def search_usha(sha, description):
43 """Search for upstream SHA.
44
45 If found, return upstream sha associated with this commit sha.
46 """
47
48 usha = ''
49 if not CHROMIUM.match(description):
50 desc = subprocess.check_output(['git', 'show',
51 '-s', sha]).decode('utf-8', errors='ignore')
52 for d in desc.splitlines():
53 m = CHERRYPICK.search(d)
54 if not m:
55 m = STABLE.search(d)
56 if not m:
57 m = STABLE2.search(d)
58 if m:
59 # The patch may have been picked multiple times; only record
60 # the first entry.
61 usha = m.group(2)[:12]
62 return usha
63 return usha
64
65
66
67def update_commits(start, cdb, sdb):
68 """Get list of commits from selected branch, starting with commit 'start'.
69
70 Branch must be checked out in current directory.
71 Skip commit if it is contained in sdb, otherwise add to cdb if it isn't
72 already there.
73 """
74
75 try:
76 conn = sqlite3.connect(cdb)
77 conn.text_factory = str
78 c = conn.cursor()
79
80 sconn = sqlite3.connect(sdb)
81 sconn.text_factory = str
82 sc = sconn.cursor()
83 except sqlite3.Error as e:
84 print('Could not update chromeos commits, raising error: ', e)
85 raise
86
87 subprocess_cmd = ['git', 'log', '--no-merges', '--abbrev=12',
88 '--oneline', '--reverse', '%s..' % start]
89 commits = subprocess.check_output(subprocess_cmd).decode('utf-8')
90
91 last = None
92 for commit in commits.splitlines():
93 if commit:
94 elem = commit.split(' ', 1)
95 sha = elem[0]
96 description = elem[1].rstrip('\n')
97 ps = subprocess.Popen(['git', 'show', sha],
98 stdout=subprocess.PIPE, encoding='utf-8')
99 spid = subprocess.check_output(['git', 'patch-id', '--stable'],
100 stdin=ps.stdout).decode('utf-8', errors='ignore')
101 patchid = spid.split(' ', 1)[0]
102
103 # Do nothing if sha is in stable database
104 sc.execute("select sha from commits where sha='%s'" % sha)
105 found = sc.fetchone()
106 if found:
107 continue
108
109 # Do nothing if sha is already in database
110 c.execute("select sha from commits where sha='%s'" % sha)
111 found = c.fetchone()
112 if found:
113 continue
114
115 last = sha
116
117 usha = search_usha(sha, description)
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -0800118 changeid = parse_changeID(sha)
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -0800119
120 c.execute('INSERT INTO commits(sha, usha, patchid,' \
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -0800121 'description, changeid) VALUES (?, ?, ?, ?, ?)',
122 (sha, usha, patchid, description, changeid))
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -0800123 if last:
124 c.execute("UPDATE tip set sha='%s' where ref=1" % last)
125
126 conn.commit()
127 conn.close()
128 sconn.close()
129
130
131def update_chromeosdb():
132 """Updates the chromeosdb for all chromeos branches."""
133 os.chdir(CHROMEOS_PATH)
134
Hirthanan Subenderan00f18042020-02-11 17:24:38 -0800135 for branch in SUPPORTED_KERNELS:
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -0800136 start = 'v%s' % branch
137 cdb = chromeosdb(branch)
138 sdb = stabledb(branch)
139 bname = chromeos_branch(branch)
140
141 print('Handling %s' % bname)
142
143 try:
144 conn = sqlite3.connect(cdb)
145 conn.text_factory = str
146
147 c = conn.cursor()
148 c.execute('select sha from tip')
149 sha = c.fetchone()
150 conn.close()
151 if sha and sha[0]:
152 start = sha[0]
153 except sqlite3.Error:
154 createdb(cdb, make_downstream_table)
155
156 subprocess.run(['git', 'checkout', bname])
157 subprocess.run(['git', 'pull'])
158
159 update_commits(start, cdb, sdb)
160
161 os.chdir(WORKDIR)
162
163
164if __name__ == '__main__':
165 update_chromeosdb()