blob: dfa93b16e06f812062c13adb928b37f91338eaae [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
11
12import os
13import sqlite3
14import re
15
16WORKDIR = os.getcwd()
17DBDIR = os.path.join(WORKDIR, 'database')
18UPSTREAMDB = os.path.join(DBDIR, 'upstream.db')
19
20# "commit" is sometimes seen multiple times, such as with commit 6093aabdd0ee
21CHERRYPICK = re.compile(r'cherry picked from (commit )+([0-9a-f]+)')
22STABLE = re.compile(r'^\s*(commit )+([a-f0-9]+) upstream')
23STABLE2 = re.compile(r'^\s*\[\s*Upstream (commit )+([0-9a-f]+)\s*\]')
24
25
26def stabledb(version):
27 """Path of stabledb"""
28 return os.path.join(DBDIR, 'stable-%s.db' % version)
29
30
31def chromeosdb(version):
32 """Path of chromeosdb"""
33 return os.path.join(DBDIR, 'chromeos-%s.db' % version)
34
35
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080036def patchdb_stable(version):
37 """Path of patchdb for stable versions."""
38 return os.path.join(DBDIR, 'patch-stable-%s.db' % version)
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080039
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080040def patchdb_chromeos(version):
41 """Path of patchdb for chromeos versions."""
42 return os.path.join(DBDIR, 'patch-chromeos-%s.db' % version)
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080043
44def stable_branch(version):
45 """Stable branch name"""
46 return 'linux-%s.y' % version
47
48
49def chromeos_branch(version):
50 """Chromeos branch name"""
51 return 'chromeos-%s' % version
52
53
54def patch_link(changeID):
55 """Link to patch on gerrit"""
56 return 'https://chromium-review.googlesource.com/q/%s' % changeID
57
58
59def doremove(filepath):
60 """remove file if it exists"""
61 try:
62 os.remove(filepath)
63 except OSError:
64 pass
65
66
67def make_downstream_table(c):
68 """Create database table storing information about chrome/stable git logs"""
69
70 c.execute('CREATE TABLE commits (sha text, usha text, '
Hirthanan Subenderanb8402a12020-02-05 14:11:00 -080071 'patchid text, description text, changeid text)')
Hirthanan Subenderan3e884d62020-01-23 13:12:45 -080072 c.execute('CREATE UNIQUE INDEX commit_sha ON commits (sha)')
73 c.execute('CREATE INDEX upstream_sha ON commits (usha)')
74 c.execute('CREATE INDEX patch_id ON commits (patchid)')
75
76
77def createdb(db, op):
78 """remove and recreate database"""
79 newdbdir = os.path.dirname(db)
80 os.makedirs(newdbdir, exist_ok=True)
81
82 doremove(db)
83
84 conn = sqlite3.connect(db)
85 c = conn.cursor()
86
87 op(c)
88
89 # Convention: table 'tip' ref 1 contains the most recently processed SHA.
90 # Use this to avoid re-processing SHAs already in the database.
91 c.execute('CREATE TABLE tip (ref integer, sha text)')
92 c.execute('INSERT INTO tip (ref, sha) VALUES (?, ?)', (1, ''))
93
94 # Save (commit) the changes
95 conn.commit()
96 conn.close()