media/webrtc/trunk/testing/gtest/scripts/fuse_gtest_files.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rwxr-xr-x

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 #!/usr/bin/env python
michael@0 2 #
michael@0 3 # Copyright 2009, Google Inc.
michael@0 4 # All rights reserved.
michael@0 5 #
michael@0 6 # Redistribution and use in source and binary forms, with or without
michael@0 7 # modification, are permitted provided that the following conditions are
michael@0 8 # met:
michael@0 9 #
michael@0 10 # * Redistributions of source code must retain the above copyright
michael@0 11 # notice, this list of conditions and the following disclaimer.
michael@0 12 # * Redistributions in binary form must reproduce the above
michael@0 13 # copyright notice, this list of conditions and the following disclaimer
michael@0 14 # in the documentation and/or other materials provided with the
michael@0 15 # distribution.
michael@0 16 # * Neither the name of Google Inc. nor the names of its
michael@0 17 # contributors may be used to endorse or promote products derived from
michael@0 18 # this software without specific prior written permission.
michael@0 19 #
michael@0 20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 31
michael@0 32 """fuse_gtest_files.py v0.2.0
michael@0 33 Fuses Google Test source code into a .h file and a .cc file.
michael@0 34
michael@0 35 SYNOPSIS
michael@0 36 fuse_gtest_files.py [GTEST_ROOT_DIR] OUTPUT_DIR
michael@0 37
michael@0 38 Scans GTEST_ROOT_DIR for Google Test source code, and generates
michael@0 39 two files: OUTPUT_DIR/gtest/gtest.h and OUTPUT_DIR/gtest/gtest-all.cc.
michael@0 40 Then you can build your tests by adding OUTPUT_DIR to the include
michael@0 41 search path and linking with OUTPUT_DIR/gtest/gtest-all.cc. These
michael@0 42 two files contain everything you need to use Google Test. Hence
michael@0 43 you can "install" Google Test by copying them to wherever you want.
michael@0 44
michael@0 45 GTEST_ROOT_DIR can be omitted and defaults to the parent
michael@0 46 directory of the directory holding this script.
michael@0 47
michael@0 48 EXAMPLES
michael@0 49 ./fuse_gtest_files.py fused_gtest
michael@0 50 ./fuse_gtest_files.py path/to/unpacked/gtest fused_gtest
michael@0 51
michael@0 52 This tool is experimental. In particular, it assumes that there is no
michael@0 53 conditional inclusion of Google Test headers. Please report any
michael@0 54 problems to googletestframework@googlegroups.com. You can read
michael@0 55 http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for
michael@0 56 more information.
michael@0 57 """
michael@0 58
michael@0 59 __author__ = 'wan@google.com (Zhanyong Wan)'
michael@0 60
michael@0 61 import os
michael@0 62 import re
michael@0 63 import sets
michael@0 64 import sys
michael@0 65
michael@0 66 # We assume that this file is in the scripts/ directory in the Google
michael@0 67 # Test root directory.
michael@0 68 DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
michael@0 69
michael@0 70 # Regex for matching '#include "gtest/..."'.
michael@0 71 INCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"')
michael@0 72
michael@0 73 # Regex for matching '#include "src/..."'.
michael@0 74 INCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"')
michael@0 75
michael@0 76 # Where to find the source seed files.
michael@0 77 GTEST_H_SEED = 'include/gtest/gtest.h'
michael@0 78 GTEST_SPI_H_SEED = 'include/gtest/gtest-spi.h'
michael@0 79 GTEST_ALL_CC_SEED = 'src/gtest-all.cc'
michael@0 80
michael@0 81 # Where to put the generated files.
michael@0 82 GTEST_H_OUTPUT = 'gtest/gtest.h'
michael@0 83 GTEST_ALL_CC_OUTPUT = 'gtest/gtest-all.cc'
michael@0 84
michael@0 85
michael@0 86 def VerifyFileExists(directory, relative_path):
michael@0 87 """Verifies that the given file exists; aborts on failure.
michael@0 88
michael@0 89 relative_path is the file path relative to the given directory.
michael@0 90 """
michael@0 91
michael@0 92 if not os.path.isfile(os.path.join(directory, relative_path)):
michael@0 93 print 'ERROR: Cannot find %s in directory %s.' % (relative_path,
michael@0 94 directory)
michael@0 95 print ('Please either specify a valid project root directory '
michael@0 96 'or omit it on the command line.')
michael@0 97 sys.exit(1)
michael@0 98
michael@0 99
michael@0 100 def ValidateGTestRootDir(gtest_root):
michael@0 101 """Makes sure gtest_root points to a valid gtest root directory.
michael@0 102
michael@0 103 The function aborts the program on failure.
michael@0 104 """
michael@0 105
michael@0 106 VerifyFileExists(gtest_root, GTEST_H_SEED)
michael@0 107 VerifyFileExists(gtest_root, GTEST_ALL_CC_SEED)
michael@0 108
michael@0 109
michael@0 110 def VerifyOutputFile(output_dir, relative_path):
michael@0 111 """Verifies that the given output file path is valid.
michael@0 112
michael@0 113 relative_path is relative to the output_dir directory.
michael@0 114 """
michael@0 115
michael@0 116 # Makes sure the output file either doesn't exist or can be overwritten.
michael@0 117 output_file = os.path.join(output_dir, relative_path)
michael@0 118 if os.path.exists(output_file):
michael@0 119 # TODO(wan@google.com): The following user-interaction doesn't
michael@0 120 # work with automated processes. We should provide a way for the
michael@0 121 # Makefile to force overwriting the files.
michael@0 122 print ('%s already exists in directory %s - overwrite it? (y/N) ' %
michael@0 123 (relative_path, output_dir))
michael@0 124 answer = sys.stdin.readline().strip()
michael@0 125 if answer not in ['y', 'Y']:
michael@0 126 print 'ABORTED.'
michael@0 127 sys.exit(1)
michael@0 128
michael@0 129 # Makes sure the directory holding the output file exists; creates
michael@0 130 # it and all its ancestors if necessary.
michael@0 131 parent_directory = os.path.dirname(output_file)
michael@0 132 if not os.path.isdir(parent_directory):
michael@0 133 os.makedirs(parent_directory)
michael@0 134
michael@0 135
michael@0 136 def ValidateOutputDir(output_dir):
michael@0 137 """Makes sure output_dir points to a valid output directory.
michael@0 138
michael@0 139 The function aborts the program on failure.
michael@0 140 """
michael@0 141
michael@0 142 VerifyOutputFile(output_dir, GTEST_H_OUTPUT)
michael@0 143 VerifyOutputFile(output_dir, GTEST_ALL_CC_OUTPUT)
michael@0 144
michael@0 145
michael@0 146 def FuseGTestH(gtest_root, output_dir):
michael@0 147 """Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
michael@0 148
michael@0 149 output_file = file(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
michael@0 150 processed_files = sets.Set() # Holds all gtest headers we've processed.
michael@0 151
michael@0 152 def ProcessFile(gtest_header_path):
michael@0 153 """Processes the given gtest header file."""
michael@0 154
michael@0 155 # We don't process the same header twice.
michael@0 156 if gtest_header_path in processed_files:
michael@0 157 return
michael@0 158
michael@0 159 processed_files.add(gtest_header_path)
michael@0 160
michael@0 161 # Reads each line in the given gtest header.
michael@0 162 for line in file(os.path.join(gtest_root, gtest_header_path), 'r'):
michael@0 163 m = INCLUDE_GTEST_FILE_REGEX.match(line)
michael@0 164 if m:
michael@0 165 # It's '#include "gtest/..."' - let's process it recursively.
michael@0 166 ProcessFile('include/' + m.group(1))
michael@0 167 else:
michael@0 168 # Otherwise we copy the line unchanged to the output file.
michael@0 169 output_file.write(line)
michael@0 170
michael@0 171 ProcessFile(GTEST_H_SEED)
michael@0 172 output_file.close()
michael@0 173
michael@0 174
michael@0 175 def FuseGTestAllCcToFile(gtest_root, output_file):
michael@0 176 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
michael@0 177
michael@0 178 processed_files = sets.Set()
michael@0 179
michael@0 180 def ProcessFile(gtest_source_file):
michael@0 181 """Processes the given gtest source file."""
michael@0 182
michael@0 183 # We don't process the same #included file twice.
michael@0 184 if gtest_source_file in processed_files:
michael@0 185 return
michael@0 186
michael@0 187 processed_files.add(gtest_source_file)
michael@0 188
michael@0 189 # Reads each line in the given gtest source file.
michael@0 190 for line in file(os.path.join(gtest_root, gtest_source_file), 'r'):
michael@0 191 m = INCLUDE_GTEST_FILE_REGEX.match(line)
michael@0 192 if m:
michael@0 193 if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
michael@0 194 # It's '#include "gtest/gtest-spi.h"'. This file is not
michael@0 195 # #included by "gtest/gtest.h", so we need to process it.
michael@0 196 ProcessFile(GTEST_SPI_H_SEED)
michael@0 197 else:
michael@0 198 # It's '#include "gtest/foo.h"' where foo is not gtest-spi.
michael@0 199 # We treat it as '#include "gtest/gtest.h"', as all other
michael@0 200 # gtest headers are being fused into gtest.h and cannot be
michael@0 201 # #included directly.
michael@0 202
michael@0 203 # There is no need to #include "gtest/gtest.h" more than once.
michael@0 204 if not GTEST_H_SEED in processed_files:
michael@0 205 processed_files.add(GTEST_H_SEED)
michael@0 206 output_file.write('#include "%s"\n' % (GTEST_H_OUTPUT,))
michael@0 207 else:
michael@0 208 m = INCLUDE_SRC_FILE_REGEX.match(line)
michael@0 209 if m:
michael@0 210 # It's '#include "src/foo"' - let's process it recursively.
michael@0 211 ProcessFile(m.group(1))
michael@0 212 else:
michael@0 213 output_file.write(line)
michael@0 214
michael@0 215 ProcessFile(GTEST_ALL_CC_SEED)
michael@0 216
michael@0 217
michael@0 218 def FuseGTestAllCc(gtest_root, output_dir):
michael@0 219 """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
michael@0 220
michael@0 221 output_file = file(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
michael@0 222 FuseGTestAllCcToFile(gtest_root, output_file)
michael@0 223 output_file.close()
michael@0 224
michael@0 225
michael@0 226 def FuseGTest(gtest_root, output_dir):
michael@0 227 """Fuses gtest.h and gtest-all.cc."""
michael@0 228
michael@0 229 ValidateGTestRootDir(gtest_root)
michael@0 230 ValidateOutputDir(output_dir)
michael@0 231
michael@0 232 FuseGTestH(gtest_root, output_dir)
michael@0 233 FuseGTestAllCc(gtest_root, output_dir)
michael@0 234
michael@0 235
michael@0 236 def main():
michael@0 237 argc = len(sys.argv)
michael@0 238 if argc == 2:
michael@0 239 # fuse_gtest_files.py OUTPUT_DIR
michael@0 240 FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
michael@0 241 elif argc == 3:
michael@0 242 # fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR
michael@0 243 FuseGTest(sys.argv[1], sys.argv[2])
michael@0 244 else:
michael@0 245 print __doc__
michael@0 246 sys.exit(1)
michael@0 247
michael@0 248
michael@0 249 if __name__ == '__main__':
michael@0 250 main()

mercurial