Clean up scripts from #1861 (nw):

* use spaces for indentation
* better error reporting
* some optimisation
* slightly more intelligent preservation of tabulation
This commit is contained in:
Vas Crabb 2017-01-10 15:12:21 +11:00
parent f1ead008c4
commit 74af888a30
2 changed files with 100 additions and 54 deletions

View File

@ -1,53 +1,82 @@
#!/usr/bin/python
##
## license:BSD-3-Clause
## copyright-holders:Zoe Blade
# Fix discrepancies in arcade ROM dump names, by Zoe Blade
# For Python 2
# For Python 2 and 3
import sys
import xml.etree.ElementTree
print('Loading XML file...')
root = xml.etree.ElementTree.parse('arcade.xml').getroot()
print('Done.')
for childMachine in root.iter('machine'):
if not childMachine.get('cloneof'):
continue
def fixPair(parentMachine, childMachine):
changes = { }
for childRom in childMachine.iter('rom'):
for parentRom in parentMachine.iter('rom'):
if parentRom.get('sha1') == childRom.get('sha1'):
# ROM pair found
if parentRom.get('name') != childRom.get('name'):
# The names don't match
changes[childRom.get('name')] = parentRom.get('name')
for parentMachine in root.iter('machine'):
if not parentMachine.get('name') == childMachine.get('cloneof'):
continue
if changes:
sourceFilename = childMachine.get('sourcefile')
# Machine pair found
try:
input = open(sourceFilename, 'r')
source = input.read()
input.close()
except Exception as e:
sys.stderr.write('%s: error reading %s: %s\n' % (sys.argv[0], sourceFilename, e))
return False
for childRom in childMachine.iter('rom'):
for parentRom in parentMachine.iter('rom'):
if not parentRom.get('sha1') == childRom.get('sha1'):
continue
for oldRomFilename in changes:
newRomFilename = '"%s"' % (changes[oldRomFilename])
oldRomFilename = '"%s"' % (oldRomFilename)
# ROM pair found
paddedLen = max(len(oldRomFilename), len(newRomFilename))
oldRomFilenamePadded = oldRomFilename.ljust(paddedLen, ' ')
newRomFilenamePadded = newRomFilename.ljust(paddedLen, ' ')
if parentRom.get('name') == childRom.get('name'):
break
source = source.replace(oldRomFilenamePadded, newRomFilenamePadded) # Try to preserve fancy spacing where possible
source = source.replace(oldRomFilename, newRomFilename) # Fallback on just replacing the filename
# The names don't match
sys.stdout.write('%s: %s -> %s\n' % (sourceFilename, oldRomFilename, newRomFilename))
sourceFilename = childMachine.get('sourcefile')
output = open(sourceFilename, 'w')
output.write(source)
output.close()
input = open(sourceFilename, 'r')
source = input.read()
input.close()
return True
oldRomFilename = '"' + childRom.get('name') + '"'
newRomFilename = '"' + parentRom.get('name') + '"'
oldRomFilenamePadded = oldRomFilename.ljust(14, ' ')
newRomFilenamePadded = newRomFilename.ljust(14, ' ')
if __name__ == '__main__':
if len(sys.argv) > 2:
sys.stderr.write('Usage:\n%s [arcade.xml]\n' % sys.argv[0])
sys.exit(1)
source = source.replace(oldRomFilenamePadded, newRomFilenamePadded) # Try to preserve fancy spacing where possible
source = source.replace(oldRomFilename, newRomFilename) # Fallback on just replacing the filename
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = 'arcade.xml'
output = open(sourceFilename, 'w')
output.write(source)
output.close()
sys.stderr.write('Loading XML file...')
sys.stderr.flush()
try:
root = xml.etree.ElementTree.parse(filename).getroot()
except Exception as e:
sys.stderr.write('\n%s: error parsing %s: %s\n' % (sys.argv[0], filename, e))
sys.exit(2)
sys.stderr.write('done.\n')
print(sourceFilename + ': ' + oldRomFilename + ' -> ' + newRomFilename)
errors = 0
for childMachine in root.iter('machine'):
if childMachine.get('cloneof'):
for parentMachine in root.iter('machine'):
if parentMachine.get('name') == childMachine.get('cloneof'):
# Machine pair found
if not fixPair(parentMachine, childMachine):
errors += 1
sys.exit(0 if errors == 0 else 3)

View File

@ -1,34 +1,51 @@
#!/usr/bin/python
##
## license:BSD-3-Clause
## copyright-holders:Zoe Blade
# Find discrepancies in arcade ROM dump names, by Zoe Blade
# For Python 2
# For Python 2 and 3
import sys
import xml.etree.ElementTree
print('Loading XML file...')
root = xml.etree.ElementTree.parse('arcade.xml').getroot()
print('Done.')
for childMachine in root.iter('machine'):
if not childMachine.get('cloneof'):
continue
def checkPair(parentMachine, childMachine):
for childRom in childMachine.iter('rom'):
for parentRom in parentMachine.iter('rom'):
if parentRom.get('sha1') == childRom.get('sha1'):
# ROM pair found
if parentRom.get('name') != childRom.get('name'):
# The names don't match
sys.stdout.write('%s %s: %s -> %s\n' % (childMachine.get('sourcefile'), childMachine.get('name'), childRom.get('name'), parentRom.get('name')))
else:
break
for parentMachine in root.iter('machine'):
if not parentMachine.get('name') == childMachine.get('cloneof'):
continue
# Machine pair found
if __name__ == '__main__':
if len(sys.argv) > 2:
sys.stderr.write('Usage:\n%s [arcade.xml]\n' % sys.argv[0])
sys.exit(1)
for childRom in childMachine.iter('rom'):
for parentRom in parentMachine.iter('rom'):
if not parentRom.get('sha1') == childRom.get('sha1'):
continue
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = 'arcade.xml'
# ROM pair found
sys.stderr.write('Loading XML file...')
sys.stderr.flush()
try:
root = xml.etree.ElementTree.parse(filename).getroot()
except Exception as e:
sys.stderr.write('\n%s: error parsing %s: %s\n' % (sys.argv[0], filename, e))
sys.exit(2)
sys.stderr.write('done.\n')
if parentRom.get('name') == childRom.get('name'):
break
for childMachine in root.iter('machine'):
if childMachine.get('cloneof'):
for parentMachine in root.iter('machine'):
if parentMachine.get('name') == childMachine.get('cloneof'):
# Machine pair found
checkPair(parentMachine, childMachine)
# The names don't match
print(childMachine.get('sourcefile') + ' ' + childMachine.get('name') + ': ' + childRom.get('name') + ' -> ' + parentRom.get('name'))
sys.exit(0)