OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright (C) 2013 Google Inc. All rights reserved. | 2 # Copyright (C) 2013 Google Inc. All rights reserved. |
3 # | 3 # |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
(...skipping 25 matching lines...) Expand all Loading... |
36 [2] Python: idl_compiler.py, which calls: | 36 [2] Python: idl_compiler.py, which calls: |
37 IDL lexer => IDL parser => Python object builder => | 37 IDL lexer => IDL parser => Python object builder => |
38 interface dependency resolver => IDL semantic validator => | 38 interface dependency resolver => IDL semantic validator => |
39 C++ code generator | 39 C++ code generator |
40 | 40 |
41 We will move IDL files from the Perl build flow [1] to the Python build flow [2] | 41 We will move IDL files from the Perl build flow [1] to the Python build flow [2] |
42 incrementally. See http://crbug.com/239771 | 42 incrementally. See http://crbug.com/239771 |
43 """ | 43 """ |
44 import optparse | 44 import optparse |
45 import os | 45 import os |
| 46 import pickle |
46 import shlex | 47 import shlex |
47 import sys | 48 import sys |
48 | 49 |
| 50 import code_generator_v8 |
49 import idl_reader | 51 import idl_reader |
50 import code_generator_v8 | |
51 | 52 |
52 | 53 |
53 def parse_options(): | 54 def parse_options(): |
54 parser = optparse.OptionParser() | 55 parser = optparse.OptionParser() |
55 parser.add_option('--additional-idl-files') | 56 parser.add_option('--additional-idl-files') |
| 57 # FIXME: The --dump-json-and-pickle option is only for debugging and will |
| 58 # be removed once we complete migrating all IDL files from the Perl flow to |
| 59 # the Python flow. |
| 60 parser.add_option('--dump-json-and-pickle', action='store_true', default=Fal
se) |
56 parser.add_option('--idl-attributes-file') | 61 parser.add_option('--idl-attributes-file') |
57 parser.add_option('--include', dest='idl_directories', action='append') | 62 parser.add_option('--include', dest='idl_directories', action='append') |
58 parser.add_option('--output-directory') | 63 parser.add_option('--output-directory') |
59 parser.add_option('--interface-dependencies-file') | 64 parser.add_option('--interface-dependencies-file') |
60 parser.add_option('--verbose', action='store_true', default=False) | 65 parser.add_option('--verbose', action='store_true', default=False) |
61 parser.add_option('--write-file-only-if-changed', type='int') | 66 parser.add_option('--write-file-only-if-changed', type='int') |
62 # ensure output comes last, so command line easy to parse via regexes | 67 # ensure output comes last, so command line easy to parse via regexes |
63 parser.disable_interspersed_args() | 68 parser.disable_interspersed_args() |
64 | 69 |
65 options, args = parser.parse_args() | 70 options, args = parser.parse_args() |
66 if options.output_directory is None: | 71 if options.output_directory is None: |
67 parser.error('Must specify output directory using --output-directory.') | 72 parser.error('Must specify output directory using --output-directory.') |
68 if options.additional_idl_files is not None: | 73 if options.additional_idl_files is not None: |
69 # additional_idl_files is passed as a string with varied (shell-style) | 74 # additional_idl_files is passed as a string with varied (shell-style) |
70 # quoting, hence needs parsing. | 75 # quoting, hence needs parsing. |
71 options.additional_idl_files = shlex.split(options.additional_idl_files) | 76 options.additional_idl_files = shlex.split(options.additional_idl_files) |
72 if len(args) != 1: | 77 if len(args) != 1: |
73 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) | 78 parser.error('Must specify exactly 1 input file as argument, but %d give
n.' % len(args)) |
74 options.idl_filename = os.path.realpath(args[0]) | 79 options.idl_filename = os.path.realpath(args[0]) |
75 return options | 80 return options |
76 | 81 |
77 | 82 |
| 83 def write_json_and_pickle(definitions, interface_name, output_directory): |
| 84 json_string = definitions.to_json() |
| 85 json_basename = interface_name + '.json' |
| 86 json_filename = os.path.join(output_directory, json_basename) |
| 87 with open(json_filename, 'w') as json_file: |
| 88 json_file.write(json_string) |
| 89 pickle_basename = interface_name + '.pkl' |
| 90 pickle_filename = os.path.join(output_directory, pickle_basename) |
| 91 with open(pickle_filename, 'wb') as pickle_file: |
| 92 pickle.dump(definitions, pickle_file) |
| 93 |
| 94 |
78 def main(): | 95 def main(): |
79 options = parse_options() | 96 options = parse_options() |
80 idl_filename = options.idl_filename | 97 idl_filename = options.idl_filename |
81 basename = os.path.basename(idl_filename) | 98 basename = os.path.basename(idl_filename) |
82 interface_name, _ = os.path.splitext(basename) | 99 interface_name, _ = os.path.splitext(basename) |
83 verbose = options.verbose | 100 verbose = options.verbose |
84 if verbose: | 101 if verbose: |
85 print idl_filename | 102 print idl_filename |
86 | 103 |
87 idl_definitions = idl_reader.read_idl_definitions(idl_filename, options.inte
rface_dependencies_file, options.additional_idl_files) | 104 definitions = idl_reader.read_idl_definitions(idl_filename, options.interfac
e_dependencies_file, options.additional_idl_files, options.idl_attributes_file,
verbose=options.verbose) |
88 # FIXME: add parameters when add validator | 105 if not definitions: |
89 # idl_definitions = idl_reader.read_idl_interface(idl_filename, options.inte
rface_dependencies_file, options.additional_idl_files, options.idl_attributes_fi
le, verbose=verbose) | |
90 if not idl_definitions: | |
91 # We generate dummy .h and .cpp files just to tell build scripts | 106 # We generate dummy .h and .cpp files just to tell build scripts |
92 # that outputs have been created. | 107 # that outputs have been created. |
93 code_generator_v8.generate_dummy_header_and_cpp(interface_name, options.
output_directory) | 108 code_generator_v8.generate_dummy_header_and_cpp(interface_name, options.
output_directory) |
94 return | 109 return |
| 110 if options.dump_json_and_pickle: |
| 111 write_json_and_pickle(definitions, interface_name, options.output_direct
ory) |
| 112 return |
95 # FIXME: turn on code generator | 113 # FIXME: turn on code generator |
96 # Currently idl_definitions must be None (so dummy .h and .cpp files are | 114 # Currently definitions must be None (so dummy .h and .cpp files are |
97 # generated), as actual code generator not present yet. | 115 # generated), or --dump-json-and-pickle selected, as actual code generator |
98 # code_generator_v8.write_interface(idl_definitions, interface_name, options
.output_directory) | 116 # not present yet. |
| 117 # code_generator_v8.write_interface(definitions, interface_name, options.out
put_directory) |
99 raise RuntimeError('Stub: code generator not implemented yet') | 118 raise RuntimeError('Stub: code generator not implemented yet') |
100 | 119 |
101 | 120 |
102 if __name__ == '__main__': | 121 if __name__ == '__main__': |
103 sys.exit(main()) | 122 sys.exit(main()) |
OLD | NEW |