Add Variables.defualted attribute
[scons.git] / test / fixture / mylex.py
blob9c9417eaf51bb7f5a03571cf4d84201c8b3d5515
1 # SPDX-License-Identifier: MIT
3 # Copyright The SCons Foundation
5 """
6 Phony lex for testing SCons.
8 Writes the contents of input file to stdout,
9 after "substituting" $LEXFLAGS and $I_ARGS
11 Needs to understand all the lex/flex options the testcases might use.
12 """
14 import getopt
15 import sys
16 from pathlib import Path
19 def make_side_effect(path, text):
20 p = Path(path)
21 if str(p.parent) != '.':
22 p.parent.mkdir(parents=True, exist_ok=True)
23 with p.open(mode="wb") as f:
24 f.write(text)
27 def fake_lex():
28 make_header = None
29 make_table = None
31 if sys.platform == 'win32':
32 longopts = ['nounistd']
33 else:
34 longopts = []
35 longopts.extend(['header-file=', 'tables-file='])
36 cmd_opts, args = getopt.getopt(sys.argv[1:], 'I:tx', longopts)
37 opt_string = ''
38 i_arguments = ''
40 for opt, arg in cmd_opts:
41 if opt == '-I':
42 i_arguments = f'{i_arguments} {arg}'
43 elif opt == '--header-file':
44 make_header = arg
45 opt_string = f'{opt_string} {opt}={arg}'
46 elif opt == '--tables-file':
47 make_table = arg
48 opt_string = f'{opt_string} {opt}={arg}'
49 else:
50 opt_string = f'{opt_string} {opt}'
52 for arg in args:
53 with open(arg, 'rb') as ifp:
54 contents = ifp.read().decode(encoding='utf-8')
55 contents = contents.replace('LEXFLAGS', opt_string)
56 contents = contents.replace('LEX', 'mylex.py')
57 contents = contents.replace('I_ARGS', i_arguments)
58 sys.stdout.write(contents)
60 # Extra bits:
61 if make_header:
62 make_side_effect(make_header, b"lex header\n")
63 if make_table:
64 make_side_effect(make_table, b"lex table\n")
67 if __name__ == '__main__':
68 fake_lex()
69 sys.exit(0)