PipInterface/Pip.py

branch
pypi
changeset 6792
9dd854f05c83
parent 6785
058d63c537a4
child 6793
cca6a35f3ad2
equal deleted inserted replaced
6785:058d63c537a4 6792:9dd854f05c83
13 except NameError: 13 except NameError:
14 pass 14 pass
15 15
16 import os 16 import os
17 import sys 17 import sys
18 import json
18 19
19 from PyQt5.QtCore import pyqtSlot, QObject, QProcess 20 from PyQt5.QtCore import pyqtSlot, QObject, QProcess
20 from PyQt5.QtWidgets import QMenu, QInputDialog, QDialog 21 from PyQt5.QtWidgets import QMenu, QInputDialog, QDialog
21 22
22 from E5Gui import E5MessageBox 23 from E5Gui import E5MessageBox
43 @param parent parent 44 @param parent parent
44 @type QObject 45 @type QObject
45 """ 46 """
46 super(Pip, self).__init__(parent) 47 super(Pip, self).__init__(parent)
47 48
48 self.__virtualenvManager = e5App().getObject("VirtualEnvManager") 49 ## self.__virtualenvManager = e5App().getObject("VirtualEnvManager")
49 ## self.__project = e5App().getObject("Project") 50 ## self.__project = e5App().getObject("Project")
50 51
51 self.__menus = {} # dictionary with references to menus 52 self.__menus = {} # dictionary with references to menus
52 53
53 ## self.__plugin.currentEnvironmentChanged.connect( 54 ## self.__plugin.currentEnvironmentChanged.connect(
543 except KeyError: 544 except KeyError:
544 venvName = Preferences.getPip("CurrentEnvironment") 545 venvName = Preferences.getPip("CurrentEnvironment")
545 if not venvName: 546 if not venvName:
546 self.__selectPipVirtualenv() 547 self.__selectPipVirtualenv()
547 venvName = Preferences.getPip("CurrentEnvironment") 548 venvName = Preferences.getPip("CurrentEnvironment")
548 if self.__virtualenvManager.isGlobalEnvironment(venvName): 549 venvManager = e5App().getObject("VirtualEnvManager")
550 if venvManager.isGlobalEnvironment(venvName):
549 venvDirectory = self.__getUserConfig() 551 venvDirectory = self.__getUserConfig()
550 else: 552 else:
551 venvDirectory = \ 553 venvDirectory = venvManager.getVirtualenvDirectory(venvName)
552 self.__virtualenvManager.getVirtualenvDirectory(venvName)
553 554
554 if venvDirectory: 555 if venvDirectory:
555 config = os.path.join(venvDirectory, pip) 556 config = os.path.join(venvDirectory, pip)
556 else: 557 else:
557 config = "" 558 config = ""
589 @rtype str 590 @rtype str
590 """ 591 """
591 if venvName == self.getDefaultEnvironmentString(): 592 if venvName == self.getDefaultEnvironmentString():
592 venvName = Preferences.getPip("CurrentEnvironment") 593 venvName = Preferences.getPip("CurrentEnvironment")
593 elif venvName == self.getProjectEnvironmentString(): 594 elif venvName == self.getProjectEnvironmentString():
594 venvName = e5App().getObject("Project").getDebugProperty("VIRTUALENV") 595 venvName = \
596 e5App().getObject("Project").getDebugProperty("VIRTUALENV")
595 if not venvName: 597 if not venvName:
596 # fall back to standard if not defined 598 # fall back to standard if not defined
597 venvName = Preferences.getPip("CurrentEnvironment") 599 venvName = Preferences.getPip("CurrentEnvironment")
598 600
599 interpreter = self.__virtualenvManager.getVirtualenvInterpreter( 601 interpreter = \
600 venvName) 602 e5App().getObject("VirtualEnvManager").getVirtualenvInterpreter(
603 venvName)
601 if not interpreter: 604 if not interpreter:
602 E5MessageBox.critical( 605 E5MessageBox.critical(
603 None, 606 None,
604 self.tr("Interpreter for Virtual Environment"), 607 self.tr("Interpreter for Virtual Environment"),
605 self.tr("""No interpreter configured for the selected""" 608 self.tr("""No interpreter configured for the selected"""
612 Public method to get a sorted list of virtual environment names. 615 Public method to get a sorted list of virtual environment names.
613 616
614 @return sorted list of virtual environment names 617 @return sorted list of virtual environment names
615 @rtype list of str 618 @rtype list of str
616 """ 619 """
617 return sorted(self.__virtualenvManager.getVirtualenvNames()) 620 return sorted(
621 e5App().getObject("VirtualEnvManager").getVirtualenvNames())
618 622
619 ########################################################################## 623 ##########################################################################
620 ## Methods below implement the individual menu entries 624 ## Methods below implement the individual menu entries
621 ########################################################################## 625 ##########################################################################
622 626
1128 indexUrl = DefaultIndexUrlXml 1132 indexUrl = DefaultIndexUrlXml
1129 1133
1130 self.__searchDialog = PipSearchDialog(self, indexUrl) 1134 self.__searchDialog = PipSearchDialog(self, indexUrl)
1131 self.__searchDialog.show() 1135 self.__searchDialog.show()
1132 1136
1137 def getInstalledPackages(self, envName, localPackages=True,
1138 notRequired=False, usersite=False):
1139 """
1140 Public method to get the list of installed packages.
1141
1142 @param envName name of the environment to get the packages for
1143 @type str
1144 @param localPackages flag indicating to get local packages only
1145 @type bool
1146 @param notRequired flag indicating to list packages that are not
1147 dependencies of installed packages as well
1148 @type bool
1149 @param usersite flag indicating to only list packages installed
1150 in user-site
1151 @type bool
1152 @return list of tuples containing the package name and version
1153 @rtype list of tuple of (str, str)
1154 """
1155 packages = []
1156
1157 if envName:
1158 interpreter = self.getVirtualenvInterpreter(envName)
1159 if interpreter:
1160 args = [
1161 "-m", "pip",
1162 "list",
1163 "--format=json",
1164 ]
1165 if localPackages:
1166 args.append("--local")
1167 if notRequired:
1168 args.append("--not-required")
1169 if usersite:
1170 args.append("--user")
1171
1172 proc = QProcess()
1173 proc.start(interpreter, args)
1174 if proc.waitForStarted(15000):
1175 if proc.waitForFinished(30000):
1176 output = str(proc.readAllStandardOutput(),
1177 Preferences.getSystem("IOEncoding"),
1178 'replace').strip()
1179 try:
1180 jsonList = json.loads(output)
1181 except Exception:
1182 jsonList = []
1183
1184 for package in jsonList:
1185 if isinstance(package, dict):
1186 packages.append((
1187 package["name"],
1188 package["version"],
1189 ))
1190
1191 return packages
1192
1193 def getOutdatedPackages(self, envName, localPackages=True,
1194 notRequired=False, usersite=False):
1195 """
1196 Public method to get the list of outdated packages.
1197
1198 @param envName name of the environment to get the packages for
1199 @type str
1200 @param localPackages flag indicating to get local packages only
1201 @type bool
1202 @param notRequired flag indicating to list packages that are not
1203 dependencies of installed packages as well
1204 @type bool
1205 @param usersite flag indicating to only list packages installed
1206 in user-site
1207 @type bool
1208 @return list of tuples containing the package name, installed version
1209 and available version
1210 @rtype list of tuple of (str, str, str)
1211 """
1212 packages = []
1213
1214 if envName:
1215 interpreter = self.getVirtualenvInterpreter(envName)
1216 if interpreter:
1217 args = [
1218 "-m", "pip",
1219 "list",
1220 "--outdated",
1221 "--format=json",
1222 ]
1223 if localPackages:
1224 args.append("--local")
1225 if notRequired:
1226 args.append("--not-required")
1227 if usersite:
1228 args.append("--user")
1229
1230 proc = QProcess()
1231 proc.start(interpreter, args)
1232 if proc.waitForStarted(15000):
1233 if proc.waitForFinished(30000):
1234 output = str(proc.readAllStandardOutput(),
1235 Preferences.getSystem("IOEncoding"),
1236 'replace').strip()
1237 try:
1238 jsonList = json.loads(output)
1239 except Exception:
1240 jsonList = []
1241
1242 for package in jsonList:
1243 if isinstance(package, dict):
1244 packages.append((
1245 package["name"],
1246 package["version"],
1247 package["latest_version"],
1248 ))
1249
1250 return packages
1251
1133 def __pipConfigure(self): 1252 def __pipConfigure(self):
1134 """ 1253 """
1135 Private slot to open the configuration page. 1254 Private slot to open the configuration page.
1136 """ 1255 """
1137 e5App().getObject("UserInterface").showPreferences("pipPage") 1256 e5App().getObject("UserInterface").showPreferences("pipPage")

eric ide

mercurial