Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 1 | #!/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 Elphick | 42ac3d1 | 2021-02-01 18:36:30 +0000 | [diff] [blame] | 16 | # This tool checks that every create (table|view) is prefixed by |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 17 | # drop (table|view). |
| 18 | |
| 19 | from __future__ import absolute_import |
| 20 | from __future__ import division |
| 21 | from __future__ import print_function |
| 22 | |
| 23 | import os |
| 24 | import re |
| 25 | import sys |
| 26 | |
| 27 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 28 | |
| 29 | |
| 30 | def 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 Elphick | 42ac3d1 | 2021-02-01 18:36:30 +0000 | [diff] [blame] | 46 | sys.stderr.write(('%s:\n "%s" vs %s %s\n') % (path, line, d_type, d_name)) |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 47 | errors += 1 |
| 48 | d_type, d_name = None, None |
| 49 | return errors |
| 50 | |
| 51 | |
| 52 | def 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 | |
| 63 | if __name__ == '__main__': |
Dan Elphick | 42ac3d1 | 2021-02-01 18:36:30 +0000 | [diff] [blame] | 64 | sys.exit(main()) |