Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 """fuse_gtest_files.py v0.2.0
00033 Fuses Google Test source code into a .h file and a .cc file.
00034
00035 SYNOPSIS
00036 fuse_gtest_files.py [GTEST_ROOT_DIR] OUTPUT_DIR
00037
00038 Scans GTEST_ROOT_DIR for Google Test source code, and generates
00039 two files: OUTPUT_DIR/gtest/gtest.h and OUTPUT_DIR/gtest/gtest-all.cc.
00040 Then you can build your tests by adding OUTPUT_DIR to the include
00041 search path and linking with OUTPUT_DIR/gtest/gtest-all.cc. These
00042 two files contain everything you need to use Google Test. Hence
00043 you can "install" Google Test by copying them to wherever you want.
00044
00045 GTEST_ROOT_DIR can be omitted and defaults to the parent
00046 directory of the directory holding this script.
00047
00048 EXAMPLES
00049 ./fuse_gtest_files.py fused_gtest
00050 ./fuse_gtest_files.py path/to/unpacked/gtest fused_gtest
00051
00052 This tool is experimental. In particular, it assumes that there is no
00053 conditional inclusion of Google Test headers. Please report any
00054 problems to googletestframework@googlegroups.com. You can read
00055 http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for
00056 more information.
00057 """
00058
00059 __author__ = 'wan@google.com (Zhanyong Wan)'
00060
00061 import os
00062 import re
00063 import sets
00064 import sys
00065
00066
00067
00068 DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
00069
00070
00071 INCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"')
00072
00073
00074 INCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"')
00075
00076
00077 GTEST_H_SEED = 'include/gtest/gtest.h'
00078 GTEST_SPI_H_SEED = 'include/gtest/gtest-spi.h'
00079 GTEST_ALL_CC_SEED = 'src/gtest-all.cc'
00080
00081
00082 GTEST_H_OUTPUT = 'gtest/gtest.h'
00083 GTEST_ALL_CC_OUTPUT = 'gtest/gtest-all.cc'
00084
00085
00086 def VerifyFileExists(directory, relative_path):
00087 """Verifies that the given file exists; aborts on failure.
00088
00089 relative_path is the file path relative to the given directory.
00090 """
00091
00092 if not os.path.isfile(os.path.join(directory, relative_path)):
00093 print 'ERROR: Cannot find %s in directory %s.' % (relative_path,
00094 directory)
00095 print ('Please either specify a valid project root directory '
00096 'or omit it on the command line.')
00097 sys.exit(1)
00098
00099
00100 def ValidateGTestRootDir(gtest_root):
00101 """Makes sure gtest_root points to a valid gtest root directory.
00102
00103 The function aborts the program on failure.
00104 """
00105
00106 VerifyFileExists(gtest_root, GTEST_H_SEED)
00107 VerifyFileExists(gtest_root, GTEST_ALL_CC_SEED)
00108
00109
00110 def VerifyOutputFile(output_dir, relative_path):
00111 """Verifies that the given output file path is valid.
00112
00113 relative_path is relative to the output_dir directory.
00114 """
00115
00116
00117 output_file = os.path.join(output_dir, relative_path)
00118 if os.path.exists(output_file):
00119
00120
00121
00122 print ('%s already exists in directory %s - overwrite it? (y/N) ' %
00123 (relative_path, output_dir))
00124 answer = sys.stdin.readline().strip()
00125 if answer not in ['y', 'Y']:
00126 print 'ABORTED.'
00127 sys.exit(1)
00128
00129
00130
00131 parent_directory = os.path.dirname(output_file)
00132 if not os.path.isdir(parent_directory):
00133 os.makedirs(parent_directory)
00134
00135
00136 def ValidateOutputDir(output_dir):
00137 """Makes sure output_dir points to a valid output directory.
00138
00139 The function aborts the program on failure.
00140 """
00141
00142 VerifyOutputFile(output_dir, GTEST_H_OUTPUT)
00143 VerifyOutputFile(output_dir, GTEST_ALL_CC_OUTPUT)
00144
00145
00146 def FuseGTestH(gtest_root, output_dir):
00147 """Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
00148
00149 output_file = file(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
00150 processed_files = sets.Set()
00151
00152 def ProcessFile(gtest_header_path):
00153 """Processes the given gtest header file."""
00154
00155
00156 if gtest_header_path in processed_files:
00157 return
00158
00159 processed_files.add(gtest_header_path)
00160
00161
00162 for line in file(os.path.join(gtest_root, gtest_header_path), 'r'):
00163 m = INCLUDE_GTEST_FILE_REGEX.match(line)
00164 if m:
00165
00166 ProcessFile('include/' + m.group(1))
00167 else:
00168
00169 output_file.write(line)
00170
00171 ProcessFile(GTEST_H_SEED)
00172 output_file.close()
00173
00174
00175 def FuseGTestAllCcToFile(gtest_root, output_file):
00176 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
00177
00178 processed_files = sets.Set()
00179
00180 def ProcessFile(gtest_source_file):
00181 """Processes the given gtest source file."""
00182
00183
00184 if gtest_source_file in processed_files:
00185 return
00186
00187 processed_files.add(gtest_source_file)
00188
00189
00190 for line in file(os.path.join(gtest_root, gtest_source_file), 'r'):
00191 m = INCLUDE_GTEST_FILE_REGEX.match(line)
00192 if m:
00193 if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
00194
00195
00196 ProcessFile(GTEST_SPI_H_SEED)
00197 else:
00198
00199
00200
00201
00202
00203
00204 if not GTEST_H_SEED in processed_files:
00205 processed_files.add(GTEST_H_SEED)
00206 output_file.write('#include "%s"\n' % (GTEST_H_OUTPUT,))
00207 else:
00208 m = INCLUDE_SRC_FILE_REGEX.match(line)
00209 if m:
00210
00211 ProcessFile(m.group(1))
00212 else:
00213 output_file.write(line)
00214
00215 ProcessFile(GTEST_ALL_CC_SEED)
00216
00217
00218 def FuseGTestAllCc(gtest_root, output_dir):
00219 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
00220
00221 output_file = file(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
00222 FuseGTestAllCcToFile(gtest_root, output_file)
00223 output_file.close()
00224
00225
00226 def FuseGTest(gtest_root, output_dir):
00227 """Fuses gtest.h and gtest-all.cc."""
00228
00229 ValidateGTestRootDir(gtest_root)
00230 ValidateOutputDir(output_dir)
00231
00232 FuseGTestH(gtest_root, output_dir)
00233 FuseGTestAllCc(gtest_root, output_dir)
00234
00235
00236 def main():
00237 argc = len(sys.argv)
00238 if argc == 2:
00239
00240 FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
00241 elif argc == 3:
00242
00243 FuseGTest(sys.argv[1], sys.argv[2])
00244 else:
00245 print __doc__
00246 sys.exit(1)
00247
00248
00249 if __name__ == '__main__':
00250 main()