Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
332 lines (267 loc) · 11.5 KB
/
Copy pathmain.py
File metadata and controls
332 lines (267 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import os
import sys
import json
import ast
import javalang
from clang.cindex import Index, CursorKind, Config, AccessSpecifier
from antlr4 import *
from AutoDocX_tool.JavaLexer import JavaLexer
from AutoDocX_tool.JavaParser import JavaParser
from AutoDocX_tool.JavaParserListener import JavaParserListener
import argparse
import subprocess
Config.set_library_file("/usr/lib/llvm-18/lib/libclang.so.1")
def get_python_functions(filepath):
try:
with open(filepath, 'r', encoding='utf-8') as f:
source = f.read()
except FileNotFoundError:
print(f"Error: The file '{filepath}' was not found.")
source = None
except UnicodeDecodeError:
print(f"Error: Cannot decode '{filepath}' using UTF-8 encoding.")
source = None
except Exception as e:
print(f"An unexpected error occurred while reading the file: {e}")
source = None
tree = ast.parse(source)
results = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
class_name = None
for parent in ast.walk(tree):
if isinstance(parent, ast.ClassDef) and node in parent.body:
class_name = parent.name
break
start_line = node.lineno
end_line = node.end_lineno if hasattr(node, 'end_lineno') else start_line
docstring = ast.get_docstring(node) or ""
body = ast.get_source_segment(source, node)
results.append({
"function_name": node.name,
"parameters": [arg.arg for arg in node.args.args],
"return_type": None,
"class_name": class_name,
"belongs_to_class": class_name is not None,
"docstring_or_comment": docstring,
"body": body,
"access_specifier": "public",
"start_line": start_line,
"end_line": end_line,
"language": "Python"
})
return results
class FunctionExtractor(JavaParserListener):
def __init__(self, tokens):
self.tokens = tokens
self.functions = []
def enterMethodDeclaration(self, ctx: JavaParser.MethodDeclarationContext):
function_name = ctx.identifier().getText()
return_type = ctx.typeTypeOrVoid().getText()
params = ctx.formalParameters().getText()
# Get body lines
body_start = ctx.methodBody().start.line if ctx.methodBody() else ctx.start.line
body_end = ctx.methodBody().stop.line if ctx.methodBody() else ctx.stop.line
# Extract method body text using token range
if ctx.methodBody():
start_index = ctx.methodBody().start.tokenIndex
end_index = ctx.methodBody().stop.tokenIndex
method_body = ' '.join(token.text for token in self.tokens.tokens[start_index:end_index + 1])
else:
method_body = ""
# Traverse up the context tree to find modifiers
modifiers = []
parent = ctx.parentCtx
while parent:
if hasattr(parent, "modifier"):
modifiers = [m.getText() for m in parent.modifier()]
break
parent = parent.parentCtx
self.functions.append({
'function_name': function_name,
'return_type': return_type,
'parameters': params,
'start_line': ctx.start.line,
'end_line': ctx.stop.line,
'body_start': body_start,
'body_end': body_end,
'method_body': method_body,
'modifiers': modifiers if modifiers else ["package-private"]
})
from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker
from AutoDocX_tool.JavaLexer import JavaLexer
from AutoDocX_tool.JavaParser import JavaParser
def get_java_functions(filepath):
try:
with open(filepath, "r", encoding="utf-8") as f:
code = f.read()
except FileNotFoundError:
print(f"Error: The file '{filepath}' was not found.")
source = None
except UnicodeDecodeError:
print(f"Error: Cannot decode '{filepath}' using UTF-8 encoding.")
source = None
except Exception as e:
print(f"An unexpected error occurred while reading the file: {e}")
source = None
input_stream = InputStream(code)
lexer = JavaLexer(input_stream)
token_stream = CommonTokenStream(lexer)
parser = JavaParser(token_stream)
tree = parser.compilationUnit()
# Pass tokens to extractor for body reconstruction
extractor = FunctionExtractor(token_stream)
walker = ParseTreeWalker()
walker.walk(extractor, tree)
results = []
for func in extractor.functions:
results.append({
"function_name": func['function_name'],
"parameters": func['parameters'],
"return_type": func['return_type'],
"start_line": func['start_line'],
"end_line": func['end_line'],
"body_start": func['body_start'],
"body_end": func['body_end'],
"modifiers": func['modifiers'],
"body": func['method_body'],
"language": "Java"
})
return results
def get_cpp_functions(filepath):
index = Index.create()
try:
tu = index.parse(filepath, args=['-std=c++17'])
except FileNotFoundError:
print(f"Error: The file '{filepath}' was not found.")
source = None
except UnicodeDecodeError:
print(f"Error: Cannot decode '{filepath}' using UTF-8 encoding.")
source = None
except Exception as e:
print(f"An unexpected error occurred while reading the file: {e}")
source = None
results = []
def extract(cursor, class_name=None):
if (cursor.kind in [CursorKind.FUNCTION_DECL, CursorKind.CXX_METHOD]
and cursor.is_definition()
and cursor.location.file
and cursor.location.file.name == filepath):
func_name = cursor.spelling
params = [f"{arg.type.spelling} {arg.spelling}" for arg in cursor.get_arguments()]
return_type = cursor.result_type.spelling if cursor.result_type else None
access = str(cursor.access_specifier).split('.')[-1].lower() if cursor.access_specifier != AccessSpecifier.INVALID else "public"
start_line = cursor.extent.start.line
end_line = cursor.extent.end.line
try:
with open(filepath, 'r', encoding='utf-8') as f:
lines = f.readlines()
body = "".join(lines[start_line - 1:end_line])
except:
body = "<could not extract>"
results.append({
"function_name": func_name,
"parameters": params,
"return_type": return_type,
"class_name": class_name,
"belongs_to_class": class_name is not None,
"docstring_or_comment": "",
"body": body,
"access_specifier": access,
"start_line": start_line,
"end_line": end_line,
"language": "C++"
})
elif cursor.kind in [CursorKind.CLASS_DECL, CursorKind.STRUCT_DECL]:
class_name = cursor.spelling
for child in cursor.get_children():
extract(child, class_name)
else:
for child in cursor.get_children():
extract(child, class_name)
extract(tu.cursor)
return results
def parse_repo(source_folder):
print("HI")
data = []
print(f"Source folder: {source_folder}")
if not os.path.exists(source_folder):
print(f"[ERROR] Path does NOT exist: {source_folder}")
return []
if not os.path.isdir(source_folder):
print(f"[ERROR] Path exists but is NOT a directory: {source_folder}")
return []
print("[OK] Path exists and is a directory.")
for root, dirs, files in os.walk(source_folder):
for file in files:
full_path = os.path.join(root, file)
rel_folder = os.path.relpath(root, source_folder)
try:
if file.endswith(".py"):
funcs = get_python_functions(full_path)
elif file.endswith(".java"):
funcs = get_java_functions(full_path)
elif file.endswith((".cpp", ".cc", ".h", ".hpp")):
funcs = get_cpp_functions(full_path)
else:
continue
for func in funcs:
func.update({
"file_path": os.path.relpath(full_path, source_folder),
"folder": rel_folder
})
data.append(func)
except Exception as e:
print(f"[!] Error parsing {full_path}: {e}")
continue
return data
import time
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Extract functions from a code repository.")
parser.add_argument("--source_folder", type=str, required=True, help="Path to the source code repository")
args = parser.parse_args()
source_folder = args.source_folder
current_dir = os.path.dirname(os.path.abspath(__file__))
output_file = rf"{current_dir}\AutoDocX_tool\static\functions.json"
print(f"Scanning repository: {source_folder} ...")
# Step 1: Extract functions
result = parse_repo(source_folder)
with open(output_file, "w", encoding="utf-8") as f:
json.dump(result, f, indent=2)
print(f"Done! Extracted {len(result)} functions. Saved to {output_file}")
# Step 4: Start Docusaurus only after above steps are done
# docusaurus_dir = os.path.join(current_dir, "AutoDocX_tool")
run_bat = os.path.join(current_dir, "AutoDocX_tool", "run.bat")
command = [run_bat, source_folder]
result = subprocess.run(command, capture_output=True, text=True, shell=True, cwd=os.path.dirname(run_bat))
print("Output:")
print(result.stdout)
print("Errors:")
print(result.stderr)
# print("🚀 Launching Docusaurus dev server...")
# # Non-blocking launch
# subprocess.Popen([docusaurus_executable, "start"], cwd=docusaurus_dir, shell=True)
# print("✅ Docusaurus is starting in the background. Visit http://localhost:3000")
# # Step 2: Run first JS script
# command1 = ["node", f"{current_dir}\\AutoDocX_tool\\generateLLMdocsnew.js"]
# print("Running command:", ' '.join(command1))
# result = subprocess.run(command1, input=source_folder, capture_output=True, text=True, encoding='utf-8')
# print("Output from JS:")
# print(result.stdout)
# print("Errors:")
# print(result.stderr)
# # Step 3: Run second JS script
# command2 = ["node", f"{current_dir}\\AutoDocX_tool\\scripts\\generateDocs.js"]
# print("Running command:", ' '.join(command2))
# result = subprocess.run(command2, capture_output=True, text=True)
# print("Output from JS:")
# print(result.stdout)
# print("Errors:")
# print(result.stderr)
# Step 4: Start Docusaurus only after above steps are done
# docusaurus_dir = os.path.join(current_dir, "AutoDocX_tool")
# docusaurus_executable = os.path.join(docusaurus_dir, "node_modules", ".bin", "docusaurus.cmd")
# print("🚀 Launching Docusaurus dev server...")
# # Non-blocking launch
# subprocess.Popen([docusaurus_executable, "start"], cwd=docusaurus_dir, shell=True)
# print("✅ Docusaurus is starting in the background. Visit http://localhost:3000")