2013-12-30 23:50:59 +01:00
|
|
|
# vim: set ts=8 sts=2 sw=2 tw=99 et:
|
|
|
|
import re
|
|
|
|
import os, sys
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
argv = sys.argv[1:]
|
|
|
|
if len(argv) < 2:
|
|
|
|
sys.stderr.write('Usage: generate_headers.py <source_path> <output_folder>\n')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
SourceFolder = os.path.abspath(os.path.normpath(argv[0]))
|
|
|
|
OutputFolder = os.path.normpath(argv[1])
|
|
|
|
|
2014-05-25 10:53:58 +02:00
|
|
|
class FolderChanger:
|
|
|
|
def __init__(self, folder):
|
|
|
|
self.old = os.getcwd()
|
|
|
|
self.new = folder
|
2013-12-30 23:50:59 +01:00
|
|
|
|
2014-05-25 10:53:58 +02:00
|
|
|
def __enter__(self):
|
|
|
|
if self.new:
|
|
|
|
os.chdir(self.new)
|
|
|
|
|
|
|
|
def __exit__(self, type, value, traceback):
|
|
|
|
os.chdir(self.old)
|
|
|
|
|
|
|
|
def run_and_return(argv):
|
2013-12-30 23:50:59 +01:00
|
|
|
# Python 2.6 doesn't have check_output.
|
|
|
|
if hasattr(subprocess, 'check_output'):
|
|
|
|
text = subprocess.check_output(argv)
|
|
|
|
if str != bytes:
|
|
|
|
text = str(text, 'utf-8')
|
|
|
|
else:
|
|
|
|
p = subprocess.Popen(argv, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
output, ignored = p.communicate()
|
|
|
|
rval = p.poll()
|
|
|
|
if rval:
|
|
|
|
raise subprocess.CalledProcessError(rval, argv)
|
|
|
|
text = output.decode('utf8')
|
2014-05-25 10:53:58 +02:00
|
|
|
return text.strip()
|
2013-12-30 23:50:59 +01:00
|
|
|
|
2014-05-25 10:53:58 +02:00
|
|
|
def get_git_version():
|
|
|
|
revision_count = run_and_return(['git', 'rev-list', '--count', 'HEAD'])
|
|
|
|
revision_hash = run_and_return(['git', 'log', '--pretty=format:%h:%H', '-n', '1'])
|
|
|
|
shorthash, longhash = revision_hash.split(':')
|
|
|
|
|
|
|
|
return revision_count, shorthash, longhash
|
2013-12-30 23:50:59 +01:00
|
|
|
|
|
|
|
def output_version_headers():
|
2014-05-25 10:53:58 +02:00
|
|
|
with FolderChanger(SourceFolder):
|
|
|
|
count, shorthash, longhash = get_git_version()
|
2013-12-30 23:50:59 +01:00
|
|
|
|
|
|
|
with open(os.path.join(SourceFolder, 'product.version')) as fp:
|
2017-09-10 02:17:35 +02:00
|
|
|
contents = fp.read().strip()
|
2013-12-30 23:50:59 +01:00
|
|
|
m = re.match('(\d+)\.(\d+)\.(\d+)-?(.*)', contents)
|
|
|
|
if m == None:
|
|
|
|
raise Exception('Could not detremine product version')
|
|
|
|
major, minor, release, tag = m.groups()
|
2015-09-29 12:07:16 +02:00
|
|
|
product = "{0}.{1}.{2}.{3}".format(major, minor, release, count)
|
2014-05-25 10:53:58 +02:00
|
|
|
fullstring = product
|
2013-12-30 23:50:59 +01:00
|
|
|
if tag != "":
|
|
|
|
fullstring += "-{0}".format(tag)
|
|
|
|
|
|
|
|
with open(os.path.join(OutputFolder, 'sourcemod_version_auto.h'), 'w') as fp:
|
|
|
|
fp.write("""
|
|
|
|
#ifndef _SOURCEMOD_AUTO_VERSION_INFORMATION_H_
|
|
|
|
#define _SOURCEMOD_AUTO_VERSION_INFORMATION_H_
|
|
|
|
|
|
|
|
#define SM_BUILD_TAG \"{0}\"
|
2014-05-25 10:53:58 +02:00
|
|
|
#define SM_BUILD_CSET \"{1}\"
|
|
|
|
#define SM_BUILD_MAJOR \"{2}\"
|
|
|
|
#define SM_BUILD_MINOR \"{3}\"
|
|
|
|
#define SM_BUILD_RELEASE \"{4}\"
|
2014-05-25 12:20:50 +02:00
|
|
|
#define SM_BUILD_LOCAL_REV \"{6}\"
|
2013-12-30 23:50:59 +01:00
|
|
|
|
2015-09-29 12:07:16 +02:00
|
|
|
#define SM_BUILD_UNIQUEID SM_BUILD_LOCAL_REV \":\" SM_BUILD_CSET
|
2013-12-30 23:50:59 +01:00
|
|
|
|
2014-05-25 10:53:58 +02:00
|
|
|
#define SM_VERSION_STRING \"{5}\"
|
2015-09-29 12:07:16 +02:00
|
|
|
#define SM_VERSION_FILE {2},{3},{4},{6}
|
2013-12-30 23:50:59 +01:00
|
|
|
|
|
|
|
#endif /* _SOURCEMOD_AUTO_VERSION_INFORMATION_H_ */
|
2014-05-25 12:20:50 +02:00
|
|
|
""".format(tag, shorthash, major, minor, release, fullstring, count))
|
2013-12-30 23:50:59 +01:00
|
|
|
|
|
|
|
with open(os.path.join(OutputFolder, 'version_auto.inc'), 'w') as fp:
|
|
|
|
fp.write("""
|
|
|
|
#if defined _auto_version_included
|
|
|
|
#endinput
|
|
|
|
#endif
|
|
|
|
#define _auto_version_included
|
|
|
|
|
|
|
|
#define SOURCEMOD_V_TAG \"{0}\"
|
2014-05-25 10:53:58 +02:00
|
|
|
#define SOURCEMOD_V_CSET \"{1}\"
|
|
|
|
#define SOURCEMOD_V_MAJOR {2}
|
|
|
|
#define SOURCEMOD_V_MINOR {3}
|
|
|
|
#define SOURCEMOD_V_RELEASE {4}
|
2014-05-25 12:18:05 +02:00
|
|
|
#define SOURCEMOD_V_REV {6}
|
2014-05-25 10:53:58 +02:00
|
|
|
|
|
|
|
#define SOURCEMOD_VERSION \"{5}\"
|
2014-05-25 12:18:05 +02:00
|
|
|
""".format(tag, shorthash, major, minor, release, fullstring, count))
|
2013-12-30 23:50:59 +01:00
|
|
|
|
|
|
|
output_version_headers()
|