1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
#!/usr/bin/env vpython3
# Copyright 2025 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Prints all of the Chrome tracing stdlib as an amalgamation (a single
# PerfettoSQL source which includes all transitive dependencies).
#
# All "CREATE PERFETTO" instructions are also going to be replaced with
# "CREATE OR REPLACE PERFETTO" instructions to ensure that the amalgamation
# can be used on top of existing stdlib.
#
# Usage: print_stdlib_amalgamation.py [chrome.module1 chrome.module2 ...]
from dataclasses import dataclass
from typing import Collection, Dict, List, Set
from pathlib import Path
import os
import re
import sys
CHROME_STDLIB_DIR = Path(
os.path.dirname(__file__)) / '../../base/tracing/stdlib'
def ReadStdlib(path: Path) -> Dict[str, str]:
"""Reads all of the SQL modules in the given directory.
Args:
path: The path to the directory containing the SQL modules.
Returns:
A dictionary mapping module names to their contents.
"""
result = {}
for root, _, files in os.walk(path):
for file in files:
if not file.endswith('.sql'):
continue
with open(os.path.join(root, file)) as f:
module = os.path.join(os.path.relpath(root, path),
file.removesuffix('.sql')).replace(
os.path.sep, '.')
result[module] = f.read()
return result
@dataclass
class Module:
"""A class representing a parsed SQL module."""
name: str
includes: List[str]
content: str
def ParseModule(name: str, content: str,
known_stdlib_modules: Collection[str]) -> Module:
"""
Parses a given SQL modules, resolving includes and updating
CREATE PERFETTO ... statements to CREATE OR REPLACE.
Args:
name: name of the module.
content: raw contents of the module.
known_stdlib_modules: a list of known stdlib modules.
Returns:
A parsed Module object.
"""
includes = []
def ReplaceInclude(match):
included_module = match.group(1)
if included_module in known_stdlib_modules:
includes.append(included_module)
return ''
return match.group(0)
content = re.sub(r'INCLUDE PERFETTO MODULE ([\w\.]*);', ReplaceInclude,
content)
content = content.replace('CREATE PERFETTO', 'CREATE OR REPLACE PERFETTO')
# If the module doesn't end with a semicolon, add one to terminate the statement.
if not content.strip().endswith(';'):
content += ';'
return Module(name, includes, content)
def ParseModules(stdlib: Dict[str, str]) -> List[Module]:
"""
Parses the given SQL modules, resolving includes and updating
CREATE PERFETTO ... statements to CREATE OR REPLACE.
Args:
stdlib: A dictionary mapping module names to their contents.
Returns:
A list of Module objects, each representing a parsed module.
"""
return [
ParseModule(name, content, stdlib.keys())
for name, content in stdlib.items()
]
def SortModules(stdlib: List[Module],
targets: List[str] | None = None) -> List[Module]:
"""
Topologically sorts the given modules based on their includes.
Assumes that there are no circular includes -- otherwise the behaviour is undefined.
Args:
stdlib: A list of Module objects.
targets: A list of module names to sort. If None, all modules will be sorted.
Returns:
A list of Module objects, sorted in topological order.
"""
modules_by_name = {m.name: m for m in stdlib}
result = []
visited = set()
def AddModule(module: Module):
if module.name in visited:
return
visited.add(module.name)
for include in module.includes:
AddModule(modules_by_name[include])
result.append(module)
if targets is None:
targets = stdlib.keys()
for target in targets:
AddModule(modules_by_name[target])
return result
def PrintModules(modules: List[Module]):
for m in modules:
print()
print()
print('--')
print('--', m.name)
print('--')
print()
print()
print(m.content)
if __name__ == '__main__':
chrome_stdlib = ReadStdlib(CHROME_STDLIB_DIR)
PrintModules(SortModules(ParseModules(chrome_stdlib), sys.argv[1:] or None))
|