Hirthanan Subenderan | 3e884d6 | 2020-01-23 13:12:45 -0800 | [diff] [blame] | 1 | #!/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 | |
| 10 | from __future__ import print_function |
| 11 | |
| 12 | import os |
| 13 | import sqlite3 |
| 14 | import re |
| 15 | |
| 16 | WORKDIR = os.getcwd() |
| 17 | DBDIR = os.path.join(WORKDIR, 'database') |
| 18 | UPSTREAMDB = os.path.join(DBDIR, 'upstream.db') |
| 19 | |
| 20 | # "commit" is sometimes seen multiple times, such as with commit 6093aabdd0ee |
| 21 | CHERRYPICK = re.compile(r'cherry picked from (commit )+([0-9a-f]+)') |
| 22 | STABLE = re.compile(r'^\s*(commit )+([a-f0-9]+) upstream') |
| 23 | STABLE2 = re.compile(r'^\s*\[\s*Upstream (commit )+([0-9a-f]+)\s*\]') |
| 24 | |
| 25 | |
| 26 | def stabledb(version): |
| 27 | """Path of stabledb""" |
| 28 | return os.path.join(DBDIR, 'stable-%s.db' % version) |
| 29 | |
| 30 | |
| 31 | def chromeosdb(version): |
| 32 | """Path of chromeosdb""" |
| 33 | return os.path.join(DBDIR, 'chromeos-%s.db' % version) |
| 34 | |
| 35 | |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 36 | def patchdb_stable(version): |
| 37 | """Path of patchdb for stable versions.""" |
| 38 | return os.path.join(DBDIR, 'patch-stable-%s.db' % version) |
Hirthanan Subenderan | 3e884d6 | 2020-01-23 13:12:45 -0800 | [diff] [blame] | 39 | |
Hirthanan Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 40 | def patchdb_chromeos(version): |
| 41 | """Path of patchdb for chromeos versions.""" |
| 42 | return os.path.join(DBDIR, 'patch-chromeos-%s.db' % version) |
Hirthanan Subenderan | 3e884d6 | 2020-01-23 13:12:45 -0800 | [diff] [blame] | 43 | |
| 44 | def stable_branch(version): |
| 45 | """Stable branch name""" |
| 46 | return 'linux-%s.y' % version |
| 47 | |
| 48 | |
| 49 | def chromeos_branch(version): |
| 50 | """Chromeos branch name""" |
| 51 | return 'chromeos-%s' % version |
| 52 | |
| 53 | |
| 54 | def patch_link(changeID): |
| 55 | """Link to patch on gerrit""" |
| 56 | return 'https://chromium-review.googlesource.com/q/%s' % changeID |
| 57 | |
| 58 | |
| 59 | def doremove(filepath): |
| 60 | """remove file if it exists""" |
| 61 | try: |
| 62 | os.remove(filepath) |
| 63 | except OSError: |
| 64 | pass |
| 65 | |
| 66 | |
| 67 | def 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 Subenderan | b8402a1 | 2020-02-05 14:11:00 -0800 | [diff] [blame] | 71 | 'patchid text, description text, changeid text)') |
Hirthanan Subenderan | 3e884d6 | 2020-01-23 13:12:45 -0800 | [diff] [blame] | 72 | 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 | |
| 77 | def 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() |