1
0
Fork 0
mirror of https://github.com/notwa/mm synced 2024-06-26 12:07:12 -07:00

refactor directory handling

This commit is contained in:
Connor Olding 2015-03-01 14:16:30 -08:00
parent 8b7048e5cd
commit 6e9a37d8a0

View File

@ -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