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.
 
 

62 lines
2.0 KiB

# -*- coding: utf-8 -*-
# This file is under GNU Affero General Public License 3.0
# see LICENSE.txt
from . import c_2_ast
from . import ast_2_acid
from . import acid_yml
from . import utils
import os
class pEpACIDGen:
def __init__(self, libclang_path, header_filename, pymodule_name, out_dir=None):
self.model = self._acid_model_create(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 _acid_model_create(self, path, out_dir=None):
model = {"path": "",
"dir": "",
"filename": "",
"out_dir": "",
"sourcecode": "",
"ast": "",
"acid": "",
"acid_yml": ""}
model["path"] = path
model["dir"] = os.path.dirname(path)
model["filename"] = os.path.basename(path)
model["out_dir"] = model["dir"]
if out_dir:
model["out_dir"] = out_dir
try:
model["sourcecode"] = utils.read_file(path)
except:
pass
return model
def extract(self, var_names, func_names, debug_ast=False, debug_acid=False, debug_yml=False):
# ast
self.model["ast"] = self.c2ast.parse(self.model["path"], follow_includes=True)
if debug_ast:
utils.write_json(self.model["ast"], self.model["out_dir"] + "/" + self.model["filename"] + ".ast.json")
# acid
self.model["acid"] = ast_2_acid.extract_acid_selectively(self.model["ast"], var_names, func_names)
if debug_acid:
utils.write_json(self.model["acid"], self.model["out_dir"] + "/" + self.model["filename"] + ".acid.json")
# yml
self.model["acid_yml"] = acid_yml.generate_acid_yml(self.model["acid"], self.pymodule_name)
if debug_yml:
utils.write_string(self.model["acid_yml"], self.model["out_dir"] + "/" + self.model["filename"] + ".acid.yml")
return self.model