fuse_gtest_files.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2009, Google Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions are
00008 # met:
00009 #
00010 #     * Redistributions of source code must retain the above copyright
00011 # notice, this list of conditions and the following disclaimer.
00012 #     * Redistributions in binary form must reproduce the above
00013 # copyright notice, this list of conditions and the following disclaimer
00014 # in the documentation and/or other materials provided with the
00015 # distribution.
00016 #     * Neither the name of Google Inc. nor the names of its
00017 # contributors may be used to endorse or promote products derived from
00018 # this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00023 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00024 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00025 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00026 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 # We assume that this file is in the scripts/ directory in the Google
00067 # Test root directory.
00068 DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
00069 
00070 # Regex for matching '#include "gtest/..."'.
00071 INCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"')
00072 
00073 # Regex for matching '#include "src/..."'.
00074 INCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"')
00075 
00076 # Where to find the source seed files.
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 # Where to put the generated files.
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   # Makes sure the output file either doesn't exist or can be overwritten.
00117   output_file = os.path.join(output_dir, relative_path)
00118   if os.path.exists(output_file):
00119     # TODO(wan@google.com): The following user-interaction doesn't
00120     # work with automated processes.  We should provide a way for the
00121     # Makefile to force overwriting the files.
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   # Makes sure the directory holding the output file exists; creates
00130   # it and all its ancestors if necessary.
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()  # Holds all gtest headers we've processed.
00151 
00152   def ProcessFile(gtest_header_path):
00153     """Processes the given gtest header file."""
00154 
00155     # We don't process the same header twice.
00156     if gtest_header_path in processed_files:
00157       return
00158 
00159     processed_files.add(gtest_header_path)
00160 
00161     # Reads each line in the given gtest header.
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         # It's '#include "gtest/..."' - let's process it recursively.
00166         ProcessFile('include/' + m.group(1))
00167       else:
00168         # Otherwise we copy the line unchanged to the output file.
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     # We don't process the same #included file twice.
00184     if gtest_source_file in processed_files:
00185       return
00186 
00187     processed_files.add(gtest_source_file)
00188 
00189     # Reads each line in the given gtest source file.
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           # It's '#include "gtest/gtest-spi.h"'.  This file is not
00195           # #included by "gtest/gtest.h", so we need to process it.
00196           ProcessFile(GTEST_SPI_H_SEED)
00197         else:
00198           # It's '#include "gtest/foo.h"' where foo is not gtest-spi.
00199           # We treat it as '#include "gtest/gtest.h"', as all other
00200           # gtest headers are being fused into gtest.h and cannot be
00201           # #included directly.
00202 
00203           # There is no need to #include "gtest/gtest.h" more than once.
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           # It's '#include "src/foo"' - let's process it recursively.
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     # fuse_gtest_files.py OUTPUT_DIR
00240     FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
00241   elif argc == 3:
00242     # fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR
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()


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:02