Last active
November 26, 2024 12:43
-
-
Save portal7/4cad498e142993147ee6b7938c41c05a to your computer and use it in GitHub Desktop.
enumerate functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nuevo | |
import os | |
import re | |
import pandas as pd | |
def analyze_cs_files(directory): | |
results = [] | |
# Regex patterns | |
controller_pattern = r"public class (\w+Controller)" | |
function_pattern = r"\[Function\(nameof\((\w+)\)\)\]" | |
openapi_summary_pattern = r"\[OpenApiOperation\(.*?Summary\s*=\s*\"([^\"]+)\"" | |
roles_pattern = r"\[PortalAuthorizationData\(.*?Roles\s*=\s*\"([^\"]+)\"" | |
route_pattern = r"\[HttpTrigger\(.*?,.*?,\s*Route\s*=\s*\"([^\"]+)\"" | |
method_pattern = r"\[HttpTrigger\((?:.*?,\s*)?AuthorizationLevel\.\w+\s*,\s*\"(\w+)\"" | |
for root, _, files in os.walk(directory): | |
for file in files: | |
if file.endswith(".cs"): | |
file_path = os.path.join(root, file) | |
with open(file_path, "r", encoding="utf-8") as f: | |
lines = f.readlines() | |
controller_name = None | |
for i, line in enumerate(lines): | |
# Get controller name | |
if not controller_name: | |
match = re.search(controller_pattern, line) | |
if match: | |
controller_name = match.group(1) | |
# Get function attributes | |
function_match = re.search(function_pattern, line) | |
if function_match: | |
function_name = function_match.group(1) | |
summary = None | |
roles = None | |
route = None | |
method_type = None | |
# Check previous lines for attributes | |
for j in range(i - 10, i): # Check 10 lines before the function definition | |
if j >= 0: | |
openapi_match = re.search(openapi_summary_pattern, lines[j]) | |
if openapi_match: | |
summary = openapi_match.group(1) | |
roles_match = re.search(roles_pattern, lines[j]) | |
if roles_match: | |
roles = roles_match.group(1) | |
route_match = re.search(route_pattern, lines[j]) | |
if route_match: | |
route = route_match.group(1) | |
method_match = re.search(method_pattern, lines[j]) | |
if method_match: | |
method_type = method_match.group(1) | |
results.append({ | |
"Filename": file_path, | |
"ControllerName": controller_name, | |
"FunctionName": function_name, | |
"Summary": summary, | |
"LineNumber": i + 1, | |
"Roles": roles, | |
"Route": route, | |
"MethodType": method_type | |
}) | |
return results | |
def export_to_csv(results, output_file): | |
df = pd.DataFrame(results) | |
df.to_csv(output_file, index=False) | |
print(f"Results exported to CSV: {output_file}") | |
def export_to_excel(results, output_file): | |
df = pd.DataFrame(results) | |
df.to_excel(output_file, index=False, engine='openpyxl') | |
print(f"Results exported to Excel: {output_file}") | |
def print_results(results): | |
print("Results:") | |
for result in results: | |
print("Filename:", result["Filename"]) | |
print("ControllerName:", result["ControllerName"]) | |
print("FunctionName:", result["FunctionName"]) | |
print("Summary:", result["Summary"]) | |
print("LineNumber:", result["LineNumber"]) | |
print("Roles:", result["Roles"]) | |
print("Route:", result["Route"]) | |
print("MethodType:", result["MethodType"]) | |
print("-" * 50) | |
if __name__ == "__main__": | |
directory_to_analyze = input("Enter the directory to analyze: ").strip() | |
output_format = input("Enter output format (csv/excel/none): ").strip().lower() | |
output_file = input("Enter output file name (e.g., output.csv or output.xlsx): ").strip() | |
results = analyze_cs_files(directory_to_analyze) | |
if output_format == "csv": | |
export_to_csv(results, output_file) | |
elif output_format == "excel": | |
export_to_excel(results, output_file) | |
else: | |
print_results(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment