6 # The contents of this file are subject to the terms of the
7 # Common Development and Distribution License (the "License").
8 # You may not use this file except in compliance with the License.
10 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11 # or http://www.opensolaris.org/os/licensing.
12 # See the License for the specific language governing permissions
13 # and limitations under the License.
15 # When distributing Covered Code, include this CDDL HEADER in each
16 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17 # If applicable, add the following below this CDDL HEADER, with the
18 # fields enclosed by brackets "[]" replaced with your own identifying
19 # information: Portions Copyright [yyyy] [name of copyright owner]
25 # Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
29 # Check that source files contain a valid comment block
34 CmntChrs
= r
'#*!/\\";. '
36 class CmtBlkError(Exception):
37 def __init__(self
, lineno
, seen
, shouldbe
):
38 Exception.__init
__(self
)
41 self
.shouldbe
= shouldbe
43 def checkblock(block
, blk_text
):
45 lictxt
= block
['block']
47 for actual
, valid
in map(lambda x
, y
: (x
and x
.lstrip(CmntChrs
), y
),
50 raise CmtBlkError(line
, actual
, valid
)
53 def cmtblkchk(fh
, blk_name
, blk_text
, filename
=None,
54 lenient
=False, verbose
=False, output
=sys
.stderr
):
63 StartText
= '%s HEADER START' % blk_name
64 EndText
= '%s HEADER END' % blk_name
65 full_text
= [StartText
, ''] + blk_text
+ ['', EndText
]
67 StartRE
= re
.compile(r
'^[%s ]*%s' % (CmntChrs
, StartText
))
68 EndRE
= re
.compile(r
'^[%s ]*%s' % (CmntChrs
, EndText
))
74 line
= line
.rstrip('\r\n')
77 if StartRE
.search(line
):
81 elif in_cmt
and EndRE
.search(line
):
84 blocks
.append({'start':start
, 'block':lic
})
91 output
.write('%s: %s: Error: Incomplete %s block\n''' %
92 (filename, start, blk_name))
94 # Check for no comment block, warn if we're
not being lenient
95 if not len(blocks
) and not lenient
:
98 output
.write("%s: Warning: No %s block\n" %
101 # Check for multiple comment blocks
104 output
.write('%s: Error: Multiple %s blocks\n'
107 ', '.join([str(x['start
']) for x in blocks])))
109 # Validate each comment block
112 checkblock(b, full_text)
113 except CmtBlkError, e:
116 "%s: %d: Error: Invalid line in %s block:\n"
120 " '%s'\n" % (filename, e.lineno, blk_name,
124 if verbose and not ret:
125 output.write("%s: Valid %s block\n" %
126 (filename, blk_name))