osquery-1/tools/codegen/genapi.py

189 lines
5.8 KiB
Python
Raw Normal View History

2014-11-05 09:52:40 +00:00
#!/usr/bin/env python
# Copyright (c) 2014, Facebook, Inc.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. An additional grant
# of patent rights can be found in the PATENTS file in the same directory.
2014-11-05 09:52:40 +00:00
from __future__ import absolute_import
from __future__ import division
2014-11-07 01:12:40 +00:00
from __future__ import print_function
2014-11-05 09:52:40 +00:00
from __future__ import unicode_literals
import argparse
import ast
2014-11-07 01:12:40 +00:00
import json
2014-11-05 09:52:40 +00:00
import logging
import os
import sys
2014-11-07 01:12:40 +00:00
import uuid
2014-11-05 09:52:40 +00:00
from gentable import Column, ForeignKey, \
table_name, schema, implementation, description, table, \
DataType, BIGINT, DATE, DATETIME, INTEGER, TEXT, \
is_blacklisted
2014-11-05 09:52:40 +00:00
# the log format for the logging module
LOG_FORMAT = "%(levelname)s [Line %(lineno)d]: %(message)s"
CANONICAL_PLATFORMS = {
2014-11-07 01:12:40 +00:00
"x": "All Platforms",
"darwin": "Darwin (Apple OS X)",
"linux": "Ubuntu, CentOS",
2014-11-05 09:52:40 +00:00
}
TEMPLATE_API_DEFINITION = """
/** @jsx React.DOM */
2014-11-07 01:12:40 +00:00
/** This page is automatically generated by genapi.py, do not edit! */
2014-11-05 09:52:40 +00:00
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
2014-11-05 09:52:40 +00:00
'use strict';
2014-11-07 01:12:40 +00:00
var API = %s;
2014-11-05 09:52:40 +00:00
module.exports = API;
"""
2014-11-07 01:12:40 +00:00
class NoIndent(object):
2014-11-07 01:12:40 +00:00
"""Special instance checked object for removing json newlines."""
2014-11-07 01:12:40 +00:00
def __init__(self, value):
self.value = value
if 'type' in self.value and isinstance(self.value['type'], DataType):
self.value['type'] = str(self.value['type'])
2014-11-07 01:12:40 +00:00
2014-11-07 01:12:40 +00:00
class Encoder(json.JSONEncoder):
2014-11-07 01:12:40 +00:00
"""
Newlines are such a pain in json-generated output.
Use this custom encoder to produce pretty json multiplexed with a more
raw json output within.
"""
2014-11-07 01:12:40 +00:00
def __init__(self, *args, **kwargs):
super(Encoder, self).__init__(*args, **kwargs)
self.kwargs = dict(kwargs)
del self.kwargs['indent']
self._replacement_map = {}
def default(self, o):
if isinstance(o, NoIndent):
key = uuid.uuid4().hex
self._replacement_map[key] = json.dumps(o.value, **self.kwargs)
return "@@%s@@" % (key,)
else:
return super(Encoder, self).default(o)
def encode(self, o):
result = super(Encoder, self).encode(o)
for k, v in self._replacement_map.iteritems():
result = result.replace('"@@%s@@"' % (k,), v)
return result
2014-11-05 09:52:40 +00:00
2014-11-05 09:52:40 +00:00
def gen_api(api):
2014-11-07 01:12:40 +00:00
"""Apply the api literal object to the template."""
api = json.dumps(
api, cls=Encoder, sort_keys=True, indent=1, separators=(',', ': ')
)
2014-11-07 01:12:40 +00:00
return TEMPLATE_API_DEFINITION % (api)
2014-11-05 09:52:40 +00:00
2014-11-05 09:52:40 +00:00
def gen_spec(tree):
2014-11-07 01:12:40 +00:00
"""Given a table tree, produce a literal of the table representation."""
exec(compile(tree, "<string>", "exec"))
2014-11-11 16:35:25 +00:00
columns = [NoIndent({
"name": column.name,
"type": column.type,
"description": column.description,
}) for column in table.columns()]
2014-11-07 01:12:40 +00:00
foreign_keys = [NoIndent({"column": key.column, "table": key.table})
for key in table.foreign_keys()]
2014-11-07 01:12:40 +00:00
return {
"name": table.table_name,
"columns": columns,
"foreign_keys": foreign_keys,
"function": table.function,
"description": table.description,
}
2014-11-05 09:52:40 +00:00
2014-11-05 09:52:40 +00:00
def main(argc, argv):
2014-11-07 01:12:40 +00:00
parser = argparse.ArgumentParser("Generate API documentation.")
parser.add_argument(
"--tables", default="osquery/tables/specs",
help="Path to osquery table specs"
)
parser.add_argument(
"--profile", default=None,
help="Add the results of a profile summary to the API."
)
2014-11-07 01:12:40 +00:00
args = parser.parse_args()
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
if not os.path.exists(args.tables):
logging.error("Cannot find path: %s" % (args.tables))
exit(1)
profile = {}
if args.profile is not None:
if not os.path.exists(args.profile):
logging.error("Cannot find path: %s" % (args.profile))
exit(1)
with open(args.profile, "r") as fh:
try:
profile = json.loads(fh.read())
except Exception as e:
logging.error("Cannot parse profile data: %s" % (str(e)))
exit(2)
2014-11-13 06:33:27 +00:00
# Read in the optional list of blacklisted tables
blacklist = None
blacklist_path = os.path.join(args.tables, "blacklist")
if os.path.exists(blacklist_path):
with open(blacklist_path, "r") as fh:
blacklist = fh.read()
2014-11-07 01:12:40 +00:00
categories = {}
for base, _, files in os.walk(args.tables):
2014-11-07 01:12:40 +00:00
for spec_file in files:
# Exclude blacklist specific file
if spec_file == 'blacklist':
continue
2014-11-07 01:12:40 +00:00
platform = os.path.basename(base)
platform_name = CANONICAL_PLATFORMS[platform]
name = spec_file.split(".table", 1)[0]
if platform not in categories.keys():
categories[platform] = {"name": platform_name, "tables": []}
with open(os.path.join(base, spec_file), "rU") as fh:
tree = ast.parse(fh.read())
table_spec = gen_spec(tree)
table_profile = profile.get("%s.%s" % (platform, name), {})
table_spec["profile"] = NoIndent(table_profile)
2014-11-13 06:33:27 +00:00
table_spec["blacklisted"] = is_blacklisted(table_spec["name"],
blacklist=blacklist)
2014-11-07 01:12:40 +00:00
categories[platform]["tables"].append(table_spec)
categories = [{"key": k, "name": v["name"], "tables": v["tables"]}
for k, v in categories.iteritems()]
2014-11-07 01:12:40 +00:00
print(gen_api(categories))
2014-11-05 09:52:40 +00:00
if __name__ == "__main__":
main(len(sys.argv), sys.argv)