|
import ast
|
|
|
|
def extract_apis(code):
|
|
tree = ast.parse(code)
|
|
api_list = []
|
|
imported_modules = {}
|
|
scope_stack = []
|
|
|
|
class ApiExtractor(ast.NodeVisitor):
|
|
def visit_Import(self, node):
|
|
for alias in node.names:
|
|
module_name = alias.name
|
|
alias_name = alias.asname or alias.name
|
|
if scope_stack:
|
|
imported_modules[scope_stack[-1]][alias_name] = module_name
|
|
else:
|
|
imported_modules[alias_name] = module_name
|
|
top_level_module = module_name.split('.')[0]
|
|
if top_level_module not in imported_modules:
|
|
imported_modules[top_level_module] = module_name
|
|
self.generic_visit(node)
|
|
|
|
def visit_ImportFrom(self, node):
|
|
module = node.module
|
|
if module:
|
|
for alias in node.names:
|
|
full_name = f'{module}.{alias.name}'
|
|
alias_name = alias.asname or alias.name
|
|
if scope_stack:
|
|
imported_modules[scope_stack[-1]][alias_name] = full_name
|
|
else:
|
|
imported_modules[alias_name] = full_name
|
|
top_level_module = module.split('.')[0]
|
|
if top_level_module not in imported_modules:
|
|
imported_modules[top_level_module] = module
|
|
self.generic_visit(node)
|
|
|
|
def visit_ClassDef(self, node):
|
|
scope_stack.append(node.name)
|
|
imported_modules[node.name] = {}
|
|
self.generic_visit(node)
|
|
scope_stack.pop()
|
|
|
|
def visit_Attribute(self, node):
|
|
if isinstance(node.value, ast.Name):
|
|
id_lookup = node.value.id
|
|
current_scope = scope_stack[-1] if scope_stack else None
|
|
base_module = (imported_modules[current_scope].get(id_lookup)
|
|
if current_scope and id_lookup in imported_modules[current_scope]
|
|
else imported_modules.get(id_lookup))
|
|
if base_module:
|
|
api_call = f"{base_module}.{node.attr}"
|
|
if api_call not in api_list:
|
|
api_list.append(api_call)
|
|
self.generic_visit(node)
|
|
|
|
def visit_Name(self, node):
|
|
id_lookup = node.id
|
|
current_scope = scope_stack[-1] if scope_stack else None
|
|
base_module = (imported_modules[current_scope].get(id_lookup)
|
|
if current_scope and id_lookup in imported_modules[current_scope]
|
|
else imported_modules.get(id_lookup))
|
|
if base_module and base_module not in api_list:
|
|
api_list.append(base_module)
|
|
self.generic_visit(node)
|
|
|
|
def visit_Call(self, node):
|
|
function_name = None
|
|
if isinstance(node.func, ast.Name):
|
|
function_name = node.func.id
|
|
elif isinstance(node.func, ast.Attribute):
|
|
attrs = []
|
|
current = node.func
|
|
while isinstance(current, ast.Attribute):
|
|
attrs.append(current.attr)
|
|
current = current.value
|
|
if isinstance(current, ast.Name):
|
|
attrs.append(current.id)
|
|
attrs.reverse()
|
|
function_name = '.'.join(attrs)
|
|
|
|
if function_name:
|
|
current_scope = scope_stack[-1] if scope_stack else None
|
|
base_module = (imported_modules[current_scope].get(function_name.split('.')[0])
|
|
if current_scope and function_name.split('.')[0] in imported_modules[current_scope]
|
|
else imported_modules.get(function_name.split('.')[0]))
|
|
if base_module:
|
|
api_call = f"{base_module}{'.' + '.'.join(function_name.split('.')[1:]) if len(function_name.split('.')) > 1 else ''}"
|
|
if api_call not in api_list:
|
|
api_list.append(api_call)
|
|
|
|
|
|
for arg in node.args:
|
|
if isinstance(arg, ast.Name) and arg.id in imported_modules:
|
|
api_call = imported_modules[arg.id]
|
|
if api_call not in api_list:
|
|
api_list.append(api_call)
|
|
|
|
self.generic_visit(node)
|
|
|
|
ApiExtractor().visit(tree)
|
|
return list(set([api for api in api_list if "." in api]))
|
|
|
|
if __name__ == "__main__":
|
|
import json
|
|
from datasets import load_dataset
|
|
dataset = load_dataset("bigcode/bigcodebench-hard", split="v0.1.0_hf")
|
|
apis = []
|
|
for item in dataset:
|
|
complete_prompt = item["complete_prompt"]
|
|
canonical_solution = item["canonical_solution"]
|
|
apis.extend(extract_apis(complete_prompt+canonical_solution))
|
|
|
|
sorted_apis = sorted(apis, lambda x: x.split('.')[0])
|
|
with open("apis.txt", "w") as f:
|
|
for api in sorted_apis:
|
|
f.write(api + "\n") |