Skip to content

Commit

Permalink
Add check to skip installing system modules
Browse files Browse the repository at this point in the history
  • Loading branch information
haseeb-heaven committed Feb 27, 2024
1 parent 4042027 commit fb11f3b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
20 changes: 20 additions & 0 deletions libs/interpreter_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,16 @@ def interpreter_main(self):
elif any(command in task.lower() for command in ['/install']):
# get the package name after the command
package_name = task.split(' ')[1]

# check if package name is not system module.
system_modules = self.package_manager.get_system_modules()

# Skip installing system modules.
if package_name in system_modules:
self.logger.info(f"Package {package_name} is a system module.")
display_markdown_message(f"Package {package_name} is a system module.")
raise Exception(f"Package {package_name} is a system module.")

if package_name:
self.logger.info(f"Installing package {package_name} on interpreter {self.INTERPRETER_LANGUAGE}")
self.package_manager.install_package(package_name, self.INTERPRETER_LANGUAGE)
Expand Down Expand Up @@ -794,6 +804,16 @@ def interpreter_main(self):
error_messages = ["ModuleNotFound", "ImportError", "No module named", "Cannot find module"]
if code_error is not None and any(error_message in code_error for error_message in error_messages):
package_name = self.package_manager.extract_package_name(code_error, self.INTERPRETER_LANGUAGE)

# check if package name is not system module.
system_modules = self.package_manager.get_system_modules()

# Skip installing system modules.
if package_name in system_modules:
self.logger.info(f"Package {package_name} is a system module.")
display_markdown_message(f"Package {package_name} is a system module.")
raise Exception(f"Package {package_name} is a system module.")

if package_name:
self.logger.info(f"Installing package {package_name} on interpreter {self.INTERPRETER_LANGUAGE}")
self.package_manager.install_package(package_name, self.INTERPRETER_LANGUAGE)
Expand Down
10 changes: 9 additions & 1 deletion libs/package_manager.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import subprocess
import re
import logging

import requests
import stdlib_list

class PackageManager:
def __init__(self):
Expand Down Expand Up @@ -68,6 +68,14 @@ def extract_package_name(self,error,language):
self.logger.error(exception)
raise exception

def get_system_modules(self):
try:
# Get a list of all module names in the standard library
stdlib = stdlib_list.stdlib_list()
return stdlib
except Exception as exception:
raise ValueError("An error occurred while getting module names") from exception

def _install_package_with_pip(self, package_name):
try:
subprocess.check_call([self.pip_command, "install", package_name])
Expand Down
5 changes: 4 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ rich
# Libraries for data analysis
pandas
matplotlib
plotly
plotly

# Libraries for standard libries
stdlib_list

0 comments on commit fb11f3b

Please sign in to comment.