243 lines
8.0 KiB
Python
243 lines
8.0 KiB
Python
# 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 argparse
|
|
from xml.dom import minidom
|
|
|
|
|
|
# Elimina los caracteres ilegales de la cadena de texto
|
|
def normalize_path(path):
|
|
illegal_chars = ["<", ">", ":", '"', "/", "\\", "|", "?", "*"]
|
|
replace_with = "_"
|
|
for char in illegal_chars:
|
|
path = path.replace(char, replace_with)
|
|
return path
|
|
|
|
|
|
# Inicialización de las opciones
|
|
ignore_list = ["DECO Cassette", "Bootleg", "prototype", "Quiz", "Mahjong", "Demo"]
|
|
|
|
# Obtiene los argumentos
|
|
parser = argparse.ArgumentParser(
|
|
description="Gestiona las roms de fbneo por desarrolladores a partir de un fichero .dat"
|
|
)
|
|
group = parser.add_mutually_exclusive_group()
|
|
group.add_argument(
|
|
"-l",
|
|
"--list",
|
|
help="Muestra la lista de desarrolladores o de juegos",
|
|
action="store_true",
|
|
)
|
|
group.add_argument("-c", "--copy", help="Copia las roms", action="store_true")
|
|
parser.add_argument(
|
|
"-s",
|
|
"--sort",
|
|
help="Ordena los resultados por carpetas de desarrollador al copiar todas las roms",
|
|
action="store_true",
|
|
)
|
|
parser.add_argument(
|
|
"-cf",
|
|
"--create_folder",
|
|
help="Copia las roms dentro de la carpeta del desarrollador",
|
|
action="store_true",
|
|
)
|
|
|
|
parser.add_argument("-m", "--manufacturer", help="Selecciona un desarrollador")
|
|
parser.add_argument(
|
|
"-d", "--dat", help="Ruta del fichero .dat con información de las roms"
|
|
)
|
|
parser.add_argument(
|
|
"-i", "--input", help="Ruta donde se encuentran las roms", default=""
|
|
)
|
|
parser.add_argument("-o", "--output", help="Ruta donde depositar las roms", default="")
|
|
args = parser.parse_args()
|
|
|
|
# Importa el xml
|
|
if args.dat == None:
|
|
print("No se ha especificado un fichero .dat")
|
|
sys.exit(2)
|
|
|
|
if not os.path.isfile(args.dat):
|
|
print("No se encuentra el fichero .dat")
|
|
sys.exit(2)
|
|
|
|
print("Parsing {} file".format(args.dat))
|
|
file = minidom.parse(args.dat)
|
|
|
|
# Lista los desarrolladores
|
|
if args.list and args.manufacturer == None:
|
|
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 args.list and args.manufacturer != None:
|
|
games = file.getElementsByTagName("game")
|
|
print("List of all", args.manufacturer, "games:", len(games))
|
|
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 == args.manufacturer
|
|
and not cloneof
|
|
and not isbios
|
|
):
|
|
print(description.firstChild.data)
|
|
|
|
# Copia los juegos del desarrollador seleccionado
|
|
if (
|
|
args.copy
|
|
and os.path.isdir(args.input)
|
|
and os.path.isdir(args.output)
|
|
and args.manufacturer != None
|
|
):
|
|
# Comprueba si ha de crear la carpeta con el nombre del desarrollador
|
|
if args.create_folder:
|
|
normalized_manufacturer = normalize_path(args.manufacturer)
|
|
os.mkdir(os.path.join(args.output, args.manufacturer))
|
|
|
|
notfound = []
|
|
ignored_games = []
|
|
copied_games = []
|
|
games = file.getElementsByTagName("game")
|
|
print("Copying all", args.manufacturer, "games:", len(games))
|
|
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]
|
|
driver = game.getElementsByTagName("driver")
|
|
status = (
|
|
game.getElementsByTagName("driver")[0].getAttribute("status")
|
|
if driver
|
|
else ""
|
|
)
|
|
if (
|
|
manufacturer.firstChild.data == args.manufacturer
|
|
and not cloneof
|
|
and not isbios
|
|
and status == "good"
|
|
):
|
|
for ignore_element in ignore_list:
|
|
if ignore_element.casefold() in description.firstChild.data.casefold():
|
|
ignored_games.append(description.firstChild.data)
|
|
isignored = True
|
|
|
|
if not isignored:
|
|
src = os.path.join(args.input, name)
|
|
if args.sort:
|
|
x = manufacturer.firstChild.data
|
|
x = x.replace(r"/", r"-")
|
|
dst = os.path.join(args.output, x, name)
|
|
if not os.path.isdir(os.path.join(args.output, x)):
|
|
os.mkdir(os.path.join(args.output, x))
|
|
else:
|
|
if args.create_folder:
|
|
dst = os.path.join(args.output, normalized_manufacturer, name)
|
|
else:
|
|
dst = os.path.join(args.output, name)
|
|
|
|
if os.path.isfile(src):
|
|
shutil.copyfile(src, dst)
|
|
copied_games.append(description.firstChild.data)
|
|
print(description.firstChild.data)
|
|
else:
|
|
notfound.append(description.firstChild.data)
|
|
|
|
print("\nMissing games:", len(notfound))
|
|
for game in notfound:
|
|
print(game)
|
|
|
|
print("\nIgnored games:", len(ignored_games))
|
|
for game in ignored_games:
|
|
print(game)
|
|
|
|
print("\nCopied games: ", len(copied_games))
|
|
print("Missing games:", len(notfound))
|
|
print("Ignored games:", len(ignored_games))
|
|
|
|
# Copia todos los juegos
|
|
if (
|
|
args.copy
|
|
and os.path.isdir(args.input)
|
|
and os.path.isdir(args.output)
|
|
and args.manufacturer == None
|
|
):
|
|
notfound = []
|
|
ignored_games = []
|
|
copied_games = []
|
|
games = file.getElementsByTagName("game")
|
|
print("Copying all games:", len(games))
|
|
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]
|
|
driver = game.getElementsByTagName("driver")
|
|
status = (
|
|
game.getElementsByTagName("driver")[0].getAttribute("status")
|
|
if driver
|
|
else ""
|
|
)
|
|
|
|
if not cloneof and not isbios and status == "good":
|
|
for ignore_element in ignore_list:
|
|
if ignore_element.casefold() in description.firstChild.data.casefold():
|
|
ignored_games.append(description.firstChild.data)
|
|
isignored = True
|
|
|
|
if not isignored:
|
|
src = os.path.join(args.input, name)
|
|
if args.sort:
|
|
x = manufacturer.firstChild.data
|
|
x = x.replace(r"/", r"-")
|
|
dst = os.path.join(args.output, x, name)
|
|
if not os.path.isdir(os.path.join(args.output, x)):
|
|
os.mkdir(os.path.join(args.output, x))
|
|
else:
|
|
dst = os.path.join(args.output, name)
|
|
|
|
if os.path.isfile(src):
|
|
shutil.copyfile(src, dst)
|
|
copied_games.append(description.firstChild.data)
|
|
print(description.firstChild.data)
|
|
else:
|
|
notfound.append(description.firstChild.data)
|
|
|
|
print("\nMissing games:", len(notfound))
|
|
for game in notfound:
|
|
print(game)
|
|
|
|
print("\nIgnored games:", len(ignored_games))
|
|
for game in ignored_games:
|
|
print(game)
|
|
|
|
print("\nCopied games: ", len(copied_games))
|
|
print("Missing games:", len(notfound))
|
|
print("Ignored games:", len(ignored_games))
|