?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download

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