ProjectPyramid/Project.py

changeset 44
989c961c33ab
parent 39
94e448a362b2
child 48
c313efdb01de
equal deleted inserted replaced
43:14e0bd707f16 44:989c961c33ab
51 self.__hooksInstalled = False 51 self.__hooksInstalled = False
52 52
53 self.__mainMenu = None 53 self.__mainMenu = None
54 54
55 self.__serverProc = None 55 self.__serverProc = None
56
57 self.__pyramidVersion = ""
56 58
57 def initActions(self): 59 def initActions(self):
58 """ 60 """
59 Public method to define the Pyramid actions. 61 Public method to define the Pyramid actions.
60 """ 62 """
439 """ 441 """
440 if self.__serverProc is not None: 442 if self.__serverProc is not None:
441 self.__serverProcFinished() 443 self.__serverProcFinished()
442 self.__setCurrentProject(None) 444 self.__setCurrentProject(None)
443 445
444 def __getVirtualEnvironment(self): 446 def __getExecutablePaths(self, file):
447 """
448 Private method to build all full path of an executable file from
449 the environment.
450
451 @param file filename of the executable (string)
452 @return list of full executable names, if the executable file is accessible
453 via the searchpath defined by the PATH environment variable, or an
454 empty list otherwise.
455 """
456 paths = []
457
458 if os.path.isabs(file):
459 if os.access(file, os.X_OK):
460 return [file]
461 else:
462 return []
463
464 cur_path = os.path.join(os.curdir, file)
465 if os.path.exists(cur_path):
466 if os.access(cur_path, os.X_OK):
467 paths.append(cur_path)
468
469 path = os.getenv('PATH')
470
471 # environment variable not defined
472 if path is not None:
473 dirs = path.split(os.pathsep)
474 for dir in dirs:
475 exe = os.path.join(dir, file)
476 if os.access(exe, os.X_OK) and exe not in paths:
477 paths.append(exe)
478
479 return paths
480
481 def supportedPythonVariants(self):
482 """
483 Public method to get the supported Python variants.
484
485 @return list of supported Python variants (list of strings)
486 """
487 variants = []
488 cmd = "pcreate"
489
490 for variant in 'Python2', 'Python3':
491 virtEnv = self.__getVirtualEnvironment(variant)
492 if virtEnv:
493 fullCmd = self.getPyramidCommand(cmd, variant)
494 if fullCmd != cmd:
495 variants.append(variant)
496 else:
497 try:
498 fullCmds = Utilities.getExecutablePaths(cmd)
499 except AttributeError:
500 fullCmds = self.__getExecutablePaths(cmd)
501 for fullCmd in fullCmds:
502 try:
503 f = open(fullCmd, 'r', encoding='utf-8')
504 l0 = f.readline()
505 f.close()
506 except (IOError, OSError):
507 l0 = ""
508 if variant.lower() in l0.lower() or \
509 "{0}.".format(variant[-1]) in l0 or \
510 (variant == "Python2" and \
511 "python3" not in l0.lower() and \
512 "python" in l0.lower()):
513 variants.append(variant)
514 break
515
516 return variants
517
518 def __getVirtualEnvironment(self, language=""):
445 """ 519 """
446 Private method to get the path of the virtual environment. 520 Private method to get the path of the virtual environment.
447 521
522 @param language Python variant to get the virtual environment
523 for (string, one of '', 'Python2' or 'Python3')
448 @return path of the virtual environment (string) 524 @return path of the virtual environment (string)
449 """ 525 """
450 language = self.__e5project.getProjectLanguage() 526 if not language:
527 language = self.__e5project.getProjectLanguage()
451 if language == "Python3": 528 if language == "Python3":
452 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3") 529 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3")
453 elif language == "Python2": 530 elif language == "Python2":
454 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2") 531 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2")
455 else: 532 else:
456 virtEnv = "" 533 virtEnv = ""
457 if virtEnv and not os.path.exists(virtEnv): 534 if virtEnv and not os.path.exists(virtEnv):
458 virtEnv = "" 535 virtEnv = ""
459 return virtEnv 536 return virtEnv
460 537
461 def getPyramidCommand(self, cmd): 538 def getPyramidCommand(self, cmd, language=""):
462 """ 539 """
463 Public method to build a Pyramid command. 540 Public method to build a Pyramid command.
464 541
465 @param cmd command (string) 542 @param cmd command (string)
543 @param language Python variant to get the virtual environment
544 for (string, one of '', 'Python2' or 'Python3')
466 @return full pyramid command (string) 545 @return full pyramid command (string)
467 """ 546 """
468 virtualEnv = self.__getVirtualEnvironment() 547 virtualEnv = self.__getVirtualEnvironment(language)
469 if virtualEnv: 548 if virtualEnv:
470 if isWindowsPlatform(): 549 if isWindowsPlatform():
471 cmd = os.path.join(virtualEnv, "Scripts", cmd) 550 cmd = os.path.join(virtualEnv, "Scripts", cmd)
472 else: 551 else:
473 cmd = os.path.join(virtualEnv, "bin", cmd) 552 fullCmd = os.path.join(virtualEnv, "bin", cmd)
553 if not os.path.exists(fullCmd):
554 fullCmd = os.path.join(virtualEnv, "local", "bin", cmd)
555 if not os.path.exists(fullCmd):
556 # fall back to just cmd
557 fullCmd = cmd
558 cmd = fullCmd
474 return cmd 559 return cmd
475 560
476 def getPythonCommand(self): 561 def getPythonCommand(self):
477 """ 562 """
478 Public method to build the Python command. 563 Public method to build the Python command.
503 588
504 def __pyramidInfo(self): 589 def __pyramidInfo(self):
505 """ 590 """
506 Private slot to show some info about Pyramid. 591 Private slot to show some info about Pyramid.
507 """ 592 """
593 version = self.getPyramidVersion()
508 url = "http://www.pylonsproject.org/projects/pyramid/about" 594 url = "http://www.pylonsproject.org/projects/pyramid/about"
595
509 msgBox = E5MessageBox.E5MessageBox(E5MessageBox.Question, 596 msgBox = E5MessageBox.E5MessageBox(E5MessageBox.Question,
510 self.trUtf8("About Pyramid"), 597 self.trUtf8("About Pyramid"),
511 self.trUtf8( 598 self.trUtf8(
512 "<p>Pyramid is a high-level Python Web framework that encourages rapid " 599 "<p>Pyramid is a high-level Python Web framework that encourages rapid "
513 "development and clean, pragmatic design.</p>" 600 "development and clean, pragmatic design.</p>"
514 "<p>URL: <a href=\"{0}\">{0}</a></p>" 601 "<p><table>"
515 ).format(url), 602 "<tr><td>Version:</td><td>{0}</td></tr>"
603 "<tr><td>URL:</td><td><a href=\"{1}\">"
604 "{1}</a></td></tr>"
605 "</table></p>"
606 ).format(version, url),
516 modal=True, 607 modal=True,
517 buttons=E5MessageBox.Ok) 608 buttons=E5MessageBox.Ok)
518 msgBox.setIconPixmap(UI.PixmapCache.getPixmap( 609 msgBox.setIconPixmap(UI.PixmapCache.getPixmap(
519 os.path.join("ProjectPyramid", "icons", "pyramid64.png"))) 610 os.path.join("ProjectPyramid", "icons", "pyramid64.png")))
520 msgBox.exec_() 611 msgBox.exec_()
612
613 def getPyramidVersion(self):
614 """
615 Public method to get the Pyramid version.
616
617 @return Pyramid version (string)
618 """
619 if not self.__pyramidVersion:
620 cmd = self.getPyramidCommand("pcreate")
621 try:
622 f = open(cmd, 'r', encoding="utf-8")
623 lines = f.read().splitlines()
624 f.close()
625 for line in lines:
626 if line.startswith("__requires__"):
627 # sample: __requires__ = 'pyramid==1.4'
628 vers = line.strip().split()[-1][1:-1].split("==")[1]
629 self.__pyramidVersion = vers
630 except (IOError, OSError):
631 self.__pyramidVersion = ""
632
633 return self.__pyramidVersion
521 634
522 def isSpawningConsole(self, consoleCmd): 635 def isSpawningConsole(self, consoleCmd):
523 """ 636 """
524 Public method to check, if the given console is a spawning console. 637 Public method to check, if the given console is a spawning console.
525 638

eric ide

mercurial