Ticket #11224: fix_boost_mpl_preprocess.py

File fix_boost_mpl_preprocess.py, 4.9 KB (added by Deniz Bahadir <deniz.bahadir@…>, 8 years ago)

Python-script which fixes the header-comments.

Line 
1# Copyright Deniz Bahadir 2015
2#
3# Distributed under the Boost Software License, Version 1.0.
4# (See accompanying file LICENSE_1_0.txt or copy at
5# http://www.boost.org/LICENSE_1_0.txt)
6#
7# See http://www.boost.org/libs/mpl for documentation.
8# See http://stackoverflow.com/a/29627158/3115457 for further information.
9
10import argparse
11import sys
12import os.path
13import re
14import fileinput
15import datetime
16import glob
17
18
19def fix_header_comment(filename, timestamp):
20 """Fixes the header-comment of the given file."""
21 # Fix input file.
22 name = os.path.basename( filename )
23 for line in fileinput.input( filename, inplace=1, mode="rU" ):
24 # If header-comment already contains anything for '$Id$', remove it.
25 line = re.sub(r'\$Id:[^$]+\$', r'$Id$', line.rstrip())
26 # Replace '$Id$' by a string containing the file's name (and a timestamp)!
27 line = re.sub(re.escape(r'$Id$'), r'$Id: ' + name + r' ' + timestamp.isoformat() + r' $', line.rstrip())
28 print(line)
29
30
31def fix_input_files_for_variadic_seq(sourceDir, timestamp):
32 """Fixes files used as input when pre-processing MPL-containers in their variadic form."""
33 files = glob.glob( os.path.join( sourceDir, "src", "*" ) )
34 for currentFile in sorted( files ):
35 fix_header_comment( currentFile, timestamp )
36
37
38def fix_input_files_for_numbered_seq(sourceDir, suffix, timestamp, containers):
39 """Fixes files used as input when pre-processing MPL-containers in their numbered form."""
40 # Fix input files for each MPL-container type.
41 for container in containers:
42 files = glob.glob( os.path.join( sourceDir, container, container + '*' + suffix ) )
43 for currentFile in sorted( files ):
44 fix_header_comment( currentFile, timestamp )
45
46
47def fix_input_files(headerDir, sourceDir, containers=['vector', 'list', 'set', 'map'], verbose='false'):
48 """Fixes source- and header-files used as input when pre-processing MPL-containers."""
49 # The new modification time.
50 timestamp = datetime.datetime.now();
51 # Fix the input files.
52 if verbose:
53 print "Fix input files for pre-processing Boost.MPL variadic containers."
54 fix_input_files_for_variadic_seq(sourceDir, timestamp)
55 if verbose:
56 print "Fix input files for pre-processing Boost.MPL numbered containers."
57 fix_input_files_for_numbered_seq(headerDir, ".hpp", timestamp, containers)
58 fix_input_files_for_numbered_seq(sourceDir, ".cpp", timestamp, containers)
59
60
61def to_existing_absolute_path(string):
62 """Converts a path into its absolute path and verifies that it exists or throws an exception."""
63 value = os.path.abspath(string)
64 if not os.path.exists( value ) or not os.path.isdir( value ):
65 msg = '"%r" is not a valid path to a directory.' % string
66 raise argparse.ArgumentTypeError(msg)
67 return value
68
69
70def main():
71 """The main function."""
72
73 # Prepare and run cmdline-parser.
74 cmdlineParser = argparse.ArgumentParser(
75 description="Fixes the input files used for pre-processing of Boost.MPL headers.")
76 cmdlineParser.add_argument("-v", "--verbose", dest='verbose', action='store_true',
77 help="Be a little bit more verbose.")
78 cmdlineParser.add_argument(dest='sourceDir', metavar="<source-dir>",
79 type=to_existing_absolute_path,
80 help="The source-directory of Boost.")
81 args = cmdlineParser.parse_args()
82
83 # Some verbose debug output.
84 if args.verbose:
85 print "Arguments extracted from command-line:"
86 print " verbose = ", args.verbose
87 print " source directory = ", args.sourceDir
88
89 # The directories for header- and source files of Boost.MPL.
90 # NOTE: Assuming 'args.sourceDir' is the source-directory of the entire boost project.
91 headerDir = os.path.join( args.sourceDir, "boost", "mpl" )
92 sourceDir = os.path.join( args.sourceDir, "libs", "mpl", "preprocessed" )
93 # Check that the header/source-directories exist.
94 if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
95 # Maybe 'args.sourceDir' is not the source-directory of the entire boost project
96 # but instead of the Boost.MPL git-directory, only?
97 headerDir = os.path.join( args.sourceDir, "include", "boost", "mpl" )
98 sourceDir = os.path.join( args.sourceDir, "preprocessed" )
99 if not os.path.exists( headerDir ) or not os.path.exists( sourceDir ):
100 cmdlineParser.print_usage()
101 print "error: Cannot find Boost.MPL header/source files in given Boost source-directory!"
102 sys.exit(0)
103
104 # Some verbose debug output.
105 if args.verbose:
106 print "Chosen header-directory: ", headerDir
107 print "Chosen source-directory: ", sourceDir
108
109 # Fix input file for generating pre-processed headers.
110 fix_input_files(headerDir, sourceDir, verbose = args.verbose)
111
112
113if __name__ == '__main__':
114 main()