1cdf0c1d5Smjnelson#! /usr/bin/python
2cdf0c1d5Smjnelson#
3cdf0c1d5Smjnelson# CDDL HEADER START
4cdf0c1d5Smjnelson#
5cdf0c1d5Smjnelson# The contents of this file are subject to the terms of the
6cdf0c1d5Smjnelson# Common Development and Distribution License (the "License").
7cdf0c1d5Smjnelson# You may not use this file except in compliance with the License.
8cdf0c1d5Smjnelson#
9cdf0c1d5Smjnelson# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10cdf0c1d5Smjnelson# or http://www.opensolaris.org/os/licensing.
11cdf0c1d5Smjnelson# See the License for the specific language governing permissions
12cdf0c1d5Smjnelson# and limitations under the License.
13cdf0c1d5Smjnelson#
14cdf0c1d5Smjnelson# When distributing Covered Code, include this CDDL HEADER in each
15cdf0c1d5Smjnelson# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16cdf0c1d5Smjnelson# If applicable, add the following below this CDDL HEADER, with the
17cdf0c1d5Smjnelson# fields enclosed by brackets "[]" replaced with your own identifying
18cdf0c1d5Smjnelson# information: Portions Copyright [yyyy] [name of copyright owner]
19cdf0c1d5Smjnelson#
20cdf0c1d5Smjnelson# CDDL HEADER END
21cdf0c1d5Smjnelson#
22cdf0c1d5Smjnelson
23cdf0c1d5Smjnelson#
24cdf0c1d5Smjnelson# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
25cdf0c1d5Smjnelson# Use is subject to license terms.
26cdf0c1d5Smjnelson#
27*ca13eaa5SAndy Fiddaman
28*ca13eaa5SAndy Fiddaman# Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
29cdf0c1d5Smjnelson
30cdf0c1d5Smjnelson#
31cdf0c1d5Smjnelson# Mercurial (lack of) keyword checks
32cdf0c1d5Smjnelson#
33cdf0c1d5Smjnelson
34cdf0c1d5Smjnelsonimport re, sys
35cdf0c1d5Smjnelson
36cdf0c1d5Smjnelson# A general 'ident'-style decleration, to allow for leniency.
37cdf0c1d5Smjnelsonident = re.compile(r'((\%Z\%(\%M\%)\s+\%I\%|\%W\%)\s+\%E\% SMI)')
38cdf0c1d5Smjnelson
39cdf0c1d5Smjnelson#
40cdf0c1d5Smjnelson# Absolutely anything that appears to be an SCCS keyword.
41cdf0c1d5Smjnelson# It's impossible to programatically differentiate between these
42*ca13eaa5SAndy Fiddaman# and other, legitimate, uses of matching strings.
43cdf0c1d5Smjnelson#
44cdf0c1d5Smjnelsonanykword = re.compile(r'%[A-ILMP-UWYZ]%')
45cdf0c1d5Smjnelson
46cdf0c1d5Smjnelsondef keywords(fh, filename=None, lenient=False, verbose=False,
47cdf0c1d5Smjnelson             output=sys.stderr):
48cdf0c1d5Smjnelson    '''Search FH for SCCS keywords, which should not be present when
49cdf0c1d5Smjnelson    Mercurial is in use.
50cdf0c1d5Smjnelson
51cdf0c1d5Smjnelson    If LENIENT, accept #ident-style declarations, for the purposes of
52cdf0c1d5Smjnelson    migration'''
53cdf0c1d5Smjnelson
54cdf0c1d5Smjnelson    if not filename:
55cdf0c1d5Smjnelson        filename = fh.name
56cdf0c1d5Smjnelson
57cdf0c1d5Smjnelson    ret = 0
58cdf0c1d5Smjnelson    lineno = 0
59*ca13eaa5SAndy Fiddaman
60cdf0c1d5Smjnelson    for line in fh:
61cdf0c1d5Smjnelson        line = line.rstrip('\r\n')
62cdf0c1d5Smjnelson        lineno += 1
63*ca13eaa5SAndy Fiddaman
64cdf0c1d5Smjnelson        if lenient and ident.search(line):
65cdf0c1d5Smjnelson            continue
66*ca13eaa5SAndy Fiddaman
67cdf0c1d5Smjnelson        match = anykword.findall(line)
68cdf0c1d5Smjnelson        if match:
69cdf0c1d5Smjnelson            ret = 1
70cdf0c1d5Smjnelson            output.write('%s: %d: contains SCCS keywords "%s"\n' %
71cdf0c1d5Smjnelson                         (filename, lineno, ', '.join(match)))
72cdf0c1d5Smjnelson            if verbose:
73cdf0c1d5Smjnelson                output.write("   %s\n" % line)
74cdf0c1d5Smjnelson
75cdf0c1d5Smjnelson    return ret
76