blob: 65dcff288d5a1840a318819f3a0a4e92b4325a67 [file] [log] [blame]
Lalit Magantie0e8bdb2021-01-11 19:43:55 +00001#!/usr/bin/env python3
2# Copyright (C) 2021 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
Dan Elphick42ac3d12021-02-01 18:36:30 +000016# This tool checks that every create (table|view) is prefixed by
Lalit Magantie0e8bdb2021-01-11 19:43:55 +000017# drop (table|view).
18
19from __future__ import absolute_import
20from __future__ import division
21from __future__ import print_function
22
23import os
24import re
25import sys
26
27ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
28
29
30def check(path):
31 with open(path) as f:
32 lines = [l.strip() for l in f.readlines()]
33
Lalit Magantif161a9b2021-11-12 16:49:18 +000034 # Check that CREATE VIEW/TABLE has a matching DROP VIEW/TABLE before it.
Lalit Magantie0e8bdb2021-01-11 19:43:55 +000035 errors = 0
36 d_type, d_name = None, None
37 for line in lines:
38 m = re.match(r'^DROP (TABLE|VIEW) IF EXISTS (.*);$', line)
39 if m is not None:
40 d_type, d_name = m.group(1), m.group(2)
41 continue
42 m = re.match(r'^CREATE (?:VIRTUAL )?(TABLE|VIEW) (.*) (?:AS|USING).*', line)
43 if m is None:
44 continue
45 type, name = m.group(1), m.group(2)
46 if type != d_type or name != d_name:
Lalit Magantif161a9b2021-11-12 16:49:18 +000047 sys.stderr.write(
48 ('Missing DROP %s before CREATE %s\n') % (d_type, d_type))
49 sys.stderr.write(('%s:\n"%s" vs %s %s\n') % (path, line, d_type, d_name))
Lalit Magantie0e8bdb2021-01-11 19:43:55 +000050 errors += 1
51 d_type, d_name = None, None
Lalit Magantif161a9b2021-11-12 16:49:18 +000052
53 # Ban the use of LIKE in non-comment lines.
54 for line in lines:
55 if line.startswith('--'):
56 continue
57
58 if 'like' in line.casefold():
59 sys.stderr.write(
60 'LIKE is banned in trace processor metrics. Prefer GLOB instead.')
61 errors += 1
62
Lalit Magantie0e8bdb2021-01-11 19:43:55 +000063 return errors
64
65
66def main():
67 errors = 0
Lalit Magantif161a9b2021-11-12 16:49:18 +000068 metrics_sources = os.path.join(ROOT_DIR, 'src', 'trace_processor', 'metrics',
69 'sql')
Lalit Magantie0e8bdb2021-01-11 19:43:55 +000070 for root, _, files in os.walk(metrics_sources, topdown=True):
71 for f in files:
72 path = os.path.join(root, f)
73 if path.endswith('.sql'):
74 errors += check(path)
75 return 0 if errors == 0 else 1
76
77
78if __name__ == '__main__':
Dan Elphick42ac3d12021-02-01 18:36:30 +000079 sys.exit(main())