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): 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') 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) if __name__ == '__main__': parser = argparse.ArgumentParser(description='fetches data from sivas or ruledesigner fusion', prog='update_database') parser.add_argument('-s', '--sivas', 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') args = parser.parse_args() out_dir = os.environ.get('CREMIG_DATA') if args.rd: get_rd_dbase(out_dir) elif args.sivas: get_sivas_dbase(args.sivas, out_dir) elif args.blacklist: get_blacklist(out_dir) else: parser.print_help()