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') blacklist_str = content[0] # warum das als Liste erkannt wird wissen die Götter... if len(blacklist_str) == 0 or len(content) == 0: print(f"import of '{blacklist_ini_filename}' from '{blacklist_net_share}' failed") exit blacklist = blacklist_str.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") 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.") sys.exit(0) else: print(f"'Output directory for .json-file {out_dir}' doesn't exist. Check cremig_setenv.bat or VPN connection.") sys.exit(0) else: print(f"Can't execute SIVAS-database query. Check VPN connection and check the path of %SIVAS_DATABASE_QUERY% in cremig_setenv.bat") sys.exit(0) def get_sivas_teilestamm(sivas_ids, out_dir): """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(" ", "") out_file = os.path.join(out_dir, id + ".csv") if not os.path.isfile(out_file): 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') 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: get_sivas_teilestamm(args.sivasid, out_dir) else: get_sivas_dbase(args.sivasid, out_dir) elif args.blacklist: get_blacklist(out_dir) else: parser.print_help()