Primiano Tucci | 34bc559 | 2021-02-19 17:53:36 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 2 | # Copyright (C) 2020 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 | |
| 16 | # This tool checks for inline comments in proto files |
| 17 | |
Florian Mayer | 41b3daa | 2020-06-29 17:45:59 +0100 | [diff] [blame] | 18 | from __future__ import absolute_import |
| 19 | from __future__ import division |
| 20 | from __future__ import print_function |
| 21 | |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 22 | import os |
| 23 | import re |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 24 | import sys |
| 25 | |
| 26 | ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 27 | |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame] | 28 | |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 29 | def main(): |
| 30 | errors = 0 |
Hector Dearman | 2717aa0 | 2022-06-20 13:30:42 +0100 | [diff] [blame] | 31 | # Don't want to check protos/third_party: |
| 32 | protos_root = os.path.join(ROOT_DIR, 'protos', 'perfetto') |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 33 | for root, _, files in os.walk(protos_root): |
| 34 | for fname in files: |
| 35 | fpath = os.path.join(root, fname) |
| 36 | if not os.path.isfile(fpath): |
| 37 | continue |
| 38 | if not fpath.endswith('.proto'): |
| 39 | continue |
Primiano Tucci | eac5d71 | 2021-05-18 20:45:05 +0100 | [diff] [blame] | 40 | with open(fpath, encoding='UTF-8') as f: |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 41 | lines = f.readlines() |
| 42 | for line in lines: |
| 43 | comm = line.find('//') |
| 44 | alt_comm = re.search(r'^\s*\*', line) |
| 45 | only_comm = re.search(r'^\s*//', line) |
| 46 | |
| 47 | # Allow comments only if not inline |
| 48 | if comm == -1 or alt_comm or only_comm: |
| 49 | continue |
| 50 | |
| 51 | rel_path = os.path.relpath(fpath, ROOT_DIR) |
Hector Dearman | b1989b0 | 2022-07-05 20:11:07 +0100 | [diff] [blame] | 52 | sys.stderr.write(('Proto file %s has inline comment, please move to ' + |
| 53 | 'the previous line:\t%s') % (rel_path, line)) |
Anindita Ghosh | 2aa4e42 | 2020-06-26 15:48:32 +0100 | [diff] [blame] | 54 | errors += 1 |
| 55 | |
| 56 | return 0 if errors == 0 else 1 |
| 57 | |
| 58 | |
| 59 | if __name__ == '__main__': |
| 60 | sys.exit(main()) |