blob: b948134c409a6bf4715fbdc434e823ca4bd7c56f [file] [log] [blame]
Kevin O'Connora6c87742015-10-13 15:09:40 -04001#!/usr/bin/env python
2# Generate version information for a program
3#
4# Copyright (C) 2015 Kevin O'Connor <kevin@koconnor.net>
5#
6# This file may be distributed under the terms of the GNU GPLv3 license.
7import sys, os, subprocess, time, socket, optparse
8
9VERSION_FORMAT = """
10/* DO NOT EDIT! This is an autogenerated file. See scripts/buildversion.py. */
11#define BUILD_VERSION "%s"
12"""
13
14# Obtain version info from "git" program
15def git_version():
16 if not os.path.exists('.git'):
17 return ""
18 params = "git describe --tags --long --dirty".split()
19 try:
20 ver = subprocess.check_output(params).decode().strip()
21 except:
22 return ""
23 return ver
24
25# Look for version in a ".version" file
26def file_version():
27 if not os.path.isfile('.version'):
28 return ""
29 try:
30 f = open('.version', 'r')
31 ver = f.readline().strip()
32 f.close()
33 except:
34 return ""
35 return ver
36
37# Generate an output file with the version information
38def write_version(outfile, version):
39 sys.stdout.write("Version: %s\n" % (version,))
40 f = open(outfile, 'w')
41 f.write(VERSION_FORMAT % (version,))
42 f.close()
43
44def main():
45 usage = "%prog [options] <outputheader.h>"
46 opts = optparse.OptionParser(usage)
47 opts.add_option("-e", "--extra", dest="extra", default="",
48 help="extra version string to append to version")
49
50 options, args = opts.parse_args()
51 if len(args) != 1:
52 opts.error("Incorrect arguments")
53 outfile = args[0]
54
55 ver = git_version()
56 if not ver:
57 ver = file_version()
58 if not ver:
59 ver = "?"
60 btime = time.strftime("%Y%m%d_%H%M%S")
61 hostname = socket.gethostname()
62 ver = "%s-%s-%s%s" % (ver, btime, hostname, options.extra)
63 write_version(outfile, ver)
64
65if __name__ == '__main__':
66 main()