3
3
import logging
4
4
import requests
5
5
import stdlib_list
6
+ from libs .logger import Logger
7
+
6
8
7
9
class PackageManager :
10
+ logger = None
11
+
8
12
def __init__ (self ):
9
13
self .pip_command = "pip"
10
14
self .pip3_command = "pip3"
11
15
self .npm_command = "npm"
12
- self .logger = logging .getLogger (__name__ )
13
- handler = logging .StreamHandler ()
14
- formatter = logging .Formatter ('%(asctime)s [%(levelname)s] [%(name)s:%(lineno)d] [%(funcName)s] - %(message)s' )
15
- handler .setFormatter (formatter )
16
- self .logger .addHandler (handler )
17
- self .logger .setLevel (logging .INFO )
16
+ self .logger = Logger .initialize ("logs/interpreter.log" )
18
17
19
18
def install_package (self , package_name , language ):
20
19
if language == "python" :
@@ -23,7 +22,7 @@ def install_package(self, package_name, language):
23
22
self .logger .error (exception )
24
23
raise exception
25
24
26
- if not self ._is_package_installed (package_name ,"pip" ):
25
+ if not self ._is_package_installed (package_name , "pip" ):
27
26
try :
28
27
# Try to install the package using pip
29
28
self ._install_package_with_pip (package_name )
@@ -58,7 +57,7 @@ def install_package(self, package_name, language):
58
57
self .logger .error (exception )
59
58
raise exception
60
59
61
- def extract_package_name (self ,error ,language ):
60
+ def extract_package_name (self , error ,language ):
62
61
if language == "python" :
63
62
return self ._extract_python_package_name (error )
64
63
elif language == "javascript" :
@@ -76,31 +75,31 @@ def get_system_modules(self):
76
75
except Exception as exception :
77
76
raise ValueError ("An error occurred while getting module names" ) from exception
78
77
79
- def _install_package_with_pip (self , package_name ):
78
+ def _install_package_with_pip (self , package_name ):
80
79
try :
81
80
subprocess .check_call ([self .pip_command , "install" , package_name ])
82
81
self .logger .info (f"Successfully installed package with pip: { package_name } " )
83
82
except subprocess .CalledProcessError as exception :
84
83
self .logger .error (f"Failed to install package with pip: { package_name } " )
85
84
raise exception
86
85
87
- def _install_package_with_pip3 (self , package_name ):
86
+ def _install_package_with_pip3 (self , package_name ):
88
87
try :
89
88
subprocess .check_call ([self .pip3_command , "install" , package_name ])
90
89
self .logger .info (f"Successfully installed package with pip3: { package_name } " )
91
90
except subprocess .CalledProcessError as exception :
92
91
self .logger .error (f"Failed to install package with pip3: { package_name } " )
93
92
raise exception
94
93
95
- def _install_package_with_npm (self , package_name ):
94
+ def _install_package_with_npm (self , package_name ):
96
95
try :
97
96
subprocess .check_call ([self .npm_command , "install" , package_name ])
98
97
self .logger .info (f"Successfully installed package with npm: { package_name } " )
99
98
except subprocess .CalledProcessError as exception :
100
99
self .logger .error (f"Failed to install package with npm: { package_name } " )
101
100
raise exception
102
101
103
- def _is_package_installed (self , package_name , package_manager ):
102
+ def _is_package_installed (self , package_name , package_manager ):
104
103
if package_manager == "pip" :
105
104
try :
106
105
subprocess .check_call ([self .pip_command , "show" , package_name ])
@@ -126,17 +125,8 @@ def _is_package_installed(self, package_name, package_manager):
126
125
exception = ValueError ("Invalid package manager selected." )
127
126
self .logger .error (exception )
128
127
raise exception
129
-
130
- def _install_package_with_npm (self , package_name ):
131
- try :
132
- subprocess .check_call ([self .npm_command , "install" , package_name ])
133
- self .logger .info (f"Successfully installed package with npm: { package_name } " )
134
- except subprocess .CalledProcessError as exception :
135
- self .logger .error (f"Failed to install package with npm: { package_name } " )
136
- raise exception
137
-
138
-
139
- def _extract_python_package_name (self , error_message ):
128
+
129
+ def _extract_python_package_name (self , error_message ):
140
130
# Regular expression pattern to match the error message
141
131
pattern = r"ModuleNotFoundError: No module named '(\w+)'|ModuleNotFoundError: '(\w+)'"
142
132
match = re .search (pattern , error_message )
@@ -150,7 +140,7 @@ def _extract_python_package_name(self, error_message):
150
140
self .logger .error (exception )
151
141
raise exception
152
142
153
- def _extract_javascript_package_name (self ,error_message ):
143
+ def _extract_javascript_package_name (self , error_message ):
154
144
try :
155
145
lines = error_message .split ('\n ' )
156
146
for line in lines :
@@ -162,32 +152,22 @@ def _extract_javascript_package_name(self,error_message):
162
152
self .logger .error (f"Failed to extract package name from error message: { exception } " )
163
153
raise exception
164
154
165
- def _check_package_exists_pip (self ,package_name ):
166
- import requests
167
- from bs4 import BeautifulSoup
168
-
155
+ def _check_package_exists_pip (self , package_name ):
169
156
try :
170
- # Send a GET request to the PyPI search page
171
- response = requests .get (f"https://pypi.org/search/?q={ package_name } " )
172
- response .raise_for_status ()
173
-
174
- # Parse the HTML content of the page
175
- soup = BeautifulSoup (response .text , 'html.parser' )
176
-
177
- # Search for the package name in the parsed HTML
178
- search_results = soup .find_all ('span' , class_ = 'package-snippet__name' )
179
- for result in search_results :
180
- if result .text .strip () == package_name :
181
- return True
182
-
183
- # If the package name was not found in the search results, log an error and raise an exception
184
- exception = ValueError (f"Package { package_name } does not exist on PyPI" )
185
- self .logger .error (exception )
186
- raise exception
187
- except requests .exceptions .RequestException as exception :
188
- raise exception
157
+ api_url = f"https://pypi.org/pypi/{ package_name } /json"
158
+ self .logger .info ("API Url is {}" .format (api_url ))
159
+ response = requests .get (api_url )
160
+ if response .status_code == 200 :
161
+ return True
162
+ else :
163
+ error_message = f"Package { package_name } does not exist on PyPI"
164
+ self .logger .error (error_message )
165
+ raise ValueError (error_message )
166
+ except requests .exceptions .RequestException as request_exception :
167
+ self .logger .error (f"Request failed: { request_exception } " )
168
+ raise request_exception
189
169
190
- def _check_package_exists_npm (self , package_name ):
170
+ def _check_package_exists_npm (self , package_name ):
191
171
try :
192
172
response = requests .get (f"https://www.npmjs.com/package/{ package_name } " )
193
173
if response .status_code == 200 :
0 commit comments