blob: 02b1aeae2c76dd7c05f2a084c351dceee179f620 [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
36def patchdb(version):
37 """Path of patchdb for each chromeosdb"""
38 return os.path.join(DBDIR, '/patch-%s.db' % version)
39
40
41def stable_branch(version):
42 """Stable branch name"""
43 return 'linux-%s.y' % version
44
45
46def chromeos_branch(version):
47 """Chromeos branch name"""
48 return 'chromeos-%s' % version
49
50
51def patch_link(changeID):
52 """Link to patch on gerrit"""
53 return 'https://chromium-review.googlesource.com/q/%s' % changeID
54
55
56def doremove(filepath):
57 """remove file if it exists"""
58 try:
59 os.remove(filepath)
60 except OSError:
61 pass
62
63
64def make_downstream_table(c):
65 """Create database table storing information about chrome/stable git logs"""
66
67 c.execute('CREATE TABLE commits (sha text, usha text, '
68 'patchid text, description text)')
69 c.execute('CREATE UNIQUE INDEX commit_sha ON commits (sha)')
70 c.execute('CREATE INDEX upstream_sha ON commits (usha)')
71 c.execute('CREATE INDEX patch_id ON commits (patchid)')
72
73
74def createdb(db, op):
75 """remove and recreate database"""
76 newdbdir = os.path.dirname(db)
77 os.makedirs(newdbdir, exist_ok=True)
78
79 doremove(db)
80
81 conn = sqlite3.connect(db)
82 c = conn.cursor()
83
84 op(c)
85
86 # Convention: table 'tip' ref 1 contains the most recently processed SHA.
87 # Use this to avoid re-processing SHAs already in the database.
88 c.execute('CREATE TABLE tip (ref integer, sha text)')
89 c.execute('INSERT INTO tip (ref, sha) VALUES (?, ?)', (1, ''))
90
91 # Save (commit) the changes
92 conn.commit()
93 conn.close()