tools/versions.py

66 lines
2.6 KiB
Python

#! /usr/bin/env python3
if __name__ == "__main__":
import argparse
import os
import yaml
import sys
# Parse
parser = argparse.ArgumentParser(description="Generate build scripts for differend versions.")
parser.add_argument("source", metavar="source", type=str, nargs=1, help="The source build script to modify.")
parser.add_argument("versions", metavar="versions", type=str, nargs="+", help="The files they contain the version information.")
parser.add_argument("-f", "--files", dest="files", type=str, nargs="?", default=".", help="The target directory to save the generated scripts in. Default: \".\"")
parser.add_argument("-l", "--list", dest="list", action="store_const", const=True, default=False, help="Output all gerenated file names.")
parser.add_argument("-L", "--list-full", dest="list_full", action="store_const", const=True, default=False, help="Like --list with path from --files.")
parser.add_argument("-n", "--names", dest="names", type=str, nargs="?", default=None, help="Are csv file with first the name and second the version name.")
args = parser.parse_args()
# Load source file
fileData = open(args.source[0], "r").read()
tmpName = os.path.basename(args.source[0])
tmp = tmpName.rfind(".")
if tmp >= 0:
configStart = tmpName[:tmp]
configEnding = tmpName[tmp:]
else:
configStart = tmpName[0]
configEnding = ""
# Parse versions
versions = {}
for version_file in args.versions:
for iName, iValue in yaml.load(open(version_file, "r").read(), Loader=yaml.SafeLoader).items():
versions[iName] = iValue
# Gen mapping
mapping = {}
if args.names is None:
for iName in versions.keys():
mapping[iName] = iName
else:
for i in filter(bool, open(args.names, "r").read().splitlines()):
tmp = i.split(",")
assert len(tmp) == 2
mapping[tmp[0]] = tmp[1]
# Gen build scripts
for version_name, version in mapping.items():
# Gen config file
tmp = fileData.replace("{{VERSION}}", version)
tmp = tmp.replace("{{VERSION_NAME}}", version_name)
for attributeID, attribute in versions[version].items():
tmp = tmp.replace("{{" + attributeID + "}}", attribute)
# Save file
outFileName = "%s_%s%s" % (configStart, version_name, configEnding)
outFile = os.path.join(args.files, outFileName)
with open(outFile, "w") as f:
f.write(tmp)
# Log output
if args.list_full:
print(outFile)
else:
print(outFileName)