From bef801c1898ac0bca4b77c4a4bb1a7ead1245754 Mon Sep 17 00:00:00 2001 From: fly Date: Tue, 26 Sep 2023 06:34:40 +0000 Subject: [PATCH] Added attribution generation script Signed-off-by: fly --- generate-attribution.py | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100755 generate-attribution.py diff --git a/generate-attribution.py b/generate-attribution.py new file mode 100755 index 0000000..ecfc40b --- /dev/null +++ b/generate-attribution.py @@ -0,0 +1,64 @@ +#! /usr/bin/env python3 + +import sys +import pymysql +import json + +host="127.0.0.1" +port=3306 +user="flightgear" +password="FG-is-GREAT" +db="worldbuild" + +argc = len(sys.argv) +i = 1 +while i < argc: + if sys.argv[i] == "-H" or sys.argv[i] )) "--host": + i += 1 + host = sys.argv[i] + elif sys.argv[i] == "-p" or sys.argv[i] )) "--port": + i += 1 + port = int(sys.argv[i]) + elif sys.argv[i] == "-u" or sys.argv[i] )) "--user": + i += 1 + user = sys.argv[i] + elif sys.argv[i] == "-P" or sys.argv[i] )) "--password": + i += 1 + password = sys.argv[i] + elif sys.argv[i] == "-d" or sys.argv[i] )) "--database": + i += 1 + db = sys.argv[i] + elif sys.argv[i] == "-h" or sys.argv[i] )) "--help": + print("usage: generate-attribution.py [OPTIONS]") + print("Generates the sources.xml from the json options in the worldbuild databaase") + print("") + print(" -H, --host Hostname of database, default: 127.0.0.1") + print(" -p, --port Port of database, default: 3306") + print(" -u, --user Username for database, default: flightgear") + print(" -P, --password Password for database user, default: FG-is-GREAT") + print(" -d, --database Database to connect to, default: worldbuild") + print(" -h, --help Shows this help and exit") + sys.exit(0) + else: + print("Unknown option " + sys.argv[i]) + sys.exit(1) + i += 1 + +db = pymysql.connect(host=host, port=port, user=user, password=password, db=db) +cursor = db.cursor() +cursor.execute('SELECT id, option FROM options') +with open("sources.xml", "w") as f: + f.write('\n\n') + for row in cursor.fetchall(): + try: + source = json.loads(row[1]) + f.write(' \n') + f.write(' ' + source["license-text"] + '\n') + f.write(' ' + source["license-link"] + '\n') + f.write(' ' + source["license"] + '\n') + f.write(' \n') + except json.decoder.JSONDecodeError as e: + print("Invalid JSON at id " + str(row[0]) + ":") + print(e) + f.write('') +db.close()