Merge "Build and package Layer Management service binaries." into tizen
[profile/ivi/layer-management.git] / scripts / check_tabbing_and_spacing.py
1 #!/usr/bin/python
2
3 ###########################################################################
4 #
5 # Copyright 2013 BMW Car IT GmbH
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #        http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 ###########################################################################
20
21 import sys, re, string
22 from common_modules.common import *
23
24 def check_tabs_no_spaces(filename, file_lines):
25     """
26     Checks if any tab character "\t" exists in the file
27
28     """
29     for i in range(len(file_lines)):
30         if file_lines[i].count("\t") > 0:
31             log_warning(filename, i + 1, "tab character (\\t) found, use 4 spaces instead", file_lines[i].strip(" "))
32
33 def check_correct_space_count(filename, file_lines):
34     """
35     Checks that the number of spaces at the beginning of every line is divislbe by four
36
37     """
38     in_multiline_comment = False
39     for i in range(len(file_lines)):
40         if in_multiline_comment:
41             #if end of muli-line comment
42             if file_lines[i].count("*/") > 0:
43                 #just make sure there is no other new multi-line comment starting on the same line (after the current mult-line comment is closed)
44                 in_multiline_comment = file_lines[i].count("/*") > 0 and file_lines[i].index("/*") > file_lines[i].index("*/") 
45         else:
46             in_multiline_comment = file_lines[i].count("/*") > 0
47
48             #regex searches for the first character that is not a space character
49             found_match = re.search("(?! )", file_lines[i])
50             if found_match:
51                 space_count = found_match.start()
52                 if space_count % 4 != 0:
53                     log_warning(filename, i + 1, "number of spaces at beginning of line must be divisible by 4")
54
55
56 def check_no_spacing_line_end(filename, file_lines):
57     """
58     Checks that lines do not end with unnecessary white space characters
59
60     """
61     for i in range(len(file_lines)):
62         if re.search(" $", file_lines[i]):
63             log_warning(filename, i + 1, "unneeded space(s) at end of line")
64
65 def check_tabbing_and_spacing(filename, file_lines):
66     """
67     Calls other functions that check general issues about tabbing and spacing
68
69     """
70     check_tabs_no_spaces(filename, file_lines)
71     check_correct_space_count(filename, file_lines)
72     check_no_spacing_line_end(filename, file_lines)
73
74
75 if __name__ == "__main__":
76     targets = sys.argv[1:]
77     targets = get_all_files(targets)
78
79     if len(targets) == 0:
80         print """
81 \t**** No input provided ****
82 \tTakes a list of files/directories as input and performs specific style checking on all files/directories.
83
84 \tGives warnings if a line contains unneeded spaces at end of line, contains tab characters (\\t)
85 \tor if spaces at line beginning are not divisible by 4.
86 """
87         exit(0)
88
89     for t in targets:
90         if t[-2:] == ".h" or t[-4:] == ".cpp" or t[-2] == ".c":
91             _, _, file_lines, _ = read_file(t)
92             check_tabbing_and_spacing(t, file_lines)