From 409d05e7253d6e1cf9d087771dd314887103bd9c Mon Sep 17 00:00:00 2001 From: Elliot Nunn Date: Fri, 11 Jan 2019 22:43:36 +0800 Subject: [PATCH] Add some new dirty tools --- AsmDepsToMakefile.py | 79 ++++++++++++++++++++++++++++++++++++++++++++ Percent.py | 20 +++++++++++ res | 35 ++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100755 AsmDepsToMakefile.py create mode 100755 Percent.py create mode 100755 res diff --git a/AsmDepsToMakefile.py b/AsmDepsToMakefile.py new file mode 100755 index 0000000..8d6cde3 --- /dev/null +++ b/AsmDepsToMakefile.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +import os +from os import path + +LWID = 44 +LWID2 = 84 + +def width(string): + n = 0 + for c in string: + if c == '\t': + n += 1 + while n % 4: n += 1 + else: n += 1 + return n + +def quote(string): + return '"' + string + '"' + +import argparse + +args = argparse.ArgumentParser(description=''' + For an MPW Assembler file, print an MPW Make line that declares its + dependencies. Defines can be specified so that MPW path variables + are produced. +''') +args.add_argument('defines', metavar='VARIABLE=PATH', action='store', nargs='*', help='Variable definition') +args.add_argument('source', metavar='ASM', action='store', help='Assembly file') +args = args.parse_args() +args.defines = [tuple(equ.split('=')) for equ in args.defines] + + +f = open(args.source, 'rb').read() +f = bytes(c for c in f if c < 128) +f = f.decode('ascii').replace('\r', '\n').split('\n') + +incfiles = [] +for line in f: + try: + starter = line.split()[0].lower() + if starter in ['load', 'include']: + incfile = line.split()[1].strip("'") + incfiles.append(incfile) + except IndexError: + pass + +incfiles.append(path.basename(args.source)) + +for i in range(len(incfiles)): + orig = incfiles[i] + doneflag = False + for k, v in args.defines: + if path.exists(path.join(v, orig)): + incfiles[i] = '{%s}%s' % (k, orig) + doneflag = True + if doneflag: continue + +isfirst = True +for l in incfiles: + if isfirst: + line = quote('{ObjDir}' + path.basename(args.source) + '.o') + '\t' + while width(line) < LWID-4: line += '\t' + line += '\u0192\t' + else: + line = '' + isfirst = False + + while width(line) < LWID: line += '\t' + + line += quote(l) + + if l != incfiles[-1]: + while width(line) < LWID2: line += '\t' + line += '\u2202' + + print(line) + +print('\tAsm {StdAOpts} -o {Targ} ' + quote(incfiles[-1])) diff --git a/Percent.py b/Percent.py new file mode 100755 index 0000000..306bff5 --- /dev/null +++ b/Percent.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +import sys + +files = sys.argv[1:] + +for i in range(len(files)): + files[i] = open(files[i], 'rb').read() + +b = files[0] +for f in files[1:]: + m = 0 + maxlen = max(len(b), len(f)) + minlen = min(len(b), len(f)) + for i in range(minlen): + if b[i] == f[i]: m += 1 + try: + print(100 * m // maxlen) + except ZeroDivisionError: + print('-') diff --git a/res b/res new file mode 100755 index 0000000..eac8139 --- /dev/null +++ b/res @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +# this is a monstrous program! + +import macresources +import shlex +import tempfile + +returns = [] + +import sys + +for f in sys.argv[1:]: + try: + a, _, b = f.partition('//') + + rtype, _, rid = b.partition('/') + rtype = rtype.encode('mac_roman') + rid = int(rid) + + rsrc = open(a, 'rb').read() + rsrc = macresources.parse_rez_code(rsrc) + rsrc = next(r for r in rsrc if r.type == rtype and r.id == rid) + + name = '-%s-%s-%d' % (a.split('/')[-1], rtype.decode('mac_roman'), rid) + handle, name = tempfile.mkstemp(dir='/tmp', suffix=name) + + open(name, 'wb').write(rsrc.data) + returns.append(name) + + except: + returns.append(f) + +if returns: + print(' '.join(shlex.quote(x) for x in returns))