diff --git a/AVsitter2/Makefile b/AVsitter2/Makefile index 2234ba8..fae0ddf 100644 --- a/AVsitter2/Makefile +++ b/AVsitter2/Makefile @@ -15,9 +15,6 @@ PREPROC_KIND=mcpp # If the preprocessor is mcpp and it is in your path, you can leave it as is. PREPROC_PATH=mcpp -# Command to remove files in your system. Use 'del' for Windows. -RM=rm -f - # Name of the zipped file to generate for SL SLZIP=AVsitter2.zip @@ -67,24 +64,24 @@ OPENSIM=[AV]sitA.oss\ all: $(SLZIP) $(OSZIP) clean: - $(RM) $(SLZIP) $(OSZIP) $(OPTIMIZED) $(OPENSIM) + $(PYTHON) build-aux.py rm $(SLZIP) $(OSZIP) $(OPTIMIZED) $(OPENSIM) optimized: $(OPTIMIZED) opensim: $(OPENSIM) $(SLZIP): $(OPTIMIZED) $(UNOPTIMIZED) - $(RM) $@ + $(PYTHON) build-aux.py rm $@ zip $@ $(OPTIMIZED) $(UNOPTIMIZED) %.lslo: %.lsl $(PYTHON) $(OPTIMIZER) -H -O addstrings,shrinknames,-extendedglobalexpr -p $(PREPROC_KIND) --precmd=$(PREPROC_PATH) $< -o $@ $(OSZIP): $(OPENSIM) - $(RM) $@ + $(PYTHON) build-aux.py rm $@ zip $@ $(OPENSIM) %.oss: %.lsl - $(PYTHON) prepare-for-oss.py $< > $@ + $(PYTHON) build-aux.py oss-process $< > $@ .PHONY : all clean optimized diff --git a/AVsitter2/build-aux.py b/AVsitter2/build-aux.py index 1fa509b..3db0314 100644 --- a/AVsitter2/build-aux.py +++ b/AVsitter2/build-aux.py @@ -1,35 +1,87 @@ #!/usr/bin/env python # coding: utf8 -import sys, re +import sys def prterr(s): sys.stderr.write(s + "\n") -def main(argc, argv): - if argc < 2: - prterr(u'Need exactly 1 argument (input filename)') - return 1 +def usage(): + """Show usage help.""" + prterr(u"""Usage: +python build-aux.py [] + +Where command can be: + + oss-process : + Processes the given file for OpenSim and outputs the result to + standard output. If is not given, read from standard input. + rm [...] + Deletes the given list of files. +""") + +def rm(filelist): + """Delete the given list of files, ignoring 'file not found' errors.""" + import os + for i in filelist: + try: + os.unlink(i) + except OSError as e: + if e.errno != 2: + raise + return 0 + +def oss_process(filename): + """Process a file for OpenSim Scripting.""" + import re # Regex that replaces a line with its OSS version when one's specified. os_re = re.compile(r'^( *)(.*?)// ?OSS::(.*)$', re.MULTILINE) - f = open(argv[1], "r"); - s = f.read() - f.close() + if filename is not None: + f = open(filename, "r"); + else: + f = sys.stdin + try: + s = f.read() + finally: + if filename is not None: + f.close() + # The U+FFFD character that AVsitter uses causes problems in OpenSim. # Replace it with U+001F (Unit Separator) which works fine. s = s.replace(b'\xEF\xBF\xBD', b'\x1F') # UUIDs in OpenSim - s = s.replace('f2e0ed5e-6592-4199-901d-a659c324ca94', '206fcbe2-47b3-41e8-98e6-8909595b8605') - s = s.replace('b30c9262-9abf-4cd1-9476-adcf5723c029', 'b88526b7-3966-43fd-ae76-1e39881c86aa') + s = s.replace('f2e0ed5e-6592-4199-901d-a659c324ca94', + '206fcbe2-47b3-41e8-98e6-8909595b8605') + s = s.replace('b30c9262-9abf-4cd1-9476-adcf5723c029', + 'b88526b7-3966-43fd-ae76-1e39881c86aa') # TODO: Replace LockGuard texture UUIDs s = os_re.sub(r'\1\3', s) sys.stdout.write(s) return 0 +def main(argc, argv): + if argc < 2: + usage() + return 0 + + cmd = argv[1] + if cmd == 'rm': + return rm(argv[2:]) + + if cmd == 'oss-process': + if argc > 3: + usage() + return 1 + filename = argv[2] if argc == 3 else None + return oss_process(filename) + + usage() + return 1 + ret = main(len(sys.argv), sys.argv) if ret is not None and ret > 0: sys.exit(ret)