createProjVersHeader.py 2.3 KB
Newer Older
1
2
3
4
5
6
#!/usr/bin/env python

import os
import sys
import re
from optparse import OptionParser
7
8
9
10
11
12

lhcb_ver_style = "v(?P<maj_ver>[0-9]+)r(?P<min_ver>[0-9]+)(?:p(?P<pat_ver>[0-9]+))?"
atlas_ver_style = "[A-Za-z]+\-(?P<maj_ver>[0-9]+)\-(?P<min_ver>[0-9]+)(?:\-(?P<pat_ver>[0-9]+))?"
plain_ver_style = "(?P<maj_ver>[0-9]+)\.(?P<min_ver>[0-9]+)(?:\.(?P<pat_ver>[0-9]+))?"


marcocle's avatar
marcocle committed
13
def main():
14
15
16
17
18
19
    parser = OptionParser(usage="ERROR: Usage %prog <project> <version> <outputfile>")
    parser.add_option("-q", "--quiet", action="store_true",
                      help="Do not print messages.")
    opts, args = parser.parse_args()
    if len(args) != 3:
        parser.error("wrong number of arguments")
20

21
22
23
    project, version, outputfile = args
    if not opts.quiet:
        print "Creating %s for %s %s" % (outputfile, project, version)
24
25
26
27
28
29
30
31
    
    for style in [lhcb_ver_style, atlas_ver_style, plain_ver_style ] :
        m = re.match(style, version)
        if m :
            majver = int(m.groupdict()['maj_ver'])
            minver = int(m.groupdict()['min_ver'])
            patver = int(m.groupdict()['pat_ver'] or 0)
            break
32
    else:
33
34
35
        # anything that is not one of the explicit version syntaxes is handled
        # in the same way, e.g. "HEAD"
        majver, minver, patver = 999, 999, 0
36

marcocle's avatar
marcocle committed
37
38
    outdir = os.path.dirname(outputfile)
    if not os.path.exists(outdir):
39
40
        if not opts.quiet:
            print "Creating directory", outdir
marcocle's avatar
marcocle committed
41
        os.makedirs(outdir)
42

43
44
    # Prepare data to be written
    outputdata = """#ifndef %(proj)s_VERSION
marcocle's avatar
marcocle committed
45
46
47
48
49
50
/* Automatically generated file: do not modify! */
#ifndef CALC_GAUDI_VERSION
#define CALC_GAUDI_VERSION(maj,min) (((maj) << 16) + (min))
#endif
#define %(proj)s_MAJOR_VERSION %(maj)d
#define %(proj)s_MINOR_VERSION %(min)d
51
#define %(proj)s_PATCH_VERSION %(pat)d
marcocle's avatar
marcocle committed
52
53
#define %(proj)s_VERSION CALC_GAUDI_VERSION(%(proj)s_MAJOR_VERSION,%(proj)s_MINOR_VERSION)
#endif
54
55
56
57
58
59
60
61
62
63
64
65
66
""" % { 'proj': project.upper(), 'min': minver, 'maj': majver, 'pat': patver }

    # Get the current content of the destination file (if any)
    try:
        f = open(outputfile, "r")
        olddata = f.read()
        f.close()
    except IOError:
        olddata = None

    # Overwrite the file only if there are changes
    if outputdata != olddata:
        open(outputfile, "w").write(outputdata)
marcocle's avatar
marcocle committed
67
68
69

if __name__ == "__main__":
    main()