mirror of
https://github.com/Sei-Lisa/LSL-PyOptimizer
synced 2025-07-01 15:48:21 +00:00
Fix --postarg and add option --preproc-show-cmdline + unit test
Thanks to @Blues-Today for reporting that --postarg was not working. This needed a regression test, and the best way to distinguish --prearg from --postarg is to display the full preprocessor command line, therefore a new option was added for that purpose: --preproc-show-cmdline Fixes #18.
This commit is contained in:
parent
8d413f910e
commit
7eaa4cb0ad
3 changed files with 73 additions and 53 deletions
117
main.py
117
main.py
|
@ -233,6 +233,7 @@ Usage: {progname}
|
||||||
[--precmd=<cmd>] preprocessor command ('cpp' by default)
|
[--precmd=<cmd>] preprocessor command ('cpp' by default)
|
||||||
[--prenodef] no LSL specific defines (__AGENTKEY__ etc.)
|
[--prenodef] no LSL specific defines (__AGENTKEY__ etc.)
|
||||||
[--preshow] show preprocessor output, and stop
|
[--preshow] show preprocessor output, and stop
|
||||||
|
[--preproc-show-cmdline] show preprocessor invocation line, and stop
|
||||||
[--avid=<UUID>] * specify UUID of avatar saving the script
|
[--avid=<UUID>] * specify UUID of avatar saving the script
|
||||||
[--avname=<name>] * specify name of avatar saving the script
|
[--avname=<name>] * specify name of avatar saving the script
|
||||||
[--assetid=<UUID>] * specify the asset UUID of the script
|
[--assetid=<UUID>] * specify the asset UUID of the script
|
||||||
|
@ -411,7 +412,7 @@ def main(argv):
|
||||||
'timestamp', 'python-exceptions', 'prettify', 'bom', 'emap',
|
'timestamp', 'python-exceptions', 'prettify', 'bom', 'emap',
|
||||||
'preproc=', 'precmd=', 'prearg=', 'prenodef', 'preshow',
|
'preproc=', 'precmd=', 'prearg=', 'prenodef', 'preshow',
|
||||||
'avid=', 'avname=', 'assetid=', 'shortname=', 'builtins='
|
'avid=', 'avname=', 'assetid=', 'shortname=', 'builtins='
|
||||||
'libdata=', 'postarg='))
|
'libdata=', 'postarg=', 'preproc-show-cmdline'))
|
||||||
except getopt.GetoptError as e:
|
except getopt.GetoptError as e:
|
||||||
Usage(argv[0])
|
Usage(argv[0])
|
||||||
werr(u"\nError: %s\n" % str2u(str(e), 'utf8'))
|
werr(u"\nError: %s\n" % str2u(str(e), 'utf8'))
|
||||||
|
@ -430,6 +431,7 @@ def main(argv):
|
||||||
script_header = ''
|
script_header = ''
|
||||||
script_timestamp = ''
|
script_timestamp = ''
|
||||||
preshow = False
|
preshow = False
|
||||||
|
preproc_show_cmdline = False
|
||||||
raise_exception = False
|
raise_exception = False
|
||||||
prettify = False
|
prettify = False
|
||||||
bom = False
|
bom = False
|
||||||
|
@ -505,12 +507,18 @@ def main(argv):
|
||||||
elif opt in ('-P', '--prearg'):
|
elif opt in ('-P', '--prearg'):
|
||||||
preproc_user_preargs.append(arg)
|
preproc_user_preargs.append(arg)
|
||||||
|
|
||||||
|
elif opt in ('-A', '--postarg'):
|
||||||
|
preproc_user_postargs.append(arg)
|
||||||
|
|
||||||
elif opt == '--prenodef':
|
elif opt == '--prenodef':
|
||||||
predefines = False
|
predefines = False
|
||||||
|
|
||||||
elif opt == '--preshow':
|
elif opt == '--preshow':
|
||||||
preshow = True
|
preshow = True
|
||||||
|
|
||||||
|
elif opt == '--preproc-show-cmdline':
|
||||||
|
preproc_show_cmdline = True
|
||||||
|
|
||||||
elif opt in ('-H', '--header'):
|
elif opt in ('-H', '--header'):
|
||||||
script_header = True
|
script_header = True
|
||||||
|
|
||||||
|
@ -576,58 +584,59 @@ def main(argv):
|
||||||
|
|
||||||
del args
|
del args
|
||||||
|
|
||||||
script = ''
|
if not preproc_show_cmdline:
|
||||||
if fname == '-':
|
script = ''
|
||||||
script = sys.stdin.read()
|
if fname == '-':
|
||||||
else:
|
script = sys.stdin.read()
|
||||||
try:
|
else:
|
||||||
f = open(fname, 'r')
|
try:
|
||||||
except IOError as e:
|
f = open(fname, 'r')
|
||||||
if e.errno == 2:
|
except IOError as e:
|
||||||
werr(u"Error: File not found: %s\n" % str2u(fname))
|
if e.errno == 2:
|
||||||
return 2
|
werr(u"Error: File not found: %s\n" % str2u(fname))
|
||||||
raise
|
return 2
|
||||||
try:
|
raise
|
||||||
script = f.read()
|
try:
|
||||||
finally:
|
script = f.read()
|
||||||
f.close()
|
finally:
|
||||||
del f
|
f.close()
|
||||||
|
del f
|
||||||
|
|
||||||
# Transform to str and check Unicode validity
|
# Transform to str and check Unicode validity
|
||||||
if type(script) is unicode:
|
if type(script) is unicode:
|
||||||
script = u2str(script, 'utf8')
|
script = u2str(script, 'utf8')
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
# Try converting the script to Unicode, to report any encoding
|
# Try converting the script to Unicode, to report any encoding
|
||||||
# errors with accurate line information.
|
# errors with accurate line information.
|
||||||
tmp = UniConvScript(script, options,
|
tmp = UniConvScript(script, options,
|
||||||
fname if fname != '-' else '<stdin>',
|
fname if fname != '-' else '<stdin>',
|
||||||
emap).to_unicode()
|
emap).to_unicode()
|
||||||
# For Python 2, just report any errors and ignore the result.
|
# For Python 2, just report any errors and ignore the result.
|
||||||
# For Python 3, use the Unicode.
|
# For Python 3, use the Unicode.
|
||||||
if python3:
|
if python3:
|
||||||
script = tmp
|
script = tmp
|
||||||
|
del tmp
|
||||||
|
except EParse as e:
|
||||||
|
# We don't call ReportError to prevent problems due to
|
||||||
|
# displaying invalid UTF-8
|
||||||
|
werr(e.args[0] + u"\n")
|
||||||
|
return 1
|
||||||
|
# Now script is in native str format.
|
||||||
|
|
||||||
|
if script_header:
|
||||||
|
script_header = ScriptHeader(script, avname)
|
||||||
|
|
||||||
|
if script_timestamp:
|
||||||
|
import time
|
||||||
|
tmp = time.time()
|
||||||
|
script_timestamp = time.strftime(
|
||||||
|
'// Generated on %Y-%m-%dT%H:%M:%S.{0:06d}Z\n'
|
||||||
|
.format(int(tmp % 1 * 1000000)), time.gmtime(tmp))
|
||||||
del tmp
|
del tmp
|
||||||
except EParse as e:
|
|
||||||
# We don't call ReportError to prevent problems due to
|
|
||||||
# displaying invalid UTF-8
|
|
||||||
werr(e.args[0] + u"\n")
|
|
||||||
return 1
|
|
||||||
# Now script is in native str format.
|
|
||||||
|
|
||||||
if script_header:
|
if shortname == '':
|
||||||
script_header = ScriptHeader(script, avname)
|
shortname = os.path.basename(fname)
|
||||||
|
|
||||||
if script_timestamp:
|
|
||||||
import time
|
|
||||||
tmp = time.time()
|
|
||||||
script_timestamp = time.strftime(
|
|
||||||
'// Generated on %Y-%m-%dT%H:%M:%S.{0:06d}Z\n'
|
|
||||||
.format(int(tmp % 1 * 1000000)), time.gmtime(tmp))
|
|
||||||
del tmp
|
|
||||||
|
|
||||||
if shortname == '':
|
|
||||||
shortname = os.path.basename(fname)
|
|
||||||
|
|
||||||
# Build preprocessor command line
|
# Build preprocessor command line
|
||||||
preproc_cmdline = [preproc_command] + preproc_user_preargs
|
preproc_cmdline = [preproc_command] + preproc_user_preargs
|
||||||
|
@ -660,13 +669,15 @@ def main(argv):
|
||||||
preproc_cmdline.append('-D__AGENTNAME__="%s"' % avname)
|
preproc_cmdline.append('-D__AGENTNAME__="%s"' % avname)
|
||||||
preproc_cmdline.append('-D__ASSETID__=' + assetid)
|
preproc_cmdline.append('-D__ASSETID__=' + assetid)
|
||||||
preproc_cmdline.append('-D__SHORTFILE__="%s"' % shortname)
|
preproc_cmdline.append('-D__SHORTFILE__="%s"' % shortname)
|
||||||
preproc_cmdline.append('-D__OPTIMIZER__=LSL PyOptimizer')
|
preproc_cmdline.append('-D__OPTIMIZER__=LSL-PyOptimizer')
|
||||||
preproc_cmdline.append('-D__OPTIMIZER_VERSION__=' + VERSION)
|
preproc_cmdline.append('-D__OPTIMIZER_VERSION__=' + VERSION)
|
||||||
|
|
||||||
# Append user arguments at the end to allow them to override defaults
|
# Append user arguments at the end to allow them to override defaults
|
||||||
preproc_cmdline += preproc_user_postargs
|
preproc_cmdline += preproc_user_postargs
|
||||||
|
|
||||||
if preproc != 'none':
|
if preproc_show_cmdline:
|
||||||
|
script = ' '.join(preproc_cmdline)
|
||||||
|
elif preproc != 'none':
|
||||||
# PreparePreproc uses and returns Unicode string encoding.
|
# PreparePreproc uses and returns Unicode string encoding.
|
||||||
script = u2b(PreparePreproc(any2u(script, 'utf8')), 'utf8')
|
script = u2b(PreparePreproc(any2u(script, 'utf8')), 'utf8')
|
||||||
# At this point, for the external preprocessor to work we need the
|
# At this point, for the external preprocessor to work we need the
|
||||||
|
@ -706,7 +717,7 @@ def main(argv):
|
||||||
elif x == 'USE_LAZY_LISTS':
|
elif x == 'USE_LAZY_LISTS':
|
||||||
options.add('lazylists')
|
options.add('lazylists')
|
||||||
|
|
||||||
if not preshow:
|
if not preshow and not preproc_show_cmdline:
|
||||||
|
|
||||||
if emap:
|
if emap:
|
||||||
options.add('emap')
|
options.add('emap')
|
||||||
|
|
1
unit_tests/preproc.suite/option-passing.out
Normal file
1
unit_tests/preproc.suite/option-passing.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
cpp --prefix-test-1 --prefix-test-2 -undef -x c -std=c99 -nostdinc -trigraphs -dN -fno-extended-identifiers -Dinteger(...)=((integer)(__VA_ARGS__)) -Dfloat(...)=((float)(__VA_ARGS__)) -Dstring(...)=((string)(__VA_ARGS__)) -Dkey(...)=((key)(__VA_ARGS__)) -Drotation(...)=((rotation)(__VA_ARGS__)) -Dquaternion(...)=((quaternion)(__VA_ARGS__)) -Dvector(...)=((vector)(__VA_ARGS__)) -Dlist(...)=((list)(__VA_ARGS__)) -D__AGENTKEY__="00000000-0000-0000-0000-000000000000" -D__AGENTID__="00000000-0000-0000-0000-000000000000" -D__AGENTIDRAW__=00000000-0000-0000-0000-000000000000 -D__AGENTNAME__="" -D__ASSETID__=00000000-0000-0000-0000-000000000000 -D__SHORTFILE__="" -D__OPTIMIZER__=LSL-PyOptimizer -D__OPTIMIZER_VERSION__=0.3.0beta --suffix-test-1 --suffix-test-2
|
8
unit_tests/preproc.suite/option-passing.run
Normal file
8
unit_tests/preproc.suite/option-passing.run
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
main.py -y -O clear,processpre \
|
||||||
|
-p gcpp \
|
||||||
|
--preproc-show-cmdline \
|
||||||
|
-P --prefix-test-1 \
|
||||||
|
-A --suffix-test-1 \
|
||||||
|
--prearg=--prefix-test-2 \
|
||||||
|
--postarg=--suffix-test-2 \
|
||||||
|
-
|
Loading…
Add table
Add a link
Reference in a new issue