Home | History | Annotate | Download | only in Scripts
      1 #/usr/bin/python
      2 
      3 """A script to prepare version informtion for use the gtest Info.plist file.
      4 
      5   This script extracts the version information from the configure.ac file and
      6   uses it to generate a header file containing the same information. The
      7   #defines in this header file will be included in during the generation of
      8   the Info.plist of the framework, giving the correct value to the version
      9   shown in the Finder.
     10 
     11   This script makes the following assumptions (these are faults of the script,
     12   not problems with the Autoconf):
     13     1. The AC_INIT macro will be contained within the first 1024 characters
     14        of configure.ac
     15     2. The version string will be 3 integers separated by periods and will be
     16        surrounded by squre brackets, "[" and "]" (e.g. [1.0.1]). The first 
     17        segment represents the major version, the second represents the minor
     18        version and the third represents the fix version.
     19     3. No ")" character exists between the opening "(" and closing ")" of
     20        AC_INIT, including in comments and character strings.
     21 """
     22 
     23 import sys
     24 import re
     25 
     26 # Read the command line argument (the output directory for Version.h)
     27 if (len(sys.argv) < 3):
     28   print "Usage: /usr/bin/python versiongenerate.py input_dir output_dir"
     29   sys.exit(1)
     30 else:
     31   input_dir = sys.argv[1]
     32   output_dir = sys.argv[2]
     33 
     34 # Read the first 1024 characters of the configure.ac file
     35 config_file = open("%s/configure.ac" % input_dir, 'r')
     36 buffer_size = 1024
     37 opening_string = config_file.read(buffer_size)
     38 config_file.close()
     39 
     40 # Extract the version string from the AC_INIT macro
     41 #   The following init_expression means:
     42 #     Extract three integers separated by periods and surrounded by squre
     43 #     brackets(e.g. "[1.0.1]") between "AC_INIT(" and ")". Do not be greedy
     44 #     (*? is the non-greedy flag) since that would pull in everything between
     45 #     the first "(" and the last ")" in the file.
     46 version_expression = re.compile(r"AC_INIT\(.*?\[(\d+)\.(\d+)\.(\d+)\].*?\)",
     47                                 re.DOTALL)
     48 version_values = version_expression.search(opening_string)
     49 major_version = version_values.group(1)
     50 minor_version = version_values.group(2)
     51 fix_version = version_values.group(3)
     52 
     53 # Write the version information to a header file to be included in the
     54 # Info.plist file.
     55 file_data = """//
     56 // DO NOT MODIFY THIS FILE (but you can delete it)
     57 //
     58 // This file is autogenerated by the versiongenerate.py script. This script
     59 // is executed in a "Run Script" build phase when creating gtest.framework. This
     60 // header file is not used during compilation of C-source. Rather, it simply
     61 // defines some version strings for substitution in the Info.plist. Because of
     62 // this, we are not not restricted to C-syntax nor are we using include guards.
     63 //
     64 
     65 #define GTEST_VERSIONINFO_SHORT %s.%s
     66 #define GTEST_VERSIONINFO_LONG %s.%s.%s
     67 
     68 """ % (major_version, minor_version, major_version, minor_version, fix_version)
     69 version_file = open("%s/Version.h" % output_dir, 'w')
     70 version_file.write(file_data)
     71 version_file.close()
     72