Add a presubmit that functions names are snake_case

R=lalitm@google.com

Change-Id: Ic55ba07d4307cd97d3d854f79b41c2927b81ce34
diff --git a/tools/check_sql_modules.py b/tools/check_sql_modules.py
index 4fb349e..05b77e7 100755
--- a/tools/check_sql_modules.py
+++ b/tools/check_sql_modules.py
@@ -17,22 +17,26 @@
 # 'internal_' is documented with proper schema.
 
 import argparse
+from typing import List, Tuple
 import os
 import sys
+import re
 
 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 sys.path.append(os.path.join(ROOT_DIR))
 
-from python.generators.stdlib_docs.parse import parse_file_to_dict
+from python.generators.stdlib_docs.parse import ParsedFile, parse_file
 
 
 def main():
   parser = argparse.ArgumentParser()
   parser.add_argument(
       '--stdlib-sources',
-      default=os.path.join(ROOT_DIR, "src", "trace_processor", "stdlib"))
+      default=os.path.join(ROOT_DIR, "src", "trace_processor", "perfetto_sql",
+                           "stdlib"))
   args = parser.parse_args()
   errors = []
+  modules: List[Tuple[str, str, ParsedFile]] = []
   for root, _, files in os.walk(args.stdlib_sources, topdown=True):
     for f in files:
       path = os.path.join(root, f)
@@ -41,29 +45,30 @@
       with open(path, 'r') as f:
         sql = f.read()
 
-      res = parse_file_to_dict(path, sql)
-      errors += res if isinstance(res, list) else []
+      parsed = parse_file(path, sql)
+      modules.append((path, sql, parsed))
+
+  functions = set()
+
+  for path, sql, parsed in modules:
+    errors += parsed.errors
+
+    lines = [l.strip() for l in sql.split('\n')]
+    for line in lines:
+      # Strip the SQL comments.
+      line = re.sub(r'--.*$', '', line)
 
       # Ban the use of LIKE in non-comment lines.
-      lines = [l.strip() for l in sql.split('\n')]
-      for line in lines:
-        if line.startswith('--'):
-          continue
-
-        if 'like' in line.casefold():
-          errors.append('LIKE is banned in trace processor metrics. '
-                        'Prefer GLOB instead.')
-          errors.append('Offending file: %s' % path)
+      if 'like' in line.casefold():
+        errors.append('LIKE is banned in trace processor metrics. '
+                      'Prefer GLOB instead.')
+        errors.append('Offending file: %s' % path)
 
       # Ban the use of CREATE_FUNCTION.
-      for line in lines:
-        if line.startswith('--'):
-          continue
-
-        if 'create_function' in line.casefold():
-          errors.append('CREATE_FUNCTION is deprecated in trace processor. '
-                        'Prefer CREATE PERFETTO FUNCTION instead.')
-          errors.append('Offending file: %s' % path)
+      if 'create_function' in line.casefold():
+        errors.append('CREATE_FUNCTION is deprecated in trace processor. '
+                      'Prefer CREATE PERFETTO FUNCTION instead.')
+        errors.append('Offending file: %s' % path)
 
   sys.stderr.write("\n".join(errors))
   sys.stderr.write("\n")
diff --git a/tools/gen_stdlib_docs_json.py b/tools/gen_stdlib_docs_json.py
index dcb4023..ee99675 100755
--- a/tools/gen_stdlib_docs_json.py
+++ b/tools/gen_stdlib_docs_json.py
@@ -23,7 +23,7 @@
 ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 sys.path.append(os.path.join(ROOT_DIR))
 
-from python.generators.stdlib_docs.parse import parse_file_to_dict
+from python.generators.stdlib_docs.parse import parse_file
 
 
 def main():
@@ -70,16 +70,35 @@
     module_name = path.split("/")[0]
     import_key = path.split(".sql")[0].replace("/", ".")
 
-    docs = parse_file_to_dict(path, sql)
-    if isinstance(docs, list):
-      for d in docs:
-        print(d)
+    docs = parse_file(path, sql)
+    if len(docs.errors) > 0:
+      for e in docs.errors:
+        print(e)
       return 1
 
-    assert isinstance(docs, dict)
-    if not any(docs.values()):
-      continue
-    file_dict = {'import_key': import_key, **docs}
+    file_dict = {
+        'import_key':
+            import_key,
+        'imports': [{
+            'name': table.name,
+            'desc': table.desc,
+            'type': table.type,
+            'cols': table.cols,
+        } for table in docs.table_views],
+        'functions': [{
+            'name': function.name,
+            'desc': function.desc,
+            'args': function.args,
+            'return_type': function.return_type,
+            'return_desc': function.return_desc,
+        } for function in docs.functions],
+        'view_functions': [{
+            'name': function.name,
+            'desc': function.desc,
+            'args': function.args,
+            'cols': function.cols,
+        } for function in docs.table_functions],
+    }
     modules[module_name].append(file_dict)
 
   with open(args.json_out, 'w+') as f: