tools/versions.py

49 lines
2.0 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.")
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
for version_file in args.versions:
versions = yaml.load(open(version_file, "r").read(), Loader=yaml.SafeLoader)
for version, attributes in versions.items():
# Gen config file
tmp = fileData.replace("{{VERSION}}", version)
for attributeID, attribute in attributes.items():
tmp = tmp.replace("{{" + attributeID + "}}", attribute)
# Save file
outFileName = "%s_%s%s" % (configStart, version, 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)