RULE: enforce rule check at build
[tools/building-blocks.git] / rule_checker.py
1 #!/usr/bin/env python
2 # License: Apache version 2
3 # (c) 2017 MyungJoo Ham <myungjoo.ham@samsung.com>
4 #
5 #############################################################
6 # Building Block Rule Checker                               #
7 #############################################################
8 # This does not check all rules of "RULES"
9 # This is a prototype with a lot of work in progress
10
11
12
13 from __future__ import print_function
14 import re
15 import os
16 import os.path
17 import sys
18
19
20 def ruleCheckInc(file):
21     error = 0
22     warning = 0
23
24     try:
25         f = open("packaging/"+file, 'r')
26     except:
27         warning += 1
28         print("WARNING: cannot find packaging/"+file)
29         return (0, 1)
30     for line in f:
31         # RULE 5.1
32         if re.search('^\s*BuildRequires', line, re.IGNORECASE):
33             error += 1
34             print("ERROR: RULE 5.1 .inc file cannot have BuildRequires tags")
35             continue
36
37         # Prevent: https://github.com/rpm-software-management/rpm/issues/158
38         if re.search('^#.*[^%]%[^%]', line) and not re.search('^#!', line):
39             error += 1
40             print("ERROR: unless it is shebang, you must not have rpm macro in a # comment. They are expanded and multiline macro will do unexpected effects.")
41             continue
42
43         # RULE 5.2
44         if re.search('^\s*Recommends', line, re.IGNORECASE) or  \
45             re.search('^\s*Provides', line, re.IGNORECASE) or   \
46             re.search('^\s*Enhances', line, re.IGNORECASE) or   \
47             re.search('^\s*Supplements', line, re.IGNORECASE):
48             error += 1
49             print("ERROR: RULE 5.2 .inc file cannot have unsupported relations")
50
51         # RULE 1-1
52         if re.search('^\s*%package\s*-n', line, re.IGNORECASE):
53             error += 1
54             print("ERROR: RULE 1.1 to ensure 1.1, do not use -n option in package name")
55
56         # Implicit / General Rule
57         if re.search('^\s*%package\s', line, re.IGNORECASE) and not re.search('^\s*%package\s', line):
58             error += 1
59             print('ERROR: (General) Please use %package, not '+re.search('^%package'))
60
61         # RULE 1-3
62         if re.search('^\s*%package', line) and not re.search('^\s*%package\s*(root)|(sub1)|(sub2)', line):
63             error +=1
64             print("ERROR: RULE 1.3 the send prefix should be root, sub1, or sub2.")
65
66             
67
68     f.close()
69     
70
71
72     return (error, warning)
73
74
75 def main():
76     dirs = os.listdir("packaging/")
77     error = 0
78     warning = 0
79
80     # iterate in the list of ./packaging/
81     for file in dirs:
82         if re.search('\.inc', file):
83             result = ruleCheckInc(file)
84             error += result[0]
85             warning += result[1]
86
87     print('Error: '+str(error))
88     print('Warning: '+str(warning))
89
90     return error
91
92 retval = main()
93 sys.exit(retval)
94