createProjVersHeader.py 2.36 KB
Newer Older
1
#!/usr/bin/env python
2
from __future__ import print_function
3
4
5
6
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():
Marco Clemencic's avatar
Marco Clemencic committed
14
15
    parser = OptionParser(
        usage="ERROR: Usage %prog <project> <version> <outputfile>")
Gitlab CI's avatar
Gitlab CI committed
16
17
    parser.add_option(
        "-q", "--quiet", action="store_true", help="Do not print messages.")
18
19
20
    opts, args = parser.parse_args()
    if len(args) != 3:
        parser.error("wrong number of arguments")
21

22
23
    project, version, outputfile = args
    if not opts.quiet:
24
        print("Creating %s for %s %s" % (outputfile, project, version))
Marco Clemencic's avatar
Marco Clemencic committed
25
26

    for style in [lhcb_ver_style, atlas_ver_style, plain_ver_style]:
27
        m = re.match(style, version)
Marco Clemencic's avatar
Marco Clemencic committed
28
        if m:
29
30
31
32
            majver = int(m.groupdict()['maj_ver'])
            minver = int(m.groupdict()['min_ver'])
            patver = int(m.groupdict()['pat_ver'] or 0)
            break
33
    else:
34
35
36
        # 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
37

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

44
45
    # Prepare data to be written
    outputdata = """#ifndef %(proj)s_VERSION
marcocle's avatar
marcocle committed
46
47
48
49
50
51
/* 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
52
#define %(proj)s_PATCH_VERSION %(pat)d
marcocle's avatar
marcocle committed
53
54
#define %(proj)s_VERSION CALC_GAUDI_VERSION(%(proj)s_MAJOR_VERSION,%(proj)s_MINOR_VERSION)
#endif
Gitlab CI's avatar
Gitlab CI committed
55
56
57
58
59
60
""" % {
        'proj': project.upper(),
        'min': minver,
        'maj': majver,
        'pat': patver
    }
61
62
63
64
65
66
67
68
69
70
71
72

    # 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
73

Marco Clemencic's avatar
Marco Clemencic committed
74

marcocle's avatar
marcocle committed
75
76
if __name__ == "__main__":
    main()