-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.py
More file actions
186 lines (144 loc) · 5.65 KB
/
cli.py
File metadata and controls
186 lines (144 loc) · 5.65 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
import glob
import json
import logging
from dotenv import load_dotenv
load_dotenv()
import os
from dataclasses import dataclass
from typing import Dict, List
from src import get_settings
from src.services.coding_service import CodingService
from src.services.repository_reader_service import RepositoryReaderService
logger = logging.getLogger(__name__)
def read_included_files(local_repo_path: str) -> Dict[str, str]:
# Ask the user if he would like to provide glob pattern or specific files
response = input(
# "Do you want to include only specific files? "
# "If yes, please choose a way to include files: "
# "1. Glob pattern\n"
# "2. Specific files\n"
f"""Do you want to include only specific files?
If yes, please choose a way to include files:
0. Glob pattern
1. Specific files
2. Read all files
"""
)
if response == "0":
include_files_glob = input(
"Please provide a glob pattern to include files: "
)
glob_pattern = os.path.join(local_repo_path, include_files_glob)
print(f"Using glob pattern: {glob_pattern}")
include_files = glob.glob(glob_pattern, recursive=True)
include_files = list(set([os.path.basename(file) for file in include_files]))
elif response == "1":
include_files = input(
"Do you want to include only specific files? "
"If yes, write the file names separated by commas. If no, press enter.\n"
"files: "
)
include_files = include_files.split(",")
else:
include_files = None
contents = RepositoryReaderService().read_files(
directory=local_repo_path,
include_files=include_files
)
print(f"Read {len(contents)} files from {local_repo_path}")
return contents
def run_code_writing_session(local_repo_path: str):
contents = read_included_files(local_repo_path=local_repo_path)
task = input("Please give me a task: ")
coding_service = CodingService()
coding_service.learn_code(file_abs_path_to_content=contents)
code_feature_files = coding_service.code_feature(
task=task, local_repo_path=local_repo_path
)
coding_service.write_code(
coded_files=code_feature_files, local_repo_path=local_repo_path
)
print(f"Code written to {local_repo_path} successfully.")
def code_review_file(
file_path: str,
content: str,
local_repo_path: str,
):
"""
This function reads the content of a file and its dependencies, learns the code, and finds issues in the code.
:param file_path: The path to the file to be reviewed.
:param content: The content of the file to be reviewed.
:param local_repo_path: The path to the local repository.
"""
coding_service = CodingService()
file_path_to_content = {file_path: content}
file_dependencies = coding_service.repo_reader_service.find_dependencies_by_file(
file_path=file_path,
local_repo_path=local_repo_path
)
for dep_idx, file_dependency_path in enumerate(file_dependencies):
if not os.path.isfile(file_dependency_path):
logger.warning(f"Dependency file {file_dependency_path} does not exist, skipping.")
continue
logger.info(
f"Reading dependency {dep_idx + 1}/{len(file_dependencies)} for the file {file_path}: {file_dependency_path}")
with open(file_dependency_path, "r") as file:
file_dependency_content = file.read()
file_path_to_content[file_dependency_path] = file_dependency_content
coding_service.learn_code(file_abs_path_to_content=file_path_to_content)
issues = coding_service.perform_code_review()
return issues
def run_code_review_session(local_repo_path: str):
contents = read_included_files(local_repo_path=local_repo_path)
bugs_output_dir = os.path.join(local_repo_path, "ohad_bugs")
for idx, (file_path, content) in enumerate(contents.items()):
print(f"Searching for issues in file {idx + 1}/{len(contents)}: {file_path}")
issues = code_review_file(
file_path=file_path,
content=content,
local_repo_path=local_repo_path,
)
if not issues:
print(f"No issues found in {file_path}, skipping.")
continue
os.makedirs(bugs_output_dir, exist_ok=True)
output_file_path = os.path.join(bugs_output_dir, f"{os.path.basename(file_path)}.issues.json")
with open(output_file_path, "w") as f:
print(f"Writing issues to {output_file_path}")
f.write(json.dumps(issues, indent=4))
@dataclass
class MenuOption:
option: str
func: callable
def main():
settings = get_settings()
local_repo_path = settings.repo_path
menu_options: List[MenuOption] = [
MenuOption(
option="Code Writing",
func=lambda: run_code_writing_session(
local_repo_path=local_repo_path
)
),
MenuOption(
option="Bug Finder",
func=lambda: run_code_review_session(
local_repo_path=local_repo_path
)
),
]
while True:
print("Please choose an option:")
for idx, option in enumerate(menu_options):
print(f"{idx}. {option.option}")
choice = input("Choose an option: ")
choice = int(choice)
menu_options[choice].func()
continue_execution = input("Do you want to do something else? (yes/no) ")
if continue_execution.lower() in ["no", "n"]:
break
else:
print("Great! Let's do something else.")
return
if __name__ == "__main__":
main()