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.
26 # Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
30 # Check that source files contain a valid comment block
35 CmntChrs
= r
'#*!/\\";. '
37 class CmtBlkError(Exception):
38 def __init__(self
, lineno
, seen
, shouldbe
):
39 Exception.__init
__(self
)
42 self
.shouldbe
= shouldbe
44 def checkblock(block
, blk_text
):
46 lictxt
= block
['block']
48 for actual
, valid
in map(lambda x
, y
: (x
and x
.lstrip(CmntChrs
), y
),
51 raise CmtBlkError(line
, actual
, valid
)
54 def cmtblkchk(fh
, blk_name
, blk_text
, filename
=None,
55 lenient
=False, verbose
=False, output
=sys
.stderr
):
64 StartText
= '%s HEADER START' % blk_name
65 EndText
= '%s HEADER END' % blk_name
66 full_text
= [StartText
, ''] + blk_text
+ ['', EndText
]
68 StartRE
= re
.compile(r
'^[%s ]*%s' % (CmntChrs
, StartText
))
69 EndRE
= re
.compile(r
'^[%s ]*%s' % (CmntChrs
, EndText
))
75 line
= line
.rstrip('\r\n')
78 if StartRE
.search(line
):
82 elif in_cmt
and EndRE
.search(line
):
85 blocks
.append({'start':start
, 'block':lic
})
92 output
.write('%s: %s: Error: Incomplete %s block\n''' %
93 (filename, start, blk_name))
95 # Check for no comment block, warn if we're
not being lenient
96 if not len(blocks
) and not lenient
:
99 output
.write("%s: Warning: No %s block\n" %
100 (filename
, blk_name
))
102 # Check for multiple comment blocks
105 output
.write('%s: Error: Multiple %s blocks\n'
108 ', '.join([str(x['start
']) for x in blocks])))
110 # Validate each comment block
113 checkblock(b, full_text)
114 except CmtBlkError as e:
117 "%s: %d: Error: Invalid line in %s block:\n"
121 " '%s'\n" % (filename, e.lineno, blk_name,
125 if verbose and not ret:
126 output.write("%s: Valid %s block\n" %
127 (filename, blk_name))