blob: 74b584b8e2542a6cdfe18f7def4ea4761130f0fd [file] [log] [blame]
Primiano Tucci34bc5592021-02-19 17:53:36 +01001#!/usr/bin/env python3
Anindita Ghosh2aa4e422020-06-26 15:48:32 +01002# 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 Mayer41b3daa2020-06-29 17:45:59 +010018from __future__ import absolute_import
19from __future__ import division
20from __future__ import print_function
21
Anindita Ghosh2aa4e422020-06-26 15:48:32 +010022import os
23import re
Anindita Ghosh2aa4e422020-06-26 15:48:32 +010024import sys
25
26ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
27
28def main():
29 errors = 0
30 protos_root = os.path.join(ROOT_DIR, 'protos')
31 for root, _, files in os.walk(protos_root):
32 for fname in files:
33 fpath = os.path.join(root, fname)
34 if not os.path.isfile(fpath):
35 continue
36 if not fpath.endswith('.proto'):
37 continue
Primiano Tuccieac5d712021-05-18 20:45:05 +010038 with open(fpath, encoding='UTF-8') as f:
Anindita Ghosh2aa4e422020-06-26 15:48:32 +010039 lines = f.readlines()
40 for line in lines:
41 comm = line.find('//')
42 alt_comm = re.search(r'^\s*\*', line)
43 only_comm = re.search(r'^\s*//', line)
44
45 # Allow comments only if not inline
46 if comm == -1 or alt_comm or only_comm:
47 continue
48
49 rel_path = os.path.relpath(fpath, ROOT_DIR)
50 sys.stderr.write(
51 ('Proto file %s has inline comment, please move to ' +
52 'the previous line:\t%s') % (rel_path, line))
53 errors += 1
54
55 return 0 if errors == 0 else 1
56
57
58if __name__ == '__main__':
59 sys.exit(main())