blob: 2fe57df116ce6e5ca29136555f5657c072495371 [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
34 errors = 0
35 d_type, d_name = None, None
36 for line in lines:
37 m = re.match(r'^DROP (TABLE|VIEW) IF EXISTS (.*);$', line)
38 if m is not None:
39 d_type, d_name = m.group(1), m.group(2)
40 continue
41 m = re.match(r'^CREATE (?:VIRTUAL )?(TABLE|VIEW) (.*) (?:AS|USING).*', line)
42 if m is None:
43 continue
44 type, name = m.group(1), m.group(2)
45 if type != d_type or name != d_name:
Dan Elphick42ac3d12021-02-01 18:36:30 +000046 sys.stderr.write(('%s:\n "%s" vs %s %s\n') % (path, line, d_type, d_name))
Lalit Magantie0e8bdb2021-01-11 19:43:55 +000047 errors += 1
48 d_type, d_name = None, None
49 return errors
50
51
52def main():
53 errors = 0
54 metrics_sources = os.path.join(ROOT_DIR, 'src', 'trace_processor', 'metrics')
55 for root, _, files in os.walk(metrics_sources, topdown=True):
56 for f in files:
57 path = os.path.join(root, f)
58 if path.endswith('.sql'):
59 errors += check(path)
60 return 0 if errors == 0 else 1
61
62
63if __name__ == '__main__':
Dan Elphick42ac3d12021-02-01 18:36:30 +000064 sys.exit(main())