mirror of
https://github.com/notwa/mm
synced 2025-02-05 05:23:22 -08:00
refactor directory handling
This commit is contained in:
parent
8b7048e5cd
commit
6e9a37d8a0
1 changed files with 16 additions and 11 deletions
27
z64dump.py
27
z64dump.py
|
@ -21,6 +21,19 @@ W4 = lambda data: struct.pack('>I', data)
|
||||||
# assume first entry is makerom (0x1060), and second entry begins from makerom
|
# assume first entry is makerom (0x1060), and second entry begins from makerom
|
||||||
fs_sig = b"\x00\x00\x00\x00\x00\x00\x10\x60\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x60"
|
fs_sig = b"\x00\x00\x00\x00\x00\x00\x10\x60\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10\x60"
|
||||||
|
|
||||||
|
class SubDir:
|
||||||
|
def __init__(self, d):
|
||||||
|
self.d = d
|
||||||
|
def __enter__(self):
|
||||||
|
self.cwd = os.getcwd()
|
||||||
|
try:
|
||||||
|
os.mkdir(self.d)
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
os.chdir(self.d)
|
||||||
|
def __exit__(self, type_, value, traceback):
|
||||||
|
os.chdir(self.cwd)
|
||||||
|
|
||||||
def dump_as(b, fn):
|
def dump_as(b, fn):
|
||||||
with open(fn, 'w+b') as f:
|
with open(fn, 'w+b') as f:
|
||||||
f.write(b)
|
f.write(b)
|
||||||
|
@ -104,15 +117,9 @@ def dump_rom(fn):
|
||||||
outdir = hashlib.sha1(data).hexdigest()
|
outdir = hashlib.sha1(data).hexdigest()
|
||||||
del data
|
del data
|
||||||
|
|
||||||
# TODO: a `with` would be suitable here for handling cwd
|
with SubDir(outdir):
|
||||||
try:
|
f.seek(0)
|
||||||
os.mkdir(outdir)
|
z_dump(f)
|
||||||
except FileExistsError:
|
|
||||||
pass
|
|
||||||
os.chdir(outdir)
|
|
||||||
|
|
||||||
f.seek(0)
|
|
||||||
z_dump(f)
|
|
||||||
|
|
||||||
def z_read_file(path, fn=None):
|
def z_read_file(path, fn=None):
|
||||||
if fn == None:
|
if fn == None:
|
||||||
|
@ -197,7 +204,6 @@ def create_rom(d):
|
||||||
f.write(W4(crc2))
|
f.write(W4(crc2))
|
||||||
|
|
||||||
def run(args):
|
def run(args):
|
||||||
cwd = os.getcwd()
|
|
||||||
for path in args:
|
for path in args:
|
||||||
if os.path.isdir(path):
|
if os.path.isdir(path):
|
||||||
create_rom(path)
|
create_rom(path)
|
||||||
|
@ -205,7 +211,6 @@ def run(args):
|
||||||
dump_rom(path)
|
dump_rom(path)
|
||||||
else:
|
else:
|
||||||
lament('no-op:', path)
|
lament('no-op:', path)
|
||||||
os.chdir(cwd)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
ret = 0
|
ret = 0
|
||||||
|
|
Loading…
Add table
Reference in a new issue