Add some new dirty tools

This commit is contained in:
Elliot Nunn 2019-01-11 22:43:36 +08:00
parent 863d53a117
commit 409d05e725
3 changed files with 134 additions and 0 deletions

79
AsmDepsToMakefile.py Executable file
View File

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

20
Percent.py Executable file
View File

@ -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('-')

35
res Executable file
View File

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