blob: dc17dff1332f3b0979fb9a81ccc33ef001b4722f [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 containing shared helper methods."""
9
10from __future__ import print_function
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080011import os
12import sqlite3
13import re
14
Hirthanan Subenderan00f18042020-02-11 17:24:38 -080015
16KERNEL_SITE = 'https://git.kernel.org/'
17UPSTREAM_REPO = KERNEL_SITE + 'pub/scm/linux/kernel/git/torvalds/linux'
18STABLE_REPO = KERNEL_SITE + 'pub/scm/linux/kernel/git/stable/linux-stable'
19
20CHROMIUM_SITE = 'https://chromium.googlesource.com/'
21CHROMEOS_REPO = CHROMIUM_SITE + 'chromiumos/third_party/kernel'
22CHROMIUM_REVIEW_BASEURL = 'https://chromium-review.googlesource.com/'
23
24SUPPORTED_KERNELS = ('4.4', '4.14', '4.19', '5.4')
25
26CHROMEOS_PATH = 'linux-chrome'
27STABLE_PATH = 'linux-stable'
28UPSTREAM_PATH = 'linux-upstream'
29
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080030WORKDIR = os.getcwd()
31DBDIR = os.path.join(WORKDIR, 'database')
32UPSTREAMDB = os.path.join(DBDIR, 'upstream.db')
33
34# "commit" is sometimes seen multiple times, such as with commit 6093aabdd0ee
35CHERRYPICK = re.compile(r'cherry picked from (commit )+([0-9a-f]+)')
36STABLE = re.compile(r'^\s*(commit )+([a-f0-9]+) upstream')
37STABLE2 = re.compile(r'^\s*\[\s*Upstream (commit )+([0-9a-f]+)\s*\]')
38
39
40def stabledb(version):
41 """Path of stabledb"""
42 return os.path.join(DBDIR, 'stable-%s.db' % version)
43
44
45def chromeosdb(version):
46 """Path of chromeosdb"""
47 return os.path.join(DBDIR, 'chromeos-%s.db' % version)
48
49
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080050def patchdb_stable(version):
51 """Path of patchdb for stable versions."""
52 return os.path.join(DBDIR, 'patch-stable-%s.db' % version)
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080053
Hirthanan Subenderan00f18042020-02-11 17:24:38 -080054
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080055def patchdb_chromeos(version):
56 """Path of patchdb for chromeos versions."""
57 return os.path.join(DBDIR, 'patch-chromeos-%s.db' % version)
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080058
Hirthanan Subenderan00f18042020-02-11 17:24:38 -080059
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080060def stable_branch(version):
61 """Stable branch name"""
62 return 'linux-%s.y' % version
63
64
65def chromeos_branch(version):
66 """Chromeos branch name"""
67 return 'chromeos-%s' % version
68
69
70def patch_link(changeID):
71 """Link to patch on gerrit"""
72 return 'https://chromium-review.googlesource.com/q/%s' % changeID
73
74
75def doremove(filepath):
76 """remove file if it exists"""
77 try:
78 os.remove(filepath)
79 except OSError:
80 pass
81
82
83def make_downstream_table(c):
84 """Create database table storing information about chrome/stable git logs"""
85
86 c.execute('CREATE TABLE commits (sha text, usha text, '
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080087 'patchid text, description text, changeid text)')
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080088 c.execute('CREATE UNIQUE INDEX commit_sha ON commits (sha)')
89 c.execute('CREATE INDEX upstream_sha ON commits (usha)')
90 c.execute('CREATE INDEX patch_id ON commits (patchid)')
91
92
93def createdb(db, op):
94 """remove and recreate database"""
95 newdbdir = os.path.dirname(db)
96 os.makedirs(newdbdir, exist_ok=True)
97
98 doremove(db)
99
100 conn = sqlite3.connect(db)
101 c = conn.cursor()
102
103 op(c)
104
105 # Convention: table 'tip' ref 1 contains the most recently processed SHA.
106 # Use this to avoid re-processing SHAs already in the database.
107 c.execute('CREATE TABLE tip (ref integer, sha text)')
108 c.execute('INSERT INTO tip (ref, sha) VALUES (?, ?)', (1, ''))
109
110 # Save (commit) the changes
111 conn.commit()
112 conn.close()