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 | |
Lalit Maganti | f161a9b | 2021-11-12 16:49:18 +0000 | [diff] [blame] | 34 | # Check that CREATE VIEW/TABLE has a matching DROP VIEW/TABLE before it. |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 35 | 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 Maganti | f161a9b | 2021-11-12 16:49:18 +0000 | [diff] [blame] | 47 | 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 Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 50 | errors += 1 |
| 51 | d_type, d_name = None, None |
Lalit Maganti | f161a9b | 2021-11-12 16:49:18 +0000 | [diff] [blame] | 52 | |
| 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 Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 63 | return errors |
| 64 | |
| 65 | |
| 66 | def main(): |
| 67 | errors = 0 |
Lalit Maganti | f161a9b | 2021-11-12 16:49:18 +0000 | [diff] [blame] | 68 | metrics_sources = os.path.join(ROOT_DIR, 'src', 'trace_processor', 'metrics', |
| 69 | 'sql') |
Lalit Maganti | e0e8bdb | 2021-01-11 19:43:55 +0000 | [diff] [blame] | 70 | 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 | |
| 78 | if __name__ == '__main__': |
Dan Elphick | 42ac3d1 | 2021-02-01 18:36:30 +0000 | [diff] [blame] | 79 | sys.exit(main()) |