Allow patching to/from and combo of dirs/files

This commit is contained in:
Elliot Nunn 2019-09-01 08:45:56 +08:00
parent 1626faad7e
commit c3d16d959f

View File

@ -1,3 +1,4 @@
import os
from os import path from os import path
from subprocess import run, DEVNULL from subprocess import run, DEVNULL
import argparse import argparse
@ -5,42 +6,65 @@ import shutil
import sys import sys
import tempfile import tempfile
def clobber(p):
p = p.rstrip(path.sep)
for x in [p, p+'.idump', p+'.rdump']:
try:
if path.isdir(x): shutil.rmtree(p)
os.remove(x)
except FileNotFoundError:
pass
def donothing():
pass
def dump(src, dest):
clobber(dest)
run(['python3', '-m', 'tbxi', 'dump', '-o', path.abspath(dest), path.abspath(src)], check=True, stdout=DEVNULL)
def build(src, dest):
clobber(dest)
run(['python3', '-m', 'tbxi', 'build', '-o', path.abspath(dest), path.abspath(src)], check=True, stdout=DEVNULL)
def copy_or_dump(src, dest):
clobber(dest)
if path.isdir(src):
shutil.copytree(src, dest)
else:
dump(src, dest)
def get_src(desc=None): def get_src(desc=None):
parser = argparse.ArgumentParser(description=desc) parser = argparse.ArgumentParser(description=desc)
parser.add_argument('src', action='store', help='Original (ROM or dumped dir)') parser.add_argument('src', action='store', help='ROM or dump directory')
parser.add_argument('-o', action='store', help='New') parser.add_argument('-o', action='store', help='Optional destination path -- "file" or "directory/"'.replace('/', path.sep))
args = parser.parse_args() args = parser.parse_args()
if args.o is None: args.o = args.src
if not path.exists(args.src): if not path.exists(args.src):
print('File not found', file=sys.stderr); sys.exit(1) print('File not found', file=sys.stderr); sys.exit(1)
if not path.isdir(args.src) and args.o is None: if path.realpath(args.src) == path.realpath(args.o) and path.isdir(args.src):
print('Cannot edit a ROM in place, use -o', file=sys.stderr); sys.exit(1) # Dest and source are the same, just edit in place
if path.isdir(args.src): return args.o, donothing
def cleanup():
pass
if args.o: elif args.o.endswith(path.sep):
try: # Dest is a folder that we can patch then exit
shutil.rmtree(args.o)
except FileNotFoundError: copy_or_dump(args.src, args.o)
pass return args.o, donothing
shutil.copytree(args.src, args.o)
src = args.o
else:
src = args.src
else: else:
# Dest must be built from a patched temp directory
# Follow up by building and deleting the tempfile
tmp = tempfile.mkdtemp() tmp = tempfile.mkdtemp()
src = path.join(tmp, 'editrom') subtmp = path.join(tmp, 'editrom')
run(['python3', '-m', 'tbxi', 'dump', '-o', src, args.src], check=True, stdout=DEVNULL)
def cleanup(): def cleanup():
run(['python3', '-m', 'tbxi', 'build', '-o', args.o, src], check=True, stdout=DEVNULL) build(subtmp, args.o)
shutil.rmtree(tmp) shutil.rmtree(tmp)
return src, cleanup copy_or_dump(args.src, subtmp)
return subtmp, cleanup