Changeset: e80511f8ec4b for MonetDB URL: https://dev.monetdb.org/hg/MonetDB/rev/e80511f8ec4b Modified Files: misc/python/fixlicense.py Branch: default Log Message:
Updated script. diffs (truncated from 539 to 300 lines): diff --git a/misc/python/fixlicense.py b/misc/python/fixlicense.py --- a/misc/python/fixlicense.py +++ b/misc/python/fixlicense.py @@ -6,24 +6,7 @@ # # Copyright 1997 - July 2008 CWI, August 2008 - 2022 MonetDB B.V. -import os, sys, getopt, stat - -usage = '''\ -%(prog)s [-ar] [-l licensefile] [file...] - -Options: --a\tadd license text (default) --r\tremove license text --l licensefile -\tprovide alternative license text -\t(handy for removing incorrect license text) --v\treport changed files on standard output - -If no file arguments, %(prog)s will read file names from standard input. - -%(prog)s makes backups of all modified files. -The backup is the file with a tilde (~) appended.\ -''' +import os, sys, argparse, stat license = [ 'This Source Code Form is subject to the terms of the Mozilla Public', @@ -37,92 +20,101 @@ def main(): func = addlicense pre = post = start = end = None verbose = False - try: - opts, args = getopt.getopt(sys.argv[1:], 'arl:sv', - ['pre=', 'post=', 'start=', 'end=']) - except getopt.GetoptError: - print(usage % {'prog': sys.argv[0]}, file=sys.stderr) + parser = argparse.ArgumentParser(description='Update license texts') + parser.add_argument('--pre', action='store', default=None, + help='line before license text') + parser.add_argument('--post', action='store', default=None, + help='line after license text') + parser.add_argument('--start', action='store', default=None, + help='text at start of license text line') + parser.add_argument('--end', action='store', default=None, + help='text at end of license text line') + parser.add_argument('--nl', action='store_false', + help='whether a blank line is added after license text') + parser.add_argument('--add', '-a', action='store_true', + help='add license file (default)') + parser.add_argument('--remove', '-r', action='store_true', + help='remove license file') + parser.add_argument('--list', '-s', action='store_true', + help='list files that already contain the license') + parser.add_argument('--license', '-l', action='store', + type=argparse.FileType('r'), + help='file with license text') + parser.add_argument('--verbose', '-v', action='store_true', + help='be a bit more verbose') + parser.add_argument('files', nargs='*', + help='files to work on (default read from stdin)') + opts = parser.parse_args() + if opts.add + opts.remove + opts.list > 1: + print('--add, --remove, and --list are mutually exclusive', + file=sys.stderr) sys.exit(1) - for o, a in opts: - if o == '-a': - func = addlicense - elif o == '-r': - func = dellicense - elif o == '-s': - func = listfile - elif o == '-l': - try: - f = open(a) - except IOError: - print('Cannot open file %s' % a, file=sys.stderr) - sys.exit(1) - del license[:] - while True: - line = f.readline() - if not line: - break - license.append(line[:-1]) - f.close() - elif o == '--pre': - pre = a - elif o == '--post': - post = a - elif o == '--start': - start = a - elif o == '--end': - end = a - elif o == '-v': - verbose = True + if opts.remove: + func = dellicense + elif opts.list: + func = listfile + else: + func = addlicense + if opts.license is not None: + del license[:] + license.extend([l.rstrip('\n') for l in opts.license.readlines()]) - if args: - for a in args: - func(a, pre=pre, post=post, start=start, end=end, verbose=verbose) + if opts.files: + for a in opts.files: + func(a, pre=opts.pre, post=opts.post, start=opts.start, end=opts.end, verbose=opts.verbose) else: while True: filename = sys.stdin.readline() if not filename: break - func(filename[:-1], pre=pre, post=post, start=start, end=end, verbose=verbose) + func(filename[:-1], pre=opts.pre, post=opts.post, start=opts.start, end=opts.end, verbose=opts.verbose) suffixrules = { - # suffix: (pre, post, start, end) - '.bash': ('', '', '# ', ''), # shell script - '.bat': ('', '', '@REM ',''), # Windows cmd batch script - '.c': ('/*', ' */', ' * ', ''), # C source - '.cc': ('', '', '// ', ''), # C++ source - '.cmake': ('#[[', '#]]', '# ', ''), # CMake source - '.cpp': ('', '', '// ', ''), # C++ source - '.el': ('', '', '; ', ''), # Emacs Lisp - '.fc': ('', '', '# ', ''), # SELinux file context - '.h': ('/*', ' */', ' * ', ''), # C header file - '.hs': ('', '', '-- ', ''), # Haskell source - '.html': ('<!--', '-->', '', ''), # HTML source - '.java': ('/*', ' */', ' * ', ''), # Java source - '.l': ('/*', ' */', ' * ', ''), # (f)lex source - '.mal': ('', '', '# ', ''), # MonetDB Assembly Language - '.php': ('<?php', '?>', '# ', ''), # PHP source - '.pl': ('', '', '# ', ''), # Perl source - '.pm': ('', '', '# ', ''), # Perl module source - '.py': ('', '', '# ', ''), # Python source - '.R': ('', '', '# ', ''), # R source - '.rb': ('', '', '# ', ''), # Ruby source - '.rc': ('', '', '// ', ''), # Windows resource file - '.rst': ('', '', '.. ', ''), # reStructured Text - '.sh': ('', '', '# ', ''), # shell script - '.sql': ('', '', '-- ', ''), # SQL source - '.t': ('', '', '# ', ''), # Perl test - '.te': ('', '', '# ', ''), # SELinux - '.xml': ('<!--', '-->', '', ''), # XML source - '.y': ('/*', ' */', ' * ', ''), # yacc (bison) source + # suffix: (pre, post, start, end, nl) + # pre: line before license text + # post: line after license text + # start: at start of each line of license text + # end: at end of each line of license text + # nl: whether a blank line should be added after license text + '.1': ('', '', '.\\" ','', False), # manual page source + '.bash': ('', '', '# ', '', True), # shell script + '.bat': ('', '', '@REM ','', True), # Windows cmd batch script + '.c': ('/*', ' */', ' * ', '', True), # C source + '.cc': ('', '', '// ', '', True), # C++ source + '.cmake': ('#[[', '#]]', '# ', '', True), # CMake source + '.cpp': ('', '', '// ', '', True), # C++ source + '.el': ('', '', '; ', '', True), # Emacs Lisp + '.fc': ('', '', '# ', '', True), # SELinux file context + '.h': ('/*', ' */', ' * ', '', True), # C header file + '.hs': ('', '', '-- ', '', True), # Haskell source + '.html': ('<!--', '-->', '', '', True), # HTML source + '.java': ('/*', ' */', ' * ', '', True), # Java source + '.l': ('/*', ' */', ' * ', '', True), # (f)lex source + '.mal': ('', '', '# ', '', True), # MonetDB Assembly Language + '.php': ('<?php', '?>', '# ', '', True), # PHP source + '.pl': ('', '', '# ', '', True), # Perl source + '.pm': ('', '', '# ', '', True), # Perl module source + '.py': ('', '', '# ', '', True), # Python source + '.R': ('', '', '# ', '', True), # R source + '.rb': ('', '', '# ', '', True), # Ruby source + '.rc': ('', '', '// ', '', True), # Windows resource file + '.rst': ('', '', '.. ', '', True), # reStructured Text + '.sh': ('', '', '# ', '', True), # shell script + '.spec': ('', '', '# ', '', True), # RPM specification file + '.sql': ('', '', '-- ', '', True), # SQL source + '.t': ('', '', '# ', '', True), # Perl test + '.te': ('', '', '# ', '', True), # SELinux + '.xml': ('<!--', '-->', '', '', True), # XML source + '.y': ('/*', ' */', ' * ', '', True), # yacc (bison) source # we also match some complete filenames - 'CMakeLists.txt': ('#[[', '#]]', '# ', ''), - 'Makefile': ('', '', '# ', ''), - '.merovingian_properties': ('', '', '# ', ''), - 'copyright': ('', '', '', ''), - 'license.txt': ('', '', '', ''), + 'CMakeLists.txt': ('#[[', '#]]', '# ', '', True), + 'Makefile': ('', '', '# ', '', True), + '.merovingian_properties': ('', '', '# ', '', True), + 'copyright': ('', '', '', '', True), + 'license.txt': ('', '', '', '', True), } -def getcomments(file, pre = None, post = None, start = None, end = None): +def getcomments(file, pre=None, post=None, start=None, end=None, nl=True): ext = '' if pre is None and post is None and start is None and end is None: if file.endswith('.in') and os.path.basename(file[:-3]) in suffixrules: @@ -143,36 +135,36 @@ def getcomments(file, pre = None, post = if line[:2] == '#!': ext = '.sh' else: - return '', '', '', '', '' - pre, post, start, end = suffixrules[ext] - if not pre: + return '', '', '', '', '', True + pre, post, start, end, nl = suffixrules[ext] + if pre is None: pre = '' - if not post: + if post is None: post = '' - if not start: + if start is None: start = '' - if not end: + if end is None: end = '' - return ext, pre, post, start, end + return ext, pre, post, start, end, nl PERL_COPYRIGHT = 'COPYRIGHT AND LICENCE\n\n' COPYRIGHT_NOTICE = 'Copyright Notice\n================\n\n' -def addlicense(file, pre = None, post = None, start = None, end = None, verbose = False): +def addlicense(file, pre=None, post=None, start=None, end=None, verbose=False): try: f = open(file) except IOError: - print('Cannot open file %s' % file, file=sys.stderr) + print(f'Cannot open file {file}', file=sys.stderr) return try: g = open(file + '.new', 'w') except IOError: - print('Cannot create temp file %s.new' % file, file=sys.stderr) + print(f'Cannot create temp file {file}.new', file=sys.stderr) return try: data = f.read() except UnicodeError: - print('UnicodeError in file %s' % file, file=sys.stderr) + print(f'UnicodeError in file {file}', file=sys.stderr) return if PERL_COPYRIGHT in data: notice = PERL_COPYRIGHT @@ -189,7 +181,7 @@ def addlicense(file, pre = None, post = g.write(data[pos:]) else: try: - ext, pre, post, start, end = getcomments(file, pre, post, start, end) + ext, pre, post, start, end, nl = getcomments(file, pre, post, start, end) if not ext: return except IOError: @@ -197,27 +189,27 @@ def addlicense(file, pre = None, post = f = open(file) line = f.readline() addblank = False - if line[:2] == '#!': + if line.startswith('#!'): # if file starts with #! command interpreter, keep the line there g.write(line) # add a blank line addblank = True line = f.readline() - if line.find('-*-') >= 0: + if '-*-' in line: # if file starts with an Emacs mode specification, keep # the line there g.write(line) # add a blank line addblank = True line = f.readline() - if line.find('vim:') >= 0: + if 'vim:' in line: # if file starts with a vim mode specification, keep # the line there g.write(line) # add a blank line addblank = True line = f.readline() - if line[:5] == '<?xml': + if line.startswith('<?xml'): # if line starts with an XML declaration, keep the line there g.write(line) # add a blank line @@ -236,7 +228,7 @@ def addlicense(file, pre = None, post = if post: g.write(post + '\n') # add empty line after license - if line: + if line and nl: _______________________________________________ checkin-list mailing list -- checkin-list@monetdb.org To unsubscribe send an email to checkin-list-le...@monetdb.org