Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: third_party/WebKit/Source/platform/inspector_protocol/CodeGenerator.py

Issue 2452853002: [inspector] use own version of third_party/inspector_protocol (Closed)
Patch Set: supported new inspector_protocol location in components/ui_devtools/BUILD.gn Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 import os.path
6 import sys
7 import optparse
8 import collections
9 import functools
10 try:
11 import json
12 except ImportError:
13 import simplejson as json
14
15 # Path handling for libraries and templates
16 # Paths have to be normalized because Jinja uses the exact template path to
17 # determine the hash used in the cache filename, and we need a pre-caching step
18 # to be concurrency-safe. Use absolute path because __file__ is absolute if
19 # module is imported, and relative if executed directly.
20 # If paths differ between pre-caching and individual file compilation, the cache
21 # is regenerated, which causes a race condition and breaks concurrent build,
22 # since some compile processes will try to read the partially written cache.
23 module_path, module_filename = os.path.split(os.path.realpath(__file__))
24
25 def read_config():
26 # pylint: disable=W0703
27 def json_to_object(data, output_base, config_base):
28 def json_object_hook(object_dict):
29 items = [(k, os.path.join(config_base, v) if k == "path" else v) for (k, v) in object_dict.items()]
30 items = [(k, os.path.join(output_base, v) if k == "output" else v) f or (k, v) in items]
31 keys, values = zip(*items)
32 return collections.namedtuple('X', keys)(*values)
33 return json.loads(data, object_hook=json_object_hook)
34
35 def init_defaults(config_tuple, path, defaults):
36 keys = list(config_tuple._fields) # pylint: disable=E1101
37 values = [getattr(config_tuple, k) for k in keys]
38 for i in xrange(len(keys)):
39 if hasattr(values[i], "_fields"):
40 values[i] = init_defaults(values[i], path + "." + keys[i], defau lts)
41 for optional in defaults:
42 if optional.find(path + ".") != 0:
43 continue
44 optional_key = optional[len(path) + 1:]
45 if optional_key.find(".") == -1 and optional_key not in keys:
46 keys.append(optional_key)
47 values.append(defaults[optional])
48 return collections.namedtuple('X', keys)(*values)
49
50 try:
51 cmdline_parser = optparse.OptionParser()
52 cmdline_parser.add_option("--output_base")
53 cmdline_parser.add_option("--jinja_dir")
54 cmdline_parser.add_option("--config")
55 arg_options, _ = cmdline_parser.parse_args()
56 jinja_dir = arg_options.jinja_dir
57 if not jinja_dir:
58 raise Exception("jinja directory must be specified")
59 output_base = arg_options.output_base
60 if not output_base:
61 raise Exception("Base output directory must be specified")
62 config_file = arg_options.config
63 if not config_file:
64 raise Exception("Config file name must be specified")
65 config_base = os.path.dirname(config_file)
66 except Exception:
67 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h tml
68 exc = sys.exc_info()[1]
69 sys.stderr.write("Failed to parse command-line arguments: %s\n\n" % exc)
70 exit(1)
71
72 try:
73 config_json_file = open(config_file, "r")
74 config_json_string = config_json_file.read()
75 config_partial = json_to_object(config_json_string, output_base, config_ base)
76 config_json_file.close()
77 defaults = {
78 ".imported": False,
79 ".imported.export_macro": "",
80 ".imported.export_header": False,
81 ".imported.header": False,
82 ".imported.package": False,
83 ".protocol.export_macro": "",
84 ".protocol.export_header": False,
85 ".exported": False,
86 ".exported.export_macro": "",
87 ".exported.export_header": False,
88 ".lib": False,
89 ".lib.export_macro": "",
90 ".lib.export_header": False,
91 }
92 return (jinja_dir, config_file, init_defaults(config_partial, "", defaul ts))
93 except Exception:
94 # Work with python 2 and 3 http://docs.python.org/py3k/howto/pyporting.h tml
95 exc = sys.exc_info()[1]
96 sys.stderr.write("Failed to parse config file: %s\n\n" % exc)
97 exit(1)
98
99
100 def to_title_case(name):
101 return name[:1].upper() + name[1:]
102
103
104 def dash_to_camelcase(word):
105 prefix = ""
106 if word[0] == "-":
107 prefix = "Negative"
108 word = word[1:]
109 return prefix + "".join(to_title_case(x) or "-" for x in word.split("-"))
110
111
112 def initialize_jinja_env(jinja_dir, cache_dir):
113 # pylint: disable=F0401
114 sys.path.insert(1, os.path.abspath(jinja_dir))
115 import jinja2
116
117 jinja_env = jinja2.Environment(
118 loader=jinja2.FileSystemLoader(module_path),
119 # Bytecode cache is not concurrency-safe unless pre-cached:
120 # if pre-cached this is read-only, but writing creates a race condition.
121 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir),
122 keep_trailing_newline=True, # newline-terminate generated files
123 lstrip_blocks=True, # so can indent control flow tags
124 trim_blocks=True)
125 jinja_env.filters.update({"to_title_case": to_title_case, "dash_to_camelcase ": dash_to_camelcase})
126 jinja_env.add_extension("jinja2.ext.loopcontrols")
127 return jinja_env
128
129
130 def patch_full_qualified_refs(protocol):
131 def patch_full_qualified_refs_in_domain(json, domain_name):
132 if isinstance(json, list):
133 for item in json:
134 patch_full_qualified_refs_in_domain(item, domain_name)
135
136 if not isinstance(json, dict):
137 return
138 for key in json:
139 if key == "type" and json[key] == "string":
140 json[key] = domain_name + ".string"
141 if key != "$ref":
142 patch_full_qualified_refs_in_domain(json[key], domain_name)
143 continue
144 if json["$ref"].find(".") == -1:
145 json["$ref"] = domain_name + "." + json["$ref"]
146 return
147
148 for domain in protocol.json_api["domains"]:
149 patch_full_qualified_refs_in_domain(domain, domain["domain"])
150
151
152 def calculate_exports(protocol):
153 def calculate_exports_in_json(json_value):
154 has_exports = False
155 if isinstance(json_value, list):
156 for item in json_value:
157 has_exports = calculate_exports_in_json(item) or has_exports
158 if isinstance(json_value, dict):
159 has_exports = ("exported" in json_value and json_value["exported"]) or has_exports
160 for key in json_value:
161 has_exports = calculate_exports_in_json(json_value[key]) or has_ exports
162 return has_exports
163
164 protocol.json_api["has_exports"] = False
165 for domain_json in protocol.json_api["domains"]:
166 domain_json["has_exports"] = calculate_exports_in_json(domain_json)
167 if domain_json["has_exports"] and domain_json["domain"] in protocol.gene rate_domains:
168 protocol.json_api["has_exports"] = True
169
170
171 def create_imported_type_definition(domain_name, type, imported_namespace):
172 # pylint: disable=W0622
173 return {
174 "return_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain_name, type["id"]),
175 "pass_type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, d omain_name, type["id"]),
176 "to_raw_type": "%s.get()",
177 "to_pass_type": "std::move(%s)",
178 "to_rvalue": "std::move(%s)",
179 "type": "std::unique_ptr<%s::%s::API::%s>" % (imported_namespace, domain _name, type["id"]),
180 "raw_type": "%s::%s::API::%s" % (imported_namespace, domain_name, type[" id"]),
181 "raw_pass_type": "%s::%s::API::%s*" % (imported_namespace, domain_name, type["id"]),
182 "raw_return_type": "%s::%s::API::%s*" % (imported_namespace, domain_name , type["id"]),
183 }
184
185
186 def create_user_type_definition(domain_name, type):
187 # pylint: disable=W0622
188 return {
189 "return_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type[ "id"]),
190 "pass_type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["i d"]),
191 "to_raw_type": "%s.get()",
192 "to_pass_type": "std::move(%s)",
193 "to_rvalue": "std::move(%s)",
194 "type": "std::unique_ptr<protocol::%s::%s>" % (domain_name, type["id"]),
195 "raw_type": "protocol::%s::%s" % (domain_name, type["id"]),
196 "raw_pass_type": "protocol::%s::%s*" % (domain_name, type["id"]),
197 "raw_return_type": "protocol::%s::%s*" % (domain_name, type["id"]),
198 }
199
200
201 def create_object_type_definition():
202 # pylint: disable=W0622
203 return {
204 "return_type": "std::unique_ptr<protocol::DictionaryValue>",
205 "pass_type": "std::unique_ptr<protocol::DictionaryValue>",
206 "to_raw_type": "%s.get()",
207 "to_pass_type": "std::move(%s)",
208 "to_rvalue": "std::move(%s)",
209 "type": "std::unique_ptr<protocol::DictionaryValue>",
210 "raw_type": "protocol::DictionaryValue",
211 "raw_pass_type": "protocol::DictionaryValue*",
212 "raw_return_type": "protocol::DictionaryValue*",
213 }
214
215
216 def create_any_type_definition():
217 # pylint: disable=W0622
218 return {
219 "return_type": "std::unique_ptr<protocol::Value>",
220 "pass_type": "std::unique_ptr<protocol::Value>",
221 "to_raw_type": "%s.get()",
222 "to_pass_type": "std::move(%s)",
223 "to_rvalue": "std::move(%s)",
224 "type": "std::unique_ptr<protocol::Value>",
225 "raw_type": "protocol::Value",
226 "raw_pass_type": "protocol::Value*",
227 "raw_return_type": "protocol::Value*",
228 }
229
230
231 def create_string_type_definition():
232 # pylint: disable=W0622
233 return {
234 "return_type": "String",
235 "pass_type": "const String&",
236 "to_pass_type": "%s",
237 "to_raw_type": "%s",
238 "to_rvalue": "%s",
239 "type": "String",
240 "raw_type": "String",
241 "raw_pass_type": "const String&",
242 "raw_return_type": "String",
243 }
244
245
246 def create_primitive_type_definition(type):
247 # pylint: disable=W0622
248 typedefs = {
249 "number": "double",
250 "integer": "int",
251 "boolean": "bool"
252 }
253 defaults = {
254 "number": "0",
255 "integer": "0",
256 "boolean": "false"
257 }
258 jsontypes = {
259 "number": "TypeDouble",
260 "integer": "TypeInteger",
261 "boolean": "TypeBoolean",
262 }
263 return {
264 "return_type": typedefs[type],
265 "pass_type": typedefs[type],
266 "to_pass_type": "%s",
267 "to_raw_type": "%s",
268 "to_rvalue": "%s",
269 "type": typedefs[type],
270 "raw_type": typedefs[type],
271 "raw_pass_type": typedefs[type],
272 "raw_return_type": typedefs[type],
273 "default_value": defaults[type]
274 }
275
276
277 def wrap_array_definition(type):
278 # pylint: disable=W0622
279 return {
280 "return_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"] ,
281 "pass_type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"],
282 "to_raw_type": "%s.get()",
283 "to_pass_type": "std::move(%s)",
284 "to_rvalue": "std::move(%s)",
285 "type": "std::unique_ptr<protocol::Array<%s>>" % type["raw_type"],
286 "raw_type": "protocol::Array<%s>" % type["raw_type"],
287 "raw_pass_type": "protocol::Array<%s>*" % type["raw_type"],
288 "raw_return_type": "protocol::Array<%s>*" % type["raw_type"],
289 "create_type": "wrapUnique(new protocol::Array<%s>())" % type["raw_type" ],
290 "out_type": "protocol::Array<%s>&" % type["raw_type"],
291 }
292
293
294 def create_type_definitions(protocol, imported_namespace):
295 protocol.type_definitions = {}
296 protocol.type_definitions["number"] = create_primitive_type_definition("numb er")
297 protocol.type_definitions["integer"] = create_primitive_type_definition("int eger")
298 protocol.type_definitions["boolean"] = create_primitive_type_definition("boo lean")
299 protocol.type_definitions["object"] = create_object_type_definition()
300 protocol.type_definitions["any"] = create_any_type_definition()
301 for domain in protocol.json_api["domains"]:
302 protocol.type_definitions[domain["domain"] + ".string"] = create_string_ type_definition()
303 if not ("types" in domain):
304 continue
305 for type in domain["types"]:
306 type_name = domain["domain"] + "." + type["id"]
307 if type["type"] == "object" and domain["domain"] in protocol.importe d_domains:
308 protocol.type_definitions[type_name] = create_imported_type_defi nition(domain["domain"], type, imported_namespace)
309 elif type["type"] == "object":
310 protocol.type_definitions[type_name] = create_user_type_definiti on(domain["domain"], type)
311 elif type["type"] == "array":
312 items_type = type["items"]["type"]
313 protocol.type_definitions[type_name] = wrap_array_definition(pro tocol.type_definitions[items_type])
314 elif type["type"] == domain["domain"] + ".string":
315 protocol.type_definitions[type_name] = create_string_type_defini tion()
316 else:
317 protocol.type_definitions[type_name] = create_primitive_type_def inition(type["type"])
318
319
320 def type_definition(protocol, name):
321 return protocol.type_definitions[name]
322
323
324 def resolve_type(protocol, prop):
325 if "$ref" in prop:
326 return protocol.type_definitions[prop["$ref"]]
327 if prop["type"] == "array":
328 return wrap_array_definition(resolve_type(protocol, prop["items"]))
329 return protocol.type_definitions[prop["type"]]
330
331
332 def join_arrays(dict, keys):
333 result = []
334 for key in keys:
335 if key in dict:
336 result += dict[key]
337 return result
338
339
340 def has_disable(commands):
341 for command in commands:
342 if command["name"] == "disable":
343 return True
344 return False
345
346
347 def format_include(header):
348 return "\"" + header + "\"" if header[0] not in "<\"" else header
349
350
351 def read_protocol_file(file_name, json_api):
352 input_file = open(file_name, "r")
353 json_string = input_file.read()
354 input_file.close()
355 parsed_json = json.loads(json_string)
356 version = parsed_json["version"]["major"] + "." + parsed_json["version"]["mi nor"]
357 domains = []
358 for domain in parsed_json["domains"]:
359 domains.append(domain["domain"])
360 domain["version"] = version
361 json_api["domains"] += parsed_json["domains"]
362 return domains
363
364
365 class Protocol(object):
366 def __init__(self):
367 self.json_api = {}
368 self.generate_domains = []
369 self.imported_domains = []
370
371
372 def main():
373 jinja_dir, config_file, config = read_config()
374
375 protocol = Protocol()
376 protocol.json_api = {"domains": []}
377 protocol.generate_domains = read_protocol_file(config.protocol.path, protoco l.json_api)
378 protocol.imported_domains = read_protocol_file(config.imported.path, protoco l.json_api) if config.imported else []
379 patch_full_qualified_refs(protocol)
380 calculate_exports(protocol)
381 create_type_definitions(protocol, "::".join(config.imported.namespace) if co nfig.imported else "")
382
383 if not config.exported:
384 for domain_json in protocol.json_api["domains"]:
385 if domain_json["has_exports"] and domain_json["domain"] in protocol. generate_domains:
386 sys.stderr.write("Domain %s is exported, but config is missing e xport entry\n\n" % domain_json["domain"])
387 exit(1)
388
389 if not os.path.exists(config.protocol.output):
390 os.mkdir(config.protocol.output)
391 if protocol.json_api["has_exports"] and not os.path.exists(config.exported.o utput):
392 os.mkdir(config.exported.output)
393 jinja_env = initialize_jinja_env(jinja_dir, config.protocol.output)
394
395 inputs = []
396 inputs.append(__file__)
397 inputs.append(config_file)
398 inputs.append(config.protocol.path)
399 if config.imported:
400 inputs.append(config.imported.path)
401 templates_dir = os.path.join(module_path, "templates")
402 inputs.append(os.path.join(templates_dir, "TypeBuilder_h.template"))
403 inputs.append(os.path.join(templates_dir, "TypeBuilder_cpp.template"))
404 inputs.append(os.path.join(templates_dir, "Exported_h.template"))
405 inputs.append(os.path.join(templates_dir, "Imported_h.template"))
406
407 h_template = jinja_env.get_template("templates/TypeBuilder_h.template")
408 cpp_template = jinja_env.get_template("templates/TypeBuilder_cpp.template")
409 exported_template = jinja_env.get_template("templates/Exported_h.template")
410 imported_template = jinja_env.get_template("templates/Imported_h.template")
411
412 outputs = dict()
413
414 for domain in protocol.json_api["domains"]:
415 class_name = domain["domain"]
416 template_context = {
417 "config": config,
418 "domain": domain,
419 "join_arrays": join_arrays,
420 "resolve_type": functools.partial(resolve_type, protocol),
421 "type_definition": functools.partial(type_definition, protocol),
422 "has_disable": has_disable,
423 "format_include": format_include,
424 }
425
426 if domain["domain"] in protocol.generate_domains:
427 outputs[os.path.join(config.protocol.output, class_name + ".h")] = h _template.render(template_context)
428 outputs[os.path.join(config.protocol.output, class_name + ".cpp")] = cpp_template.render(template_context)
429 if domain["has_exports"]:
430 outputs[os.path.join(config.exported.output, class_name + ".h")] = exported_template.render(template_context)
431 if domain["domain"] in protocol.imported_domains and domain["has_exports "]:
432 outputs[os.path.join(config.protocol.output, class_name + ".h")] = i mported_template.render(template_context)
433
434 if config.lib:
435 template_context = {
436 "config": config,
437 "format_include": format_include,
438 }
439
440 lib_templates_dir = os.path.join(module_path, "lib")
441 # Note these should be sorted in the right order.
442 # TODO(dgozman): sort them programmatically based on commented includes.
443 lib_h_templates = [
444 "Collections_h.template",
445 "ErrorSupport_h.template",
446 "Values_h.template",
447 "Object_h.template",
448 "ValueConversions_h.template",
449 "Maybe_h.template",
450 "Array_h.template",
451 "BackendCallback_h.template",
452 "DispatcherBase_h.template",
453 "Parser_h.template",
454 ]
455
456 lib_cpp_templates = [
457 "Protocol_cpp.template",
458 "ErrorSupport_cpp.template",
459 "Values_cpp.template",
460 "Object_cpp.template",
461 "DispatcherBase_cpp.template",
462 "Parser_cpp.template",
463 ]
464
465 forward_h_templates = [
466 "Forward_h.template",
467 "Allocator_h.template",
468 "FrontendChannel_h.template",
469 ]
470
471 def generate_lib_file(file_name, template_files):
472 parts = []
473 for template_file in template_files:
474 inputs.append(os.path.join(lib_templates_dir, template_file))
475 template = jinja_env.get_template("lib/" + template_file)
476 parts.append(template.render(template_context))
477 outputs[file_name] = "\n\n".join(parts)
478
479 generate_lib_file(os.path.join(config.lib.output, "Forward.h"), forward_ h_templates)
480 generate_lib_file(os.path.join(config.lib.output, "Protocol.h"), lib_h_t emplates)
481 generate_lib_file(os.path.join(config.lib.output, "Protocol.cpp"), lib_c pp_templates)
482
483 # Make gyp / make generatos happy, otherwise make rebuilds world.
484 inputs_ts = max(map(os.path.getmtime, inputs))
485 up_to_date = True
486 for output_file in outputs.iterkeys():
487 if not os.path.exists(output_file) or os.path.getmtime(output_file) < in puts_ts:
488 up_to_date = False
489 break
490 if up_to_date:
491 sys.exit()
492
493 for file_name, content in outputs.iteritems():
494 out_file = open(file_name, "w")
495 out_file.write(content)
496 out_file.close()
497
498
499 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698