The Gaudi Framework  v33r1 (b1225454)
createProjVersHeader.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
12 from __future__ import print_function
13 import os
14 import sys
15 import re
16 from optparse import OptionParser
17 
18 lhcb_ver_style = "v(?P<maj_ver>[0-9]+)r(?P<min_ver>[0-9]+)(?:p(?P<pat_ver>[0-9]+))?"
19 atlas_ver_style = "[A-Za-z]+\-(?P<maj_ver>[0-9]+)\-(?P<min_ver>[0-9]+)(?:\-(?P<pat_ver>[0-9]+))?"
20 plain_ver_style = "(?P<maj_ver>[0-9]+)\.(?P<min_ver>[0-9]+)(?:\.(?P<pat_ver>[0-9]+))?"
21 
22 
23 def main():
24  parser = OptionParser(
25  usage="ERROR: Usage %prog <project> <version> <outputfile>")
26  parser.add_option(
27  "-q", "--quiet", action="store_true", help="Do not print messages.")
28  opts, args = parser.parse_args()
29  if len(args) != 3:
30  parser.error("wrong number of arguments")
31 
32  project, version, outputfile = args
33  if not opts.quiet:
34  print("Creating %s for %s %s" % (outputfile, project, version))
35 
36  for style in [lhcb_ver_style, atlas_ver_style, plain_ver_style]:
37  m = re.match(style, version)
38  if m:
39  majver = int(m.groupdict()['maj_ver'])
40  minver = int(m.groupdict()['min_ver'])
41  patver = int(m.groupdict()['pat_ver'] or 0)
42  break
43  else:
44  # anything that is not one of the explicit version syntaxes is handled
45  # in the same way, e.g. "HEAD"
46  majver, minver, patver = 999, 999, 0
47 
48  outdir = os.path.dirname(outputfile)
49  if not os.path.exists(outdir):
50  if not opts.quiet:
51  print("Creating directory", outdir)
52  os.makedirs(outdir)
53 
54  # Prepare data to be written
55  outputdata = """#ifndef %(proj)s_VERSION
56 /* Automatically generated file: do not modify! */
57 #ifndef CALC_GAUDI_VERSION
58 #define CALC_GAUDI_VERSION(maj,min) (((maj) << 16) + (min))
59 #endif
60 #define %(proj)s_MAJOR_VERSION %(maj)d
61 #define %(proj)s_MINOR_VERSION %(min)d
62 #define %(proj)s_PATCH_VERSION %(pat)d
63 #define %(proj)s_VERSION CALC_GAUDI_VERSION(%(proj)s_MAJOR_VERSION,%(proj)s_MINOR_VERSION)
64 #endif
65 """ % {
66  'proj': project.upper(),
67  'min': minver,
68  'maj': majver,
69  'pat': patver
70  }
71 
72  # Get the current content of the destination file (if any)
73  try:
74  f = open(outputfile, "r")
75  olddata = f.read()
76  f.close()
77  except IOError:
78  olddata = None
79 
80  # Overwrite the file only if there are changes
81  if outputdata != olddata:
82  open(outputfile, "w").write(outputdata)
83 
84 
85 if __name__ == "__main__":
86  main()