Skip to content

Commit 61c4aa2

Browse files
committed
Added Upgrade --flag
1 parent 724985f commit 61c4aa2

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

interpreter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import traceback
2222
import warnings
2323
from libs.markdown_code import display_markdown_message
24+
from libs.utility_manager import UtilityManager
2425

2526
# The main version of the interpreter.
2627
INTERPRETER_VERSION = "2.1"
@@ -35,6 +36,7 @@ def main():
3536
parser.add_argument('--lang', '-l', type=str, default='python', help='Set the interpreter language. (Defaults to Python)')
3637
parser.add_argument('--display_code', '-dc', action='store_true', default=False, help='Display the code in output')
3738
parser.add_argument('--history', '-hi', action='store_true', default=False, help='Use history as memory')
39+
parser.add_argument('--upgrade', '-up', action='store_true', default=False, help='Upgrade the interpreter')
3840
args = parser.parse_args()
3941

4042
# Check if only the application name is passed
@@ -44,6 +46,11 @@ def main():
4446

4547
warnings.filterwarnings("ignore") # To ignore all warnings
4648

49+
# Upgrade the interpreter if the --upgrade flag is passed.
50+
if args.upgrade:
51+
UtilityManager.upgrade_interpreter()
52+
return
53+
4754
# Create an instance of the Interpreter class and call the main method.
4855
interpreter = Interpreter(args)
4956
interpreter.interpreter_main(INTERPRETER_VERSION)

libs/interpreter_lib.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -515,29 +515,7 @@ def interpreter_main(self,version):
515515

516516
# UPGRAGE - Command section.
517517
elif task.lower() == '/upgrade':
518-
519-
# Download the requirements file
520-
requirements_file_url = 'https://raw.githubusercontent.com/haseeb-heaven/code-interpreter/main/requirements.txt'
521-
requirements_file_downloaded = self.utility_manager.download_file(requirements_file_url,'requirements.txt')
522-
523-
# Commands to execute.
524-
command_pip_upgrade = 'pip install open-code-interpreter --upgrade'
525-
command_pip_requirements = 'pip install -r requirements.txt --upgrade'
526-
527-
# Execute the commands.
528-
command_output,_ = self.code_interpreter.execute_command(command_pip_upgrade)
529-
display_markdown_message(f"Command Upgrade executed successfully.")
530-
if requirements_file_downloaded:
531-
command_output,_ = self.code_interpreter.execute_command(command_pip_requirements)
532-
display_markdown_message(f"Command Requirements executed successfully.")
533-
else:
534-
self.logger.warn(f"Requirements file not downloaded.")
535-
display_markdown_message(f"Warning: Requirements file not downloaded.")
536-
537-
if command_output:
538-
self.logger.info(f"Command executed successfully.")
539-
display_code(command_output)
540-
self.logger.info(f"Output: {command_output[:100]}")
518+
self.utility_manager.upgrade_interpreter()
541519
continue
542520

543521
# EXECUTE - Command section.

libs/utility_manager.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import platform
44
import re
55
import subprocess
6+
from libs.code_interpreter import CodeInterpreter
67
from libs.logger import Logger
78
import csv
89
import glob
910
from datetime import datetime
1011

11-
from libs.markdown_code import display_markdown_message
12+
from libs.markdown_code import display_code, display_markdown_message
1213

1314
class UtilityManager:
15+
logger = None
1416
def __init__(self):
1517
try:
1618
if not os.path.exists('logs'):
@@ -222,17 +224,46 @@ def clear_screen(self):
222224

223225
# method to download file from Web and save it
224226

225-
def download_file(self,url,file_name):
227+
@staticmethod
228+
def _download_file(url,file_name):
226229
try:
230+
logger = Logger.initialize_logger("logs/interpreter.log")
227231
import requests
228-
self.logger.info(f"Downloading file: {url}")
232+
logger.info(f"Downloading file: {url}")
229233
response = requests.get(url, allow_redirects=True)
230234
response.raise_for_status()
231235

232236
with open(file_name, 'wb') as file:
233237
file.write(response.content)
234-
self.logger.info(f"Reuquirements.txt file downloaded.")
238+
logger.info(f"Reuquirements.txt file downloaded.")
235239
return True
236240
except Exception as exception:
237-
self.logger.error(f"Error in downloading file: {str(exception)}")
241+
logger.error(f"Error in downloading file: {str(exception)}")
238242
return False
243+
244+
@staticmethod
245+
def upgrade_interpreter():
246+
code_interpreter = CodeInterpreter()
247+
logger = Logger.initialize_logger("logs/interpreter.log")
248+
# Download the requirements file
249+
requirements_file_url = 'https://raw.githubusercontent.com/haseeb-heaven/code-interpreter/main/requirements.txt'
250+
requirements_file_downloaded = UtilityManager._download_file(requirements_file_url,'requirements.txt')
251+
252+
# Commands to execute.
253+
command_pip_upgrade = 'pip install open-code-interpreter --upgrade'
254+
command_pip_requirements = 'pip install -r requirements.txt --upgrade'
255+
256+
# Execute the commands.
257+
command_output,_ = code_interpreter.execute_command(command_pip_upgrade)
258+
display_markdown_message(f"Command Upgrade executed successfully.")
259+
if requirements_file_downloaded:
260+
command_output,_ = code_interpreter.execute_command(command_pip_requirements)
261+
display_markdown_message(f"Command Requirements executed successfully.")
262+
else:
263+
logger.warn(f"Requirements file not downloaded.")
264+
display_markdown_message(f"Warning: Requirements file not downloaded.")
265+
266+
if command_output:
267+
logger.info(f"Command executed successfully.")
268+
display_code(command_output)
269+
logger.info(f"Output: {command_output[:100]}")

0 commit comments

Comments
 (0)