gmock/gtest/scripts/fuse_gtest_files.py
Go to the documentation of this file.
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 http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide for
56 more information.
57 """
58 
59 __author__ = 'wan@google.com (Zhanyong Wan)'
60 
61 import os
62 import re
63 import sys
64 
65 # We assume that this file is in the scripts/ directory in the Google
66 # Test root directory.
67 DEFAULT_GTEST_ROOT_DIR = os.path.join(os.path.dirname(__file__), '..')
68 
69 # Regex for matching '#include "gtest/..."'.
70 INCLUDE_GTEST_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(gtest/.+)"')
71 
72 # Regex for matching '#include "src/..."'.
73 INCLUDE_SRC_FILE_REGEX = re.compile(r'^\s*#\s*include\s*"(src/.+)"')
74 
75 # Where to find the source seed files.
76 GTEST_H_SEED = 'include/gtest/gtest.h'
77 GTEST_SPI_H_SEED = 'include/gtest/gtest-spi.h'
78 GTEST_ALL_CC_SEED = 'src/gtest-all.cc'
79 
80 # Where to put the generated files.
81 GTEST_H_OUTPUT = 'gtest/gtest.h'
82 GTEST_ALL_CC_OUTPUT = 'gtest/gtest-all.cc'
83 
84 
85 def VerifyFileExists(directory, relative_path):
86  """Verifies that the given file exists; aborts on failure.
87 
88  relative_path is the file path relative to the given directory.
89  """
90 
91  if not os.path.isfile(os.path.join(directory, relative_path)):
92  print(('ERROR: Cannot find %s in directory %s.' % (relative_path,
93  directory)))
94  print ('Please either specify a valid project root directory '
95  'or omit it on the command line.')
96  sys.exit(1)
97 
98 
99 def ValidateGTestRootDir(gtest_root):
100  """Makes sure gtest_root points to a valid gtest root directory.
101 
102  The function aborts the program on failure.
103  """
104 
105  VerifyFileExists(gtest_root, GTEST_H_SEED)
106  VerifyFileExists(gtest_root, GTEST_ALL_CC_SEED)
107 
108 
109 def VerifyOutputFile(output_dir, relative_path):
110  """Verifies that the given output file path is valid.
111 
112  relative_path is relative to the output_dir directory.
113  """
114 
115  # Makes sure the output file either doesn't exist or can be overwritten.
116  output_file = os.path.join(output_dir, relative_path)
117  if os.path.exists(output_file):
118  # TODO(wan@google.com): The following user-interaction doesn't
119  # work with automated processes. We should provide a way for the
120  # Makefile to force overwriting the files.
121  print(('%s already exists in directory %s - overwrite it? (y/N) ' %
122  (relative_path, output_dir)))
123  answer = sys.stdin.readline().strip()
124  if answer not in ['y', 'Y']:
125  print('ABORTED.')
126  sys.exit(1)
127 
128  # Makes sure the directory holding the output file exists; creates
129  # it and all its ancestors if necessary.
130  parent_directory = os.path.dirname(output_file)
131  if not os.path.isdir(parent_directory):
132  os.makedirs(parent_directory)
133 
134 
135 def ValidateOutputDir(output_dir):
136  """Makes sure output_dir points to a valid output directory.
137 
138  The function aborts the program on failure.
139  """
140 
141  VerifyOutputFile(output_dir, GTEST_H_OUTPUT)
142  VerifyOutputFile(output_dir, GTEST_ALL_CC_OUTPUT)
143 
144 
145 def FuseGTestH(gtest_root, output_dir):
146  """Scans folder gtest_root to generate gtest/gtest.h in output_dir."""
147 
148  output_file = open(os.path.join(output_dir, GTEST_H_OUTPUT), 'w')
149  processed_files = set() # Holds all gtest headers we've processed.
150 
151  def ProcessFile(gtest_header_path):
152  """Processes the given gtest header file."""
153 
154  # We don't process the same header twice.
155  if gtest_header_path in processed_files:
156  return
157 
158  processed_files.add(gtest_header_path)
159 
160  # Reads each line in the given gtest header.
161  for line in open(os.path.join(gtest_root, gtest_header_path), 'r'):
162  m = INCLUDE_GTEST_FILE_REGEX.match(line)
163  if m:
164  # It's '#include "gtest/..."' - let's process it recursively.
165  ProcessFile('include/' + m.group(1))
166  else:
167  # Otherwise we copy the line unchanged to the output file.
168  output_file.write(line)
169 
170  ProcessFile(GTEST_H_SEED)
171  output_file.close()
172 
173 
174 def FuseGTestAllCcToFile(gtest_root, output_file):
175  """Scans folder gtest_root to generate gtest/gtest-all.cc in output_file."""
176 
177  processed_files = set()
178 
179  def ProcessFile(gtest_source_file):
180  """Processes the given gtest source file."""
181 
182  # We don't process the same #included file twice.
183  if gtest_source_file in processed_files:
184  return
185 
186  processed_files.add(gtest_source_file)
187 
188  # Reads each line in the given gtest source file.
189  for line in open(os.path.join(gtest_root, gtest_source_file), 'r'):
190  m = INCLUDE_GTEST_FILE_REGEX.match(line)
191  if m:
192  if 'include/' + m.group(1) == GTEST_SPI_H_SEED:
193  # It's '#include "gtest/gtest-spi.h"'. This file is not
194  # #included by "gtest/gtest.h", so we need to process it.
195  ProcessFile(GTEST_SPI_H_SEED)
196  else:
197  # It's '#include "gtest/foo.h"' where foo is not gtest-spi.
198  # We treat it as '#include "gtest/gtest.h"', as all other
199  # gtest headers are being fused into gtest.h and cannot be
200  # #included directly.
201 
202  # There is no need to #include "gtest/gtest.h" more than once.
203  if not GTEST_H_SEED in processed_files:
204  processed_files.add(GTEST_H_SEED)
205  output_file.write('#include "%s"\n' % (GTEST_H_OUTPUT,))
206  else:
207  m = INCLUDE_SRC_FILE_REGEX.match(line)
208  if m:
209  # It's '#include "src/foo"' - let's process it recursively.
210  ProcessFile(m.group(1))
211  else:
212  output_file.write(line)
213 
214  ProcessFile(GTEST_ALL_CC_SEED)
215 
216 
217 def FuseGTestAllCc(gtest_root, output_dir):
218  """Scans folder gtest_root to generate gtest/gtest-all.cc in output_dir."""
219 
220  output_file = open(os.path.join(output_dir, GTEST_ALL_CC_OUTPUT), 'w')
221  FuseGTestAllCcToFile(gtest_root, output_file)
222  output_file.close()
223 
224 
225 def FuseGTest(gtest_root, output_dir):
226  """Fuses gtest.h and gtest-all.cc."""
227 
228  ValidateGTestRootDir(gtest_root)
229  ValidateOutputDir(output_dir)
230 
231  FuseGTestH(gtest_root, output_dir)
232  FuseGTestAllCc(gtest_root, output_dir)
233 
234 
235 def main():
236  argc = len(sys.argv)
237  if argc == 2:
238  # fuse_gtest_files.py OUTPUT_DIR
239  FuseGTest(DEFAULT_GTEST_ROOT_DIR, sys.argv[1])
240  elif argc == 3:
241  # fuse_gtest_files.py GTEST_ROOT_DIR OUTPUT_DIR
242  FuseGTest(sys.argv[1], sys.argv[2])
243  else:
244  print(__doc__)
245  sys.exit(1)
246 
247 
248 if __name__ == '__main__':
249  main()
def VerifyOutputFile(output_dir, relative_path)
def FuseGTestH(gtest_root, output_dir)
FMT_API void print(std::FILE *f, CStringRef format_str, ArgList args)
Definition: format.cc:873
def VerifyFileExists(directory, relative_path)
def FuseGTestAllCc(gtest_root, output_dir)
def FuseGTest(gtest_root, output_dir)
def FuseGTestAllCcToFile(gtest_root, output_file)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:05