165 lines
6.7 KiB
Python
165 lines
6.7 KiB
Python
import argparse
|
|
import configparser
|
|
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
|
|
"""
|
|
Dieses Programm:
|
|
- Kopiert die CSV-Datei mit allen RuleDesigner-Nummern
|
|
- Fragt Sivas ab und generiert dabei eine json-Datei, in der alle Teilenummern aufgelistet sind,
|
|
die in Verbindung mit einer angegebenen Teilenummer migriert werden müssen.
|
|
"""
|
|
def get_blacklist(outdir):
|
|
"""read out list of sivas Ids which are not allowed from network drive
|
|
'//ruledesigner//DF_Daten//onfigurator//ustomVaulting//RP_BOM.ini'
|
|
within the section 'Settings'
|
|
in the key 'PartNumbersToDrop'
|
|
"""
|
|
blacklist_net_share = os.environ.get("BLACKLIST_DATABASE_SOURCE")
|
|
blacklist_ini_filename = os.environ.get("BLACKLIST_DATABASE_NAME")
|
|
blacklist_source_path = os.path.join(blacklist_net_share, blacklist_ini_filename)
|
|
config = configparser.RawConfigParser()
|
|
try:
|
|
config.read(blacklist_source_path)
|
|
except configparser.DuplicateOptionError:
|
|
pass
|
|
|
|
content = config.get('Settings','PartNumbersToDrop')
|
|
|
|
if type(content) == list:
|
|
content = content[0] # Wird benötigt, weil der String mit den Nummern aus der Blackliste manchmal in eine Liste gepackt wird, manchmal nicht - warum wissen die Götter...
|
|
|
|
if len(content) == 0 or len(content) == 0:
|
|
print(f"import of '{blacklist_ini_filename}' from '{blacklist_net_share}' failed")
|
|
exit
|
|
|
|
blacklist = content.split(';')
|
|
|
|
print("writing txt-file of blacklist ids...")
|
|
outfile = os.path.join(outdir, 'blacklist.txt')
|
|
with open(outfile, 'w', encoding='utf-8') as fh:
|
|
fh.write('\n'.join(blacklist))
|
|
print("done")
|
|
|
|
def get_rd_dbase(out_dir=os.environ.get('CREMIG_DATA')):
|
|
|
|
rd_csv_source_dir = os.environ.get("RD_DATABASE_SOURCE")
|
|
rd_csv_filename = os.environ.get("RD_DATABASE_NAME")
|
|
csv_source_path = os.path.join(rd_csv_source_dir, rd_csv_filename)
|
|
|
|
if os.path.isfile(csv_source_path):
|
|
csv_destiny_path = os.path.join(out_dir, rd_csv_filename)
|
|
|
|
if os.path.isdir(out_dir):
|
|
print("Copying CSV-file of RuleDesigner database...")
|
|
shutil.copyfile(csv_source_path, csv_destiny_path)
|
|
print("done")
|
|
else:
|
|
print(f"Can't copy RuleDesigner-database, because output directory ${out_dir} doesn't exist. Check %CREMIG_DATA% in cremig_setenv.bat")
|
|
sys.exit(0)
|
|
else:
|
|
print(f"File with ruldesigner-partnumbers ${csv_source_path} doesn't exist. Check VPN connection and %RD_DATABASE_SOURCE% + %RD_DATABASE_NAME% in cremig_setenv.bat")
|
|
sys.exit(0)
|
|
|
|
def get_sivas_dbase(sivas_id, out_dir):
|
|
sivas_exe_path = os.environ.get('SIVAS_DATABASE_QUERY')
|
|
sivas_id = sivas_id.replace(".json", "")
|
|
|
|
if os.path.isfile(sivas_exe_path):
|
|
|
|
if os.path.isdir(out_dir):
|
|
print(f"Attempting to generate a json-file of all parts to be migrated in relation with '{sivas_id}'")
|
|
subprocess.run(f'{sivas_exe_path} -o {out_dir} --format json -number "{sivas_id}"')
|
|
|
|
if os.path.isfile(os.path.join(out_dir, sivas_id + ".json")):
|
|
print("done")
|
|
return True
|
|
else:
|
|
print(f"Failure - Couldn't generate {sivas_id}.json. Check for any spelling/typing errors. Otherwise given number is a part without a BOM.")
|
|
return False
|
|
else:
|
|
print(f"'Output directory for .json-file {out_dir}' doesn't exist. Check cremig_setenv.bat or VPN connection.")
|
|
return False
|
|
else:
|
|
print(f"Can't execute SIVAS-database query. Check VPN connection and check the path of %SIVAS_DATABASE_QUERY% in cremig_setenv.bat")
|
|
return False
|
|
|
|
def get_sivas_teilestamm(sivas_ids, out_dir, overwrite=True):
|
|
"""call the sivas exe for getting all csv files with the item data for the given sivas ids"""
|
|
|
|
sivas_teilestamm_exe = os.environ.get('SIVAS_TEILESTAMM')
|
|
|
|
if isinstance(sivas_ids, str):
|
|
sivas_ids = list(set(sivas_ids.strip().split(",")))
|
|
|
|
ids_to_generate = []
|
|
for id in sivas_ids:
|
|
id = id.replace(" ", "")
|
|
if not overwrite:
|
|
out_file = os.path.join(out_dir, id + ".csv")
|
|
if not os.path.isfile(out_file):
|
|
ids_to_generate.append(id)
|
|
continue
|
|
else:
|
|
ids_to_generate.append(id)
|
|
|
|
if not id.isnumeric():
|
|
print(f"Partnumber '{id}' contains invalid characters. Please check given partnumber. Multiple partnumbers have to be specified as comma separated values without space, e.g. 200000089,200000090")
|
|
|
|
|
|
if os.path.isfile(sivas_teilestamm_exe):
|
|
|
|
if os.path.isdir(out_dir):
|
|
|
|
if len(ids_to_generate) == 0:
|
|
print("CSV-file(s) already exist or given partnumber(s) are migrated.")
|
|
return
|
|
|
|
else:
|
|
print(f"Attempting to generate csv-file(s) for given partnumber(s)...")
|
|
|
|
ids_to_generate = ",".join(ids_to_generate)
|
|
os.environ["SIVAS_EXCEL_EXPORT_DIR"] = out_dir
|
|
subprocess.run(f"{sivas_teilestamm_exe} -teilenr={ids_to_generate} -open_target_dir=n")
|
|
else:
|
|
print(f"Output directory for .csv-file(s) ${out_dir} doesn't exist.")
|
|
else:
|
|
print(f"Can't execute SIVAS-database query. Check VPN connection or check the path of %SIVAS_DATABASE_QUERY% in cremig_setenv.bat")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description='fetches data from sivas or ruledesigner fusion', prog='update_database')
|
|
parser.add_argument('-s', '--sivasid', action='store', help='fetch data of this id from sivas fo local database folder', metavar='id')
|
|
parser.add_argument('-r', '--rd', action='store_true', help='fetch list of existing parts from network drive')
|
|
parser.add_argument('-b', '--blacklist', action='store_true', help='fetch list of not allowed parts from network drive')
|
|
parser.add_argument('-t', '--teilestamm', action='store_true', help='fetch teilestamm-data for given partnumbers as csv')
|
|
parser.add_argument('-w', '--overwrite', action='store_true', help='vorhandene CSV-Dateien werden nicht erneut aus SIVAS abgefragt')
|
|
|
|
args = parser.parse_args()
|
|
|
|
out_dir = os.environ.get('CREMIG_DATA')
|
|
|
|
if args.rd:
|
|
get_rd_dbase(out_dir)
|
|
|
|
elif args.sivasid:
|
|
|
|
if args.teilestamm:
|
|
|
|
overwrite = args.overwrite
|
|
if overwrite:
|
|
get_sivas_teilestamm(args.sivasid, out_dir, overwrite = True)
|
|
else:
|
|
get_sivas_teilestamm(args.sivasid, out_dir, overwrite = False)
|
|
|
|
else:
|
|
get_sivas_dbase(args.sivasid, out_dir)
|
|
|
|
elif args.blacklist:
|
|
get_blacklist(out_dir)
|
|
|
|
else:
|
|
parser.print_help()
|