# Script para copiar roms a partir de un xml de fbneo # Copia las roms por desarrollador y sin clones # Por hacer: # pasar por parametro si se quieren clones import os import shutil import sys import getopt from xml.dom import minidom # Inicialización de las opciones opt_manufacturer = "" opt_list = "no" opt_dat = "" opt_src_roms = "" opt_dst_roms = "" opt_copy = "no" opt_sort = "no" ignore_list = ["DECO Cassette"] # Comprueba los parametros try: opts, args = getopt.getopt( sys.argv[1:], "hlm:d:ci:o:s", ["help", "list", "manufacturer=", "dat=", "copy", "input=", "output=", "sort"], ) except getopt.GetoptError: print("test.py -i -o ") sys.exit(2) for opt, arg in opts: if opt in ("-h", "--help"): print("test.py -i -o ") sys.exit() elif opt in ("-l", "--list"): opt_list = "yes" elif opt in ("-m", "--manufacturer"): opt_manufacturer = arg elif opt in ("-d", "--dat"): opt_dat = arg elif opt in ("-c", "--copy"): opt_copy = "yes" elif opt in ("-i", "--input"): opt_src_roms = arg elif opt in ("-o", "--output"): opt_dst_roms = arg elif opt in ("-s", "--sort"): opt_sort = "yes" # Importa el xml if opt_dat == "" or not os.path.isfile(opt_dat): sys.exit(2) print("Parsing {} file".format(opt_dat)) file = minidom.parse(opt_dat) # Lista los desarrolladores if opt_list == "yes" and opt_manufacturer == "": print("List of all manufacturers:") manufacturers = [] games = file.getElementsByTagName("game") for game in games: manufacturer = game.getElementsByTagName("manufacturer")[0] manufacturers.append(manufacturer.firstChild.data) # Elimina los duplicados manufacturers = list(dict.fromkeys(manufacturers)) # Ordena la lista manufacturers.sort() # Imprime la lista for i in manufacturers: print(i) # Lista los juegos de un desarrollador if opt_list == "yes" and opt_manufacturer != "": print("List of all {} games".format(opt_manufacturer)) games = file.getElementsByTagName("game") for game in games: cloneof = game.getAttribute("cloneof") isbios = game.getAttribute("isbios") manufacturer = game.getElementsByTagName("manufacturer")[0] description = game.getElementsByTagName("description")[0] if ( manufacturer.firstChild.data == opt_manufacturer and not cloneof and not isbios ): print("%s" % (description.firstChild.data)) # Copia los juegos seleccionados if ( opt_copy == "yes" and os.path.isdir(opt_src_roms) and os.path.isdir(opt_dst_roms) and opt_manufacturer != "" ): print("Copying all {} games".format(opt_manufacturer)) notfound = [] ignored_games = [] games = file.getElementsByTagName("game") for game in games: isignored = False name = game.getAttribute("name") + ".zip" cloneof = game.getAttribute("cloneof") isbios = game.getAttribute("isbios") manufacturer = game.getElementsByTagName("manufacturer")[0] description = game.getElementsByTagName("description")[0] if ( manufacturer.firstChild.data == opt_manufacturer and not cloneof and not isbios ): for element in ignore_list: if description.firstChild.data.find(element) != -1: ignored_games.append(description.firstChild.data) isignored = True if not isignored: src = os.path.join(opt_src_roms, name) if opt_sort == "yes": x = manufacturer.firstChild.data x = x.replace(r"/", r"-") dst = os.path.join(opt_dst_roms, x, name) if not os.path.isdir(os.path.join(opt_dst_roms, x)): os.mkdir(os.path.join(opt_dst_roms, x)) else: dst = os.path.join(opt_dst_roms, name) if os.path.isfile(src): shutil.copyfile(src, dst) print("%s" % (description.firstChild.data)) else: notfound.append(description.firstChild.data) print("\nMissing games:") for game in notfound: print(game) print("\nIgnored games:") for game in ignored_games: print(game) # Copia todos los juegos if ( opt_copy == "yes" and os.path.isdir(opt_src_roms) and os.path.isdir(opt_dst_roms) and opt_manufacturer == "" ): print("Copying all games") notfound = [] games = file.getElementsByTagName("game") for game in games: name = game.getAttribute("name") + ".zip" cloneof = game.getAttribute("cloneof") isbios = game.getAttribute("isbios") manufacturer = game.getElementsByTagName("manufacturer")[0] description = game.getElementsByTagName("description")[0] if not cloneof and not isbios: src = os.path.join(opt_src_roms, name) if opt_sort == "yes": x = manufacturer.firstChild.data x = x.replace(r"/", r"-") dst = os.path.join(opt_dst_roms, x, name) if not os.path.isdir(os.path.join(opt_dst_roms, x)): os.mkdir(os.path.join(opt_dst_roms, x)) else: dst = os.path.join(opt_dst_roms, name) if os.path.isfile(src): shutil.copyfile(src, dst) print("%s" % (description.firstChild.data)) else: notfound.append(description.firstChild.data) print("\nMissing games:") for game in notfound: print(game)