Whamcloud - gitweb
LU-3221 lov: remove set_fs() call from lov_getstripe()
[fs/lustre-release.git] / contrib / scripts / nn-check.py
1 #!/usr/bin/python
2
3 # This script is for checking that patches don't introduce non-portable symbols
4 # into the Lustre/LNET/libcfs code.
5 #
6 # Input:
7 # 1. (Required) Filename (including path) of the diff file to be checked
8 # 2. (Optional) path to the nn-final-symbol-list.txt file (By default, this
9 #    script looks for nn-final-symbol-list.txt in the current working
10 #    directory.)
11 #
12 # Output:
13 # The output of this script is either PASS or FAIL (with WARNINGS).
14 # FAIL means that there may have been symbols found that are not supposed
15 # to be used.  This requires the person running the script to look into the
16 # WARNINGS that are in the output to determine if there is a problem.
17
18 # Author: lisa.week@sun.com
19
20 import string
21 import re
22 import sys
23 import optparse
24 import os.path
25 import fileinput
26
27 # Setup command line options for nn-check.py
28 from optparse import OptionParser
29 usage = "%prog DIFF-FILE [options]"
30 parser = OptionParser(usage)
31 parser.add_option("-s", "--symb", action="store", dest="symb_pathname",
32                   help="(Optional) PATH to nn-final-symbol-list.txt file",
33                   metavar="PATH")
34
35 (options, args) = parser.parse_args()
36
37 # Check if we have the minimum number of arguments supplied. 
38 if len(args) < 1:
39         parser.error("Incorrect number of arguments, see nn-check -h for help.")
40
41 # Check if we were passed a path to the nn-final-symbol-list.txt file
42 if options.symb_pathname:
43         symb_file = os.path.join(options.symb_pathname,
44                                  'nn-final-symbol-list.txt')
45 else:
46         symb_file = 'nn-final-symbol-list.txt'
47
48 # Global Variables
49 bad_symbol_cnt = 0
50 symbol_dict = dict() 
51
52 # Function Definitions
53 def search_symbol(line, linenum):
54         global bad_symbol_cnt
55
56         for key, val in symbol_dict.items():
57                 regex_match = val.search(line)
58
59                 if regex_match:
60                         print_symbol = regex_match.group(0)
61                         print 'warning: Found %s at line %d:' \
62                                 % (print_symbol, linenum)
63                         print '%s' % line.rstrip()
64                         bad_symbol_cnt += 1
65
66 # Open the nn-final-symbol-list.txt file and pull in the symbols to check into
67 # a dictionary object.
68 try:
69         f = fileinput.input(symb_file)
70 except IOError:
71         print 'nn-check.py: error: %s not found.' % symb_file
72         print 'Is nn-final-symbol-list.txt is in your current working directory'
73         print 'or have you have passed nn-check.py a valid path to the file?'
74         sys.exit(1)
75
76
77 for line in f:
78         stripped_symbol = line.rstrip()
79         symbol_dict[stripped_symbol] = re.compile(stripped_symbol)
80
81 # Close nn-final-symbol-list.txt
82 f.close()
83
84 # Open the diff file passed to the script and parse it for the symbols from
85 # nn-final-symbol-list.txt
86 try:
87         f = fileinput.input(sys.argv[1])
88 except IOError:
89         print 'nn-check.py: error: %s not found.' % sys.argv[1] 
90         print 'Check the path provided for the diff file.'
91         sys.exit(1)
92
93 # The main portion of the script
94 print '==================================================='
95 print '%s: starting nn-check' % sys.argv[1]
96 print '==================================================='
97
98 index = re.compile(r'^\+\+\+ b/(.*)')
99 plus = re.compile(r'^\+')
100 for line in f:
101         # Check for the "diff --cc " delimiter in order to grab the file name.
102         index_match = index.match(line)
103
104         if index_match:
105                 # Store the file name
106                 filename=index_match.group(1)
107                 print '%s: ' % filename
108         else:
109                 # Check if the line starts with a "+" character.
110                 plus_match = plus.match(line)
111                 if plus_match:
112                         # The line starts with a "+" character.  Look for
113                         # non-portable symbols
114                         search_symbol(line, f.lineno())
115                 else:
116                         continue
117
118 # Close the diff file
119 f.close()
120
121 # Finish up and print the results of the script (i.e. total number of
122 # bad symbols found)
123 if bad_symbol_cnt != 0:
124         print '=============================='
125         print 'Finished nn-check status: FAIL'
126         print '=============================='
127         print 'Found %d potential problem(s).  See "WARNINGS" from script output and refer to https://wikis.lustre.org/intra/index.php/Lustre_Name_Normalization for the complete set of rules to make sure you have not used a non-portable symbol.' % bad_symbol_cnt
128 else:
129         print '=============================='
130         print 'Finished nn-check status: PASS'
131         print '=============================='