ProjectDjango/Project.py

changeset 132
576cb5a3b035
parent 125
d280acf98fb5
child 142
ecadd7fd0963
equal deleted inserted replaced
131:e17a33dfc6e9 132:576cb5a3b035
109 super(Project, self).__init__(parent) 109 super(Project, self).__init__(parent)
110 110
111 self.__plugin = plugin 111 self.__plugin = plugin
112 self.__ui = parent 112 self.__ui = parent
113 self.__e5project = e5App().getObject("Project") 113 self.__e5project = e5App().getObject("Project")
114 try:
115 self.__virtualEnvManager = e5App().getObject("VirtualEnvManager")
116 except KeyError:
117 self.__virtualEnvManager = None
114 self.__hooksInstalled = False 118 self.__hooksInstalled = False
115 119
116 self.__menus = {} # dictionary with references to menus 120 self.__menus = {} # dictionary with references to menus
117 121
118 self.__serverProc = None 122 self.__serverProc = None
1234 self.__serverProcFinished() 1238 self.__serverProcFinished()
1235 self.__setCurrentSite(None) 1239 self.__setCurrentSite(None)
1236 1240
1237 def __getExecutablePaths(self, file): 1241 def __getExecutablePaths(self, file):
1238 """ 1242 """
1239 Private method to build all full path of an executable file from 1243 Private method to build all full paths of an executable file from
1240 the environment. 1244 the environment.
1241 1245
1242 @param file filename of the executable (string) 1246 @param file filename of the executable (string)
1243 @return list of full executable names, if the executable file is 1247 @return list of full executable names, if the executable file is
1244 accessible via the searchpath defined by the PATH environment 1248 accessible via the searchpath defined by the PATH environment
1285 cmd = self.__getDjangoAdminCommand(variant) 1289 cmd = self.__getDjangoAdminCommand(variant)
1286 if isWindowsPlatform(): 1290 if isWindowsPlatform():
1287 if cmd: 1291 if cmd:
1288 variants.append(variant) 1292 variants.append(variant)
1289 else: 1293 else:
1290 try: 1294 if cmd:
1291 fullCmds = Utilities.getExecutablePaths(cmd)
1292 except AttributeError:
1293 fullCmds = self.__getExecutablePaths(cmd)
1294 for fullCmd in fullCmds:
1295 try: 1295 try:
1296 f = open(fullCmd, 'r', encoding='utf-8') 1296 fullCmds = Utilities.getExecutablePaths(cmd)
1297 l0 = f.readline() 1297 except AttributeError:
1298 f.close() 1298 fullCmds = self.__getExecutablePaths(cmd)
1299 except (IOError, OSError): 1299 for fullCmd in fullCmds:
1300 l0 = "" 1300 try:
1301 if variant.lower() in l0.lower() or \ 1301 f = open(fullCmd, 'r', encoding='utf-8')
1302 "{0}.".format(variant[-1]) in l0 or \ 1302 l0 = f.readline()
1303 (variant == "Python2" and 1303 f.close()
1304 "python3" not in l0.lower() and 1304 except (IOError, OSError):
1305 "python" in l0.lower()): 1305 l0 = ""
1306 variants.append(variant) 1306 if self.__isSuitableForVariant(variant, l0):
1307 break 1307 variants.append(variant)
1308 break
1308 1309
1309 return variants 1310 return variants
1311
1312 def __isSuitableForVariant(self, variant, line0):
1313 """
1314 Private method to test, if a detected command file is suitable for the
1315 given Python variant.
1316
1317 @param variant Python variant to test for
1318 @type str (one of Python2 or Python3)
1319 @param line0 first line of the executable
1320 @type str
1321 """
1322 assert variant in ("Python2", "Python3")
1323
1324 l0 = line0.lower()
1325 ok = (variant.lower() in l0 or
1326 "{0}.".format(variant[-1]) in l0)
1327 if variant == "Python2":
1328 ok |= "python3" not in l0 and "python" in l0
1329 ok |= "pypy2" in l0
1330 ok |= "pypy3" not in l0 and "pypy" in l0
1331 else:
1332 ok |= "pypy3" in l0
1333
1334 return ok
1310 1335
1311 def __getVirtualEnvironment(self, language=""): 1336 def __getVirtualEnvironment(self, language=""):
1312 """ 1337 """
1313 Private method to get the path of the virtual environment. 1338 Private method to get the path of the virtual environment.
1314 1339
1316 for (string, one of '', 'Python2' or 'Python3') 1341 for (string, one of '', 'Python2' or 'Python3')
1317 @return path of the virtual environment (string) 1342 @return path of the virtual environment (string)
1318 """ 1343 """
1319 if not language: 1344 if not language:
1320 language = self.__e5project.getProjectLanguage() 1345 language = self.__e5project.getProjectLanguage()
1321 if language == "Python3": 1346 if self.__virtualEnvManager:
1322 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3") 1347 if language == "Python3":
1323 elif language == "Python2": 1348 venvName = self.__plugin.getPreferences(
1324 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2") 1349 "VirtualEnvironmentNamePy3")
1350 elif language == "Python2":
1351 venvName = self.__plugin.getPreferences(
1352 "VirtualEnvironmentNamePy2")
1353 else:
1354 venvName = ""
1355 if venvName:
1356 virtEnv = self.__virtualEnvManager.getVirtualenvDirectory(
1357 venvName)
1358 if not virtEnv:
1359 virtEnv = os.path.dirname(
1360 self.__virtualEnvManager.getVirtualenvInterpreter(
1361 venvName))
1362 if virtEnv.endswith(("Scripts", "bin")):
1363 virtEnv = os.path.dirname(virtEnv)
1364 else:
1365 virtEnv = ""
1325 else: 1366 else:
1326 virtEnv = "" 1367 # backward compatibility
1368 if language == "Python3":
1369 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3")
1370 elif language == "Python2":
1371 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2")
1372 else:
1373 virtEnv = ""
1327 if virtEnv and not os.path.exists(virtEnv): 1374 if virtEnv and not os.path.exists(virtEnv):
1328 virtEnv = "" 1375 virtEnv = ""
1329 return virtEnv 1376 return virtEnv
1330 1377
1331 def __getDebugEnvironment(self, language=""): 1378 def __getDebugEnvironment(self, language=""):
1336 for (string, one of '', 'Python2' or 'Python3') 1383 for (string, one of '', 'Python2' or 'Python3')
1337 @return path of the debugger environment (string) 1384 @return path of the debugger environment (string)
1338 """ 1385 """
1339 if not language: 1386 if not language:
1340 language = self.__e5project.getProjectLanguage() 1387 language = self.__e5project.getProjectLanguage()
1341 if language == "Python3": 1388 if self.__virtualEnvManager:
1342 debugEnv = Preferences.getDebugger("Python3Interpreter") 1389 debugEnv = self.__getVirtualEnvironment(language)
1343 if not debugEnv and sys.version_info[0] == 3: 1390 if not debugEnv:
1391 if language == "Python3":
1392 venvName = Preferences.getDebugger("Python3VirtualEnv")
1393 elif language == "Python2":
1394 venvName = Preferences.getDebugger("Python2VirtualEnv")
1395 else:
1396 venvName = ""
1397
1398 if venvName:
1399 debugEnv = self.__virtualEnvManager.getVirtualenvDirectory(
1400 venvName)
1401 else:
1402 debugEnv = ""
1403 else:
1404 # backward compatibility
1405 if language == "Python3":
1406 debugEnv = Preferences.getDebugger("Python3Interpreter")
1407 if not debugEnv and sys.version_info[0] == 3:
1408 debugEnv = sys.executable
1409 elif language == "Python2":
1410 debugEnv = Preferences.getDebugger("PythonInterpreter")
1411 if not debugEnv and sys.version_info[0] == 2:
1412 debugEnv = sys.executable
1413 else:
1344 debugEnv = sys.executable 1414 debugEnv = sys.executable
1345 elif language == "Python2": 1415 debugEnv = os.path.dirname(debugEnv)
1346 debugEnv = Preferences.getDebugger("PythonInterpreter") 1416 if debugEnv and not os.path.exists(debugEnv):
1347 if not debugEnv and sys.version_info[0] == 2: 1417 if (language == "Python3" and sys.version_info[0] == 3) or \
1348 debugEnv = sys.executable 1418 (language == "Python2" and sys.version_info[0] == 2):
1349 else: 1419 debugEnv = sys.exec_prefix
1350 debugEnv = sys.executable 1420 else:
1351 debugEnv = os.path.dirname(debugEnv) 1421 debugEnv = ""
1352 if debugEnv and not os.path.exists(debugEnv):
1353 if (language == "Python3" and sys.version_info[0] == 3) or \
1354 (language == "Python2" and sys.version_info[0] == 2):
1355 debugEnv = sys.exec_prefix
1356 else:
1357 debugEnv = ""
1358 return debugEnv 1422 return debugEnv
1359 1423
1360 def __getDjangoAdminCommand(self, language=""): 1424 def __getDjangoAdminCommand(self, language=""):
1361 """ 1425 """
1362 Private method to build a django-admin.py command. 1426 Private method to build a django-admin.py command.
1369 language = self.__e5project.getProjectLanguage() 1433 language = self.__e5project.getProjectLanguage()
1370 1434
1371 virtualEnv = self.__getVirtualEnvironment(language) 1435 virtualEnv = self.__getVirtualEnvironment(language)
1372 if virtualEnv: 1436 if virtualEnv:
1373 if isWindowsPlatform(): 1437 if isWindowsPlatform():
1374 cmd = os.path.join(virtualEnv, "Scripts", "django-admin.py") 1438 for cmd in [
1439 # standard Python
1440 os.path.join(virtualEnv, "Scripts", "django-admin.py"),
1441 # PyPy
1442 os.path.join(virtualEnv, "bin", "django-admin.py"),
1443 ]:
1444 if os.path.exists(cmd):
1445 break
1446 else:
1447 cmd = ""
1375 else: 1448 else:
1376 cmds = [ 1449 cmds = [
1377 os.path.join(virtualEnv, "bin", "django-admin.py"), 1450 os.path.join(virtualEnv, "bin", "django-admin.py"),
1378 os.path.join(virtualEnv, "bin", "django-admin"), 1451 os.path.join(virtualEnv, "bin", "django-admin"),
1379 os.path.join(virtualEnv, "local", "bin", 1452 os.path.join(virtualEnv, "local", "bin",
1425 Private method to build the Python command. 1498 Private method to build the Python command.
1426 1499
1427 @return python command (string) 1500 @return python command (string)
1428 """ 1501 """
1429 language = self.__e5project.getProjectLanguage() 1502 language = self.__e5project.getProjectLanguage()
1430 virtualEnv = self.__getVirtualEnvironment() 1503 if self.__virtualEnvManager:
1431 if isWindowsPlatform(): 1504 if language == "Python3":
1432 pythonExeList = ["python.exe", "pypy.exe"] 1505 venvName = self.__plugin.getPreferences(
1433 if not virtualEnv: 1506 "VirtualEnvironmentNamePy3")
1434 virtualEnv = self.__getDebugEnvironment(language) 1507 if not venvName:
1435 for pythonExe in pythonExeList: 1508 # if none configured, use the global one
1436 for python in [ 1509 venvName = Preferences.getDebugger("Python3VirtualEnv")
1437 os.path.join(virtualEnv, "Scripts", pythonExe), 1510 elif language == "Python2":
1438 os.path.join(virtualEnv, "bin", pythonExe), 1511 venvName = self.__plugin.getPreferences(
1439 os.path.join(virtualEnv, pythonExe) 1512 "VirtualEnvironmentNamePy2")
1440 ]: 1513 if not venvName:
1441 if os.path.exists(python): 1514 # if none configured, use the global one
1515 venvName = Preferences.getDebugger("Python2VirtualEnv")
1516 else:
1517 venvName = ""
1518 if venvName:
1519 python = self.__virtualEnvManager.getVirtualenvInterpreter(
1520 venvName)
1521 else:
1522 python = ""
1523 else:
1524 # backward compatibility
1525 virtualEnv = self.__getVirtualEnvironment()
1526 if isWindowsPlatform():
1527 pythonExeList = ["python.exe", "pypy.exe"]
1528 if not virtualEnv:
1529 virtualEnv = self.__getDebugEnvironment(language)
1530 for pythonExe in pythonExeList:
1531 for python in [
1532 os.path.join(virtualEnv, "Scripts", pythonExe),
1533 os.path.join(virtualEnv, "bin", pythonExe),
1534 os.path.join(virtualEnv, pythonExe)
1535 ]:
1536 if os.path.exists(python):
1537 break
1538 else:
1539 python = ""
1540
1541 if python:
1442 break 1542 break
1443 else: 1543 else:
1444 python = "" 1544 python = ""
1545 else:
1546 if language == "Python3":
1547 pythonExeList = ["python3", "pypy3"]
1548 elif language == "Python2":
1549 pythonExeList = ["python2", "pypy2"]
1550 if not virtualEnv:
1551 virtualEnv = self.__getDebugEnvironment(language)
1445 1552
1446 if python: 1553 for pythonExe in pythonExeList:
1447 break 1554 for python in [
1448 else: 1555 os.path.join(virtualEnv, "bin", pythonExe),
1449 python = "" 1556 # omit the version character
1450 else: 1557 os.path.join(virtualEnv, "bin", pythonExe)[:-1],
1451 if language == "Python3": 1558 os.path.join(virtualEnv, pythonExe),
1452 pythonExeList = ["python3", "pypy3"] 1559 # omit the version character
1453 elif language == "Python2": 1560 os.path.join(virtualEnv, pythonExe)[:-1],
1454 pythonExeList = ["python2", "pypy2"] 1561 ]:
1455 if not virtualEnv: 1562 if os.path.exists(python):
1456 virtualEnv = self.__getDebugEnvironment(language) 1563 break
1457 1564 else:
1458 for pythonExe in pythonExeList: 1565 python = ""
1459 for python in [ 1566
1460 os.path.join(virtualEnv, "bin", pythonExe), 1567 if python:
1461 # omit the version character
1462 os.path.join(virtualEnv, "bin", pythonExe)[:-1],
1463 os.path.join(virtualEnv, pythonExe),
1464 # omit the version character
1465 os.path.join(virtualEnv, pythonExe)[:-1],
1466 ]:
1467 if os.path.exists(python):
1468 break 1568 break
1469 else: 1569 else:
1470 python = "" 1570 python = ""
1471
1472 if python:
1473 break
1474 else:
1475 python = ""
1476 1571
1477 return python 1572 return python
1478 1573
1479 def __djangoInfo(self): 1574 def __djangoInfo(self):
1480 """ 1575 """

eric ide

mercurial