fuse_gmock_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_gmock_files.py v0.1.0
00033 Fuses Google Mock and Google Test source code into two .h files and a .cc file.
00034 
00035 SYNOPSIS
00036        fuse_gmock_files.py [GMOCK_ROOT_DIR] OUTPUT_DIR
00037 
00038        Scans GMOCK_ROOT_DIR for Google Mock and Google Test source
00039        code, assuming Google Test is in the GMOCK_ROOT_DIR/gtest
00040        sub-directory, and generates three files:
00041        OUTPUT_DIR/gtest/gtest.h, OUTPUT_DIR/gmock/gmock.h, and
00042        OUTPUT_DIR/gmock-gtest-all.cc.  Then you can build your tests
00043        by adding OUTPUT_DIR to the include search path and linking
00044        with OUTPUT_DIR/gmock-gtest-all.cc.  These three files contain
00045        everything you need to use Google Mock.  Hence you can
00046        "install" Google Mock by copying them to wherever you want.
00047 
00048        GMOCK_ROOT_DIR can be omitted and defaults to the parent
00049        directory of the directory holding this script.
00050 
00051 EXAMPLES
00052        ./fuse_gmock_files.py fused_gmock
00053        ./fuse_gmock_files.py path/to/unpacked/gmock fused_gmock
00054 
00055 This tool is experimental.  In particular, it assumes that there is no
00056 conditional inclusion of Google Mock or Google Test headers.  Please
00057 report any problems to googlemock@googlegroups.com.  You can read
00058 http://code.google.com/p/googlemock/wiki/CookBook for more
00059 information.
00060 """
00061 
00062 __author__ = 'wan@google.com (Zhanyong Wan)'
00063 
00064 import os
00065 import re
00066 import sets
00067 import sys
00068 
00069 # We assume that this file is in the scripts/ directory in the Google
00070 # Mock root directory.
00071 DEFAULT_GMOCK_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
00072 
00073 # We need to call into gtest/scripts/fuse_gtest_files.py.
00074 sys.path.append(os.path.join(DEFAULT_GMOCK_ROOT_DIR, 'gtest/scripts'))
00075 import fuse_gtest_files
00076 gtest = fuse_gtest_files
00077 
00078 # Regex for matching '#include "gmock/..."'.
00079 INCLUDE_GMOCK_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gmock/.+)"')
00080 
00081 # Where to find the source seed files.
00082 GMOCK_H_SEED = 'include/gmock/gmock.h'
00083 GMOCK_ALL_CC_SEED = 'src/gmock-all.cc'
00084 
00085 # Where to put the generated files.
00086 GTEST_H_OUTPUT = 'gtest/gtest.h'
00087 GMOCK_H_OUTPUT = 'gmock/gmock.h'
00088 GMOCK_GTEST_ALL_CC_OUTPUT = 'gmock-gtest-all.cc'
00089 
00090 
00091 def GetGTestRootDir(gmock_root):
00092   """Returns the root directory of Google Test."""
00093 
00094   return os.path.join(gmock_root, 'gtest')
00095 
00096 
00097 def ValidateGMockRootDir(gmock_root):
00098   """Makes sure gmock_root points to a valid gmock root directory.
00099 
00100   The function aborts the program on failure.
00101   """
00102 
00103   gtest.ValidateGTestRootDir(GetGTestRootDir(gmock_root))
00104   gtest.VerifyFileExists(gmock_root, GMOCK_H_SEED)
00105   gtest.VerifyFileExists(gmock_root, GMOCK_ALL_CC_SEED)
00106 
00107 
00108 def ValidateOutputDir(output_dir):
00109   """Makes sure output_dir points to a valid output directory.
00110 
00111   The function aborts the program on failure.
00112   """
00113 
00114   gtest.VerifyOutputFile(output_dir, gtest.GTEST_H_OUTPUT)
00115   gtest.VerifyOutputFile(output_dir, GMOCK_H_OUTPUT)
00116   gtest.VerifyOutputFile(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT)
00117 
00118 
00119 def FuseGMockH(gmock_root, output_dir):
00120   """Scans folder gmock_root to generate gmock/gmock.h in output_dir."""
00121 
00122   output_file = file(os.path.join(output_dir, GMOCK_H_OUTPUT), 'w')
00123   processed_files = sets.Set()  # Holds all gmock headers we've processed.
00124 
00125   def ProcessFile(gmock_header_path):
00126     """Processes the given gmock header file."""
00127 
00128     # We don't process the same header twice.
00129     if gmock_header_path in processed_files:
00130       return
00131 
00132     processed_files.add(gmock_header_path)
00133 
00134     # Reads each line in the given gmock header.
00135     for line in file(os.path.join(gmock_root, gmock_header_path), 'r'):
00136       m = INCLUDE_GMOCK_FILE_REGEX.match(line)
00137       if m:
00138         # It's '#include "gmock/..."' - let's process it recursively.
00139         ProcessFile('include/' + m.group(1))
00140       else:
00141         m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
00142         if m:
00143           # It's '#include "gtest/foo.h"'.  We translate it to
00144           # "gtest/gtest.h", regardless of what foo is, since all
00145           # gtest headers are fused into gtest/gtest.h.
00146 
00147           # There is no need to #include gtest.h twice.
00148           if not gtest.GTEST_H_SEED in processed_files:
00149             processed_files.add(gtest.GTEST_H_SEED)
00150             output_file.write('#include "%s"\n' % (gtest.GTEST_H_OUTPUT,))
00151         else:
00152           # Otherwise we copy the line unchanged to the output file.
00153           output_file.write(line)
00154 
00155   ProcessFile(GMOCK_H_SEED)
00156   output_file.close()
00157 
00158 
00159 def FuseGMockAllCcToFile(gmock_root, output_file):
00160   """Scans folder gmock_root to fuse gmock-all.cc into output_file."""
00161 
00162   processed_files = sets.Set()
00163 
00164   def ProcessFile(gmock_source_file):
00165     """Processes the given gmock source file."""
00166 
00167     # We don't process the same #included file twice.
00168     if gmock_source_file in processed_files:
00169       return
00170 
00171     processed_files.add(gmock_source_file)
00172 
00173     # Reads each line in the given gmock source file.
00174     for line in file(os.path.join(gmock_root, gmock_source_file), 'r'):
00175       m = INCLUDE_GMOCK_FILE_REGEX.match(line)
00176       if m:
00177         # It's '#include "gmock/foo.h"'.  We treat it as '#include
00178         # "gmock/gmock.h"', as all other gmock headers are being fused
00179         # into gmock.h and cannot be #included directly.
00180 
00181         # There is no need to #include "gmock/gmock.h" more than once.
00182         if not GMOCK_H_SEED in processed_files:
00183           processed_files.add(GMOCK_H_SEED)
00184           output_file.write('#include "%s"\n' % (GMOCK_H_OUTPUT,))
00185       else:
00186         m = gtest.INCLUDE_GTEST_FILE_REGEX.match(line)
00187         if m:
00188           # It's '#include "gtest/..."'.
00189           # There is no need to #include gtest.h as it has been
00190           # #included by gtest-all.cc.
00191           pass
00192         else:
00193           m = gtest.INCLUDE_SRC_FILE_REGEX.match(line)
00194           if m:
00195             # It's '#include "src/foo"' - let's process it recursively.
00196             ProcessFile(m.group(1))
00197           else:
00198             # Otherwise we copy the line unchanged to the output file.
00199             output_file.write(line)
00200 
00201   ProcessFile(GMOCK_ALL_CC_SEED)
00202 
00203 
00204 def FuseGMockGTestAllCc(gmock_root, output_dir):
00205   """Scans folder gmock_root to generate gmock-gtest-all.cc in output_dir."""
00206 
00207   output_file = file(os.path.join(output_dir, GMOCK_GTEST_ALL_CC_OUTPUT), 'w')
00208   # First, fuse gtest-all.cc into gmock-gtest-all.cc.
00209   gtest.FuseGTestAllCcToFile(GetGTestRootDir(gmock_root), output_file)
00210   # Next, append fused gmock-all.cc to gmock-gtest-all.cc.
00211   FuseGMockAllCcToFile(gmock_root, output_file)
00212   output_file.close()
00213 
00214 
00215 def FuseGMock(gmock_root, output_dir):
00216   """Fuses gtest.h, gmock.h, and gmock-gtest-all.h."""
00217 
00218   ValidateGMockRootDir(gmock_root)
00219   ValidateOutputDir(output_dir)
00220 
00221   gtest.FuseGTestH(GetGTestRootDir(gmock_root), output_dir)
00222   FuseGMockH(gmock_root, output_dir)
00223   FuseGMockGTestAllCc(gmock_root, output_dir)
00224 
00225 
00226 def main():
00227   argc = len(sys.argv)
00228   if argc == 2:
00229     # fuse_gmock_files.py OUTPUT_DIR
00230     FuseGMock(DEFAULT_GMOCK_ROOT_DIR, sys.argv[1])
00231   elif argc == 3:
00232     # fuse_gmock_files.py GMOCK_ROOT_DIR OUTPUT_DIR
00233     FuseGMock(sys.argv[1], sys.argv[2])
00234   else:
00235     print __doc__
00236     sys.exit(1)
00237 
00238 
00239 if __name__ == '__main__':
00240   main()


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40