Print this page
*** NO COMMENTS ***
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/scripts/cddlchk.py
+++ new/usr/src/tools/scripts/cddlchk.py
1 -#!/usr/bin/python2.4
1 +#!/usr/bin/python2.6
2 2 #
3 3 # CDDL HEADER START
4 4 #
5 5 # The contents of this file are subject to the terms of the
6 6 # Common Development and Distribution License (the "License").
7 7 # You may not use this file except in compliance with the License.
8 8 #
9 9 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 # or http://www.opensolaris.org/os/licensing.
11 11 # See the License for the specific language governing permissions
12 12 # and limitations under the License.
13 13 #
14 14 # When distributing Covered Code, include this CDDL HEADER in each
15 15 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 # If applicable, add the following below this CDDL HEADER, with the
17 17 # fields enclosed by brackets "[]" replaced with your own identifying
18 18 # information: Portions Copyright [yyyy] [name of copyright owner]
19 19 #
20 20 # CDDL HEADER END
21 21 #
22 22
23 23 #
24 24 # Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25 25 #
26 26
27 27 #
28 28 # Check for valid CDDL blocks in source files.
29 29 #
30 30
31 31 import sys, os, getopt, fnmatch
32 32
33 33 sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "lib",
34 34 "python%d.%d" % sys.version_info[:2]))
35 35
36 36 # Allow running from the source tree, using the modules in the source tree
37 37 sys.path.insert(2, os.path.join(os.path.dirname(__file__), '..'))
38 38
39 39 from onbld.Checks.Cddl import cddlchk
40 40
41 41 class ExceptionList(object):
42 42 def __init__(self):
43 43 self.dirs = []
44 44 self.files = []
45 45 self.extensions = []
46 46
47 47 def load(self, exfile):
48 48 fh = None
49 49 try:
50 50 fh = open(exfile, 'r')
51 51 except IOError, e:
52 52 sys.stderr.write('Failed to open exception list: '
53 53 '%s: %s\n' % (e.filename, e.strerror))
54 54 sys.exit(2)
55 55
56 56 for line in fh:
57 57 line = line.strip()
58 58
59 59 if line.strip().endswith('/'):
60 60 self.dirs.append(line[0:-1])
61 61 elif line.startswith('*.'):
62 62 self.extensions.append(line)
63 63 else:
64 64 self.files.append(line)
65 65
66 66 fh.close()
67 67
68 68 def match(self, filename):
69 69 if os.path.isdir(filename):
70 70 return filename in self.dirs
71 71 else:
72 72 if filename in self.files:
73 73 return True
74 74
75 75 for pat in self.extensions:
76 76 if fnmatch.fnmatch(filename, pat):
77 77 return True
78 78
79 79 def __contains__(self, elt):
80 80 return self.match(elt)
81 81
82 82 def usage():
83 83 progname = os.path.split(sys.argv[0])[1]
84 84 sys.stderr.write('''Usage: %s [-av] [-x exceptions] paths...
85 85 -a check that all the specified files have a CDDL block.
86 86 -v report on all files, not just those with errors.
87 87 -x exceptions load an exceptions file
88 88 ''' % progname)
89 89 sys.exit(2)
90 90
91 91
92 92 def check(filename, opts):
93 93 try:
94 94 fh = open(filename, 'r')
95 95 except IOError, e:
96 96 sys.stderr.write("failed to open '%s': %s\n" %
97 97 (e.filename, e.strerror))
98 98 return 1
99 99 else:
100 100 return cddlchk(fh, verbose=opts['verbose'],
101 101 lenient=opts['lenient'],
102 102 output=sys.stdout)
103 103
104 104 def walker(opts, dirname, fnames):
105 105 for f in fnames:
106 106 path = os.path.join(dirname, f)
107 107
108 108 if not os.path.isdir(path):
109 109 if not path in opts['exclude']:
110 110 opts['status'] |= check(path, opts)
111 111 else:
112 112 if path in opts['exclude']:
113 113 fnames.remove(f)
114 114
115 115 def walkpath(path, opts):
116 116 if os.path.isdir(path):
117 117 os.path.walk(path, walker, opts)
118 118 else:
119 119 if not path in opts['exclude']:
120 120 opts['status'] |= check(path, opts)
121 121
122 122 def main(args):
123 123 options = {
124 124 'status': 0,
125 125 'lenient': True,
126 126 'verbose': False,
127 127 'exclude': ExceptionList()
128 128 }
129 129
130 130 try:
131 131 opts, args = getopt.getopt(sys.argv[1:], 'avx:')
132 132 except getopt.GetoptError:
133 133 usage()
134 134 sys.exit(2)
135 135
136 136 for opt, arg in opts:
137 137 if opt == '-a':
138 138 options['lenient'] = False
139 139 elif opt == '-v':
140 140 options['verbose'] = True
141 141 elif opt == '-x':
142 142 options['exclude'].load(arg)
143 143
144 144 for path in args:
145 145 walkpath(path, options)
146 146
147 147 return options['status']
148 148
149 149 if __name__ == '__main__':
150 150 sys.exit(main(sys.argv[1:]))
↓ open down ↓ |
139 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX