tbxi-patches/patch_common.py

71 lines
2.0 KiB
Python
Raw Normal View History

import os
2019-07-28 08:16:05 +00:00
from os import path
from subprocess import run, DEVNULL
import argparse
import shutil
import sys
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)
2019-07-28 08:16:05 +00:00
def get_src(desc=None):
parser = argparse.ArgumentParser(description=desc)
2019-09-01 02:07:49 +00:00
parser.add_argument('src', action='store', metavar='SRC', help='ROM or dump directory')
parser.add_argument('-o', action='store', metavar='DEST', help='optional destination path -- "file" or "directory/"'.replace('/', path.sep))
2019-07-28 08:16:05 +00:00
args = parser.parse_args()
if args.o is None: args.o = args.src
2019-07-28 08:16:05 +00:00
if not path.exists(args.src):
print('File not found', file=sys.stderr); sys.exit(1)
if path.realpath(args.src) == path.realpath(args.o) and path.isdir(args.src):
# Dest and source are the same, just edit in place
2019-07-28 08:16:05 +00:00
return args.o, donothing
2019-07-28 08:16:05 +00:00
elif args.o.endswith(path.sep):
# Dest is a folder that we can patch then exit
2019-07-28 08:16:05 +00:00
copy_or_dump(args.src, args.o)
return args.o, donothing
2019-07-28 08:16:05 +00:00
else:
# Dest must be built from a patched temp directory
2019-07-28 08:16:05 +00:00
# Follow up by building and deleting the tempfile
tmp = tempfile.mkdtemp()
subtmp = path.join(tmp, 'editrom')
2019-07-28 08:16:05 +00:00
def cleanup():
build(subtmp, args.o)
2019-07-28 08:16:05 +00:00
shutil.rmtree(tmp)
copy_or_dump(args.src, subtmp)
return subtmp, cleanup