You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.9 KiB
57 lines
1.9 KiB
# -*- coding: utf-8 -*-
|
|
|
|
from . import c_2_ast
|
|
from . import ast_2_cid
|
|
from . import cid_2_yml
|
|
from . import utils
|
|
|
|
import os
|
|
|
|
|
|
class CIDTools:
|
|
|
|
def __init__(self, libclang_path, header_filename, pymodule_name, out_dir=None):
|
|
self.header = self._create_header(header_filename, out_dir)
|
|
self.c2ast = c_2_ast.C2AST(libclang_path)
|
|
self.pymodule_name = pymodule_name
|
|
|
|
# out-dir is in-dir if not specified
|
|
def _create_header(self, path, out_dir=None):
|
|
header = {"path": "",
|
|
"dir": "",
|
|
"filename": "",
|
|
"out_dir": "",
|
|
"sourcecode": "",
|
|
"ast": "",
|
|
"cid": "",
|
|
"yml": ""}
|
|
|
|
header["path"] = path
|
|
header["dir"] = os.path.dirname(path)
|
|
header["filename"] = os.path.basename(path)
|
|
|
|
header["out_dir"] = header["dir"]
|
|
if out_dir:
|
|
header["out_dir"] = out_dir
|
|
|
|
header["sourcecode"] = utils.read_file(path)
|
|
return header
|
|
|
|
|
|
def extract(self, var_names, func_names, debug_ast=False, debug_cid=False, debug_yml=False):
|
|
# ast
|
|
self.header["ast"] = self.c2ast.parse(self.header["path"], follow_includes=True)
|
|
if debug_ast:
|
|
utils.write_json(self.header["ast"], self.header["out_dir"] + "/" + self.header["filename"] + ".ast.json")
|
|
|
|
# cid
|
|
self.header["cid"] = ast_2_cid.extract_cid_selectively(self.header["ast"], var_names, func_names)
|
|
if debug_cid:
|
|
utils.write_json(self.header["cid"], self.header["out_dir"] + "/" + self.header["filename"] + ".cid.json")
|
|
|
|
# yml
|
|
self.header["yml"] = cid_2_yml.generate_yml(self.header["cid"], self.pymodule_name)
|
|
if debug_yml:
|
|
utils.write_string(self.header["yml"], self.header["out_dir"] + "/" + self.header["filename"] + ".cid.yml")
|
|
|
|
return self.header
|
|
|