| OLD | NEW |
| 1 # Copyright 2017 The Chromium Authors. All rights reserved. | 1 # Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import ast | 5 import ast |
| 6 import optparse | 6 import optparse |
| 7 import os.path | 7 import os.path |
| 8 import re | 8 import re |
| 9 import sys | 9 import sys |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 | 41 |
| 42 def to_singular(text): | 42 def to_singular(text): |
| 43 return text[:-1] if text[-1] == "s" else text | 43 return text[:-1] if text[-1] == "s" else text |
| 44 | 44 |
| 45 | 45 |
| 46 def to_lower_case(name): | 46 def to_lower_case(name): |
| 47 return name[:1].lower() + name[1:] | 47 return name[:1].lower() + name[1:] |
| 48 | 48 |
| 49 | 49 |
| 50 def agent_config(agent_name, field): | 50 def agent_config(agent_name, field): |
| 51 observers = config["observers"] | 51 return config["observers"].get(agent_name, {}).get(field) |
| 52 if agent_name not in observers: | |
| 53 return None | |
| 54 return observers[agent_name][field] if field in observers[agent_name] else N
one | |
| 55 | 52 |
| 56 | 53 |
| 57 def agent_name_to_class(agent_name): | 54 def agent_name_to_class(agent_name): |
| 58 return agent_config(agent_name, "class") or "Inspector%sAgent" % agent_name | 55 return agent_config(agent_name, "class") or "Inspector%sAgent" % agent_name |
| 59 | 56 |
| 60 | 57 |
| 61 def agent_name_to_include(agent_name): | 58 def agent_name_to_include(agent_name): |
| 62 include_path = agent_config(agent_name, "include") or config["settings"]["de
fault_include"] | 59 include_path = agent_config(agent_name, "include_path") or config["settings"
]["include_path"] |
| 63 return os.path.join(include_path, agent_name_to_class(agent_name) + ".h") | 60 return os.path.join(include_path, agent_name_to_class(agent_name) + ".h") |
| 64 | 61 |
| 65 | 62 |
| 66 def initialize_jinja_env(cache_dir): | 63 def initialize_jinja_env(cache_dir): |
| 67 jinja_env = jinja2.Environment( | 64 jinja_env = jinja2.Environment( |
| 68 loader=jinja2.FileSystemLoader(os.path.join(module_path, "templates")), | 65 loader=jinja2.FileSystemLoader(os.path.join(module_path, "templates")), |
| 69 # Bytecode cache is not concurrency-safe unless pre-cached: | 66 # Bytecode cache is not concurrency-safe unless pre-cached: |
| 70 # if pre-cached this is read-only, but writing creates a race condition. | 67 # if pre-cached this is read-only, but writing creates a race condition. |
| 71 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), | 68 bytecode_cache=jinja2.FileSystemBytecodeCache(cache_dir), |
| 72 keep_trailing_newline=True, # newline-terminate generated files | 69 keep_trailing_newline=True, # newline-terminate generated files |
| (...skipping 23 matching lines...) Expand all Loading... |
| 96 model = [] | 93 model = [] |
| 97 while len(source): | 94 while len(source): |
| 98 match, source = match_and_consume(r"interface\s(\w*)\s?\{([^\{]*)\}", so
urce) | 95 match, source = match_and_consume(r"interface\s(\w*)\s?\{([^\{]*)\}", so
urce) |
| 99 if not match: | 96 if not match: |
| 100 sys.stderr.write("Cannot parse %s\n" % source[:100]) | 97 sys.stderr.write("Cannot parse %s\n" % source[:100]) |
| 101 sys.exit(1) | 98 sys.exit(1) |
| 102 model.append(File(match.group(1), match.group(2))) | 99 model.append(File(match.group(1), match.group(2))) |
| 103 return model | 100 return model |
| 104 | 101 |
| 105 | 102 |
| 106 def include_probes_header(): | |
| 107 return "#include \"%s\"" % config["settings"]["probes_header"] | |
| 108 | |
| 109 | |
| 110 class File(object): | 103 class File(object): |
| 111 def __init__(self, name, source): | 104 def __init__(self, name, source): |
| 112 self.name = name | 105 self.name = name |
| 113 self.header_name = self.name + "Inl" | 106 self.header_name = self.name + "Inl" |
| 114 self.includes = [include_probes_header()] | |
| 115 self.forward_declarations = [] | 107 self.forward_declarations = [] |
| 116 self.declarations = [] | 108 self.declarations = [] |
| 117 for line in map(str.strip, source.split("\n")): | 109 for line in map(str.strip, source.split("\n")): |
| 118 line = re.sub(r"\s{2,}", " ", line).strip() # Collapse whitespace | 110 line = re.sub(r"\s{2,}", " ", line).strip() # Collapse whitespace |
| 119 if len(line) == 0: | 111 if len(line) == 0: |
| 120 continue | 112 continue |
| 121 elif line.startswith("#include"): | |
| 122 self.includes.append(line) | |
| 123 elif line.startswith("class ") or line.startswith("struct "): | 113 elif line.startswith("class ") or line.startswith("struct "): |
| 124 self.forward_declarations.append(line) | 114 self.forward_declarations.append(line) |
| 125 else: | 115 else: |
| 126 self.declarations.append(Method(line)) | 116 self.declarations.append(Method(line)) |
| 127 self.includes.sort() | |
| 128 self.forward_declarations.sort() | 117 self.forward_declarations.sort() |
| 129 | 118 |
| 130 | 119 |
| 131 class Method(object): | 120 class Method(object): |
| 132 def __init__(self, source): | 121 def __init__(self, source): |
| 133 match = re.match(r"(?:(\w+\*?)\s+)?(\w+)\s*\((.*)\)\s*;", source) | 122 match = re.match(r"(?:(\w+\*?)\s+)?(\w+)\s*\((.*)\)\s*;", source) |
| 134 if not match: | 123 if not match: |
| 135 sys.stderr.write("Cannot parse %s\n" % source) | 124 sys.stderr.write("Cannot parse %s\n" % source) |
| 136 sys.exit(1) | 125 sys.exit(1) |
| 137 | 126 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 all_observers = set() | 193 all_observers = set() |
| 205 observers_by_probe = {} | 194 observers_by_probe = {} |
| 206 unused_probes = set(all_pidl_probes) | 195 unused_probes = set(all_pidl_probes) |
| 207 for observer_name in config["observers"]: | 196 for observer_name in config["observers"]: |
| 208 all_observers.add(observer_name) | 197 all_observers.add(observer_name) |
| 209 observer = config["observers"][observer_name] | 198 observer = config["observers"][observer_name] |
| 210 for probe in observer["probes"]: | 199 for probe in observer["probes"]: |
| 211 unused_probes.discard(probe) | 200 unused_probes.discard(probe) |
| 212 if probe not in all_pidl_probes: | 201 if probe not in all_pidl_probes: |
| 213 raise Exception('Probe %s is not declared in PIDL file' % probe) | 202 raise Exception('Probe %s is not declared in PIDL file' % probe) |
| 214 if probe not in observers_by_probe: | 203 observers_by_probe.setdefault(probe, set()).add(observer_name) |
| 215 observers_by_probe[probe] = set() | |
| 216 observers_by_probe[probe].add(observer_name) | |
| 217 if unused_probes: | 204 if unused_probes: |
| 218 raise Exception("Unused probes: %s" % unused_probes) | 205 raise Exception("Unused probes: %s" % unused_probes) |
| 219 | 206 |
| 220 for f in files: | 207 for f in files: |
| 221 for probe in f.declarations: | 208 for probe in f.declarations: |
| 222 probe.agents = observers_by_probe[probe.name] | 209 probe.agents = observers_by_probe[probe.name] |
| 223 return all_observers | 210 return all_observers |
| 224 | 211 |
| 225 | 212 |
| 226 cmdline_parser = optparse.OptionParser() | 213 cmdline_parser = optparse.OptionParser() |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 sink_h_file = open(output_dirpath + "/" + to_singular(base_name) + "Sink.h", "w"
) | 257 sink_h_file = open(output_dirpath + "/" + to_singular(base_name) + "Sink.h", "w"
) |
| 271 sink_h_file.write(sink_h_template.render(template_context)) | 258 sink_h_file.write(sink_h_template.render(template_context)) |
| 272 sink_h_file.close() | 259 sink_h_file.close() |
| 273 | 260 |
| 274 for f in files: | 261 for f in files: |
| 275 template_context["file"] = f | 262 template_context["file"] = f |
| 276 h_template = jinja_env.get_template("/InstrumentingProbesInl.h.tmpl") | 263 h_template = jinja_env.get_template("/InstrumentingProbesInl.h.tmpl") |
| 277 h_file = open(output_dirpath + "/" + f.header_name + ".h", "w") | 264 h_file = open(output_dirpath + "/" + f.header_name + ".h", "w") |
| 278 h_file.write(h_template.render(template_context)) | 265 h_file.write(h_template.render(template_context)) |
| 279 h_file.close() | 266 h_file.close() |
| OLD | NEW |