ProjectPyramid/Project.py

changeset 114
4de6dbce4dd6
parent 112
916727bdff4d
child 126
f148f85d9e16
equal deleted inserted replaced
113:10364f51ba54 114:4de6dbce4dd6
106 super(Project, self).__init__(parent) 106 super(Project, self).__init__(parent)
107 107
108 self.__plugin = plugin 108 self.__plugin = plugin
109 self.__ui = parent 109 self.__ui = parent
110 self.__e5project = e5App().getObject("Project") 110 self.__e5project = e5App().getObject("Project")
111 try:
112 self.__virtualEnvManager = e5App().getObject("VirtualEnvManager")
113 except KeyError:
114 self.__virtualEnvManager = None
111 self.__hooksInstalled = False 115 self.__hooksInstalled = False
112 116
113 self.__menus = {} # dictionary with references to menus 117 self.__menus = {} # dictionary with references to menus
114 118
115 self.__serverProc = None 119 self.__serverProc = None
567 self.__serverProcFinished() 571 self.__serverProcFinished()
568 self.__setCurrentProject(None) 572 self.__setCurrentProject(None)
569 573
570 def __getExecutablePaths(self, file): 574 def __getExecutablePaths(self, file):
571 """ 575 """
572 Private method to build all full path of an executable file from 576 Private method to build all full paths of an executable file from
573 the environment. 577 the environment.
574 578
575 @param file filename of the executable (string) 579 @param file filename of the executable (string)
576 @return list of full executable names, if the executable file is 580 @return list of full executable names, if the executable file is
577 accessible via the searchpath defined by the PATH environment 581 accessible via the searchpath defined by the PATH environment
621 fullCmd = self.getPyramidCommand(cmd, variant) 625 fullCmd = self.getPyramidCommand(cmd, variant)
622 if isWindowsPlatform(): 626 if isWindowsPlatform():
623 if fullCmd != cmd: 627 if fullCmd != cmd:
624 variants.append(variant) 628 variants.append(variant)
625 else: 629 else:
626 try: 630 if cmd:
627 fullCmds = Utilities.getExecutablePaths(cmd)
628 except AttributeError:
629 fullCmds = self.__getExecutablePaths(cmd)
630 for fullCmd in fullCmds:
631 try: 631 try:
632 f = open(fullCmd, 'r', encoding='utf-8') 632 fullCmds = Utilities.getExecutablePaths(cmd)
633 l0 = f.readline() 633 except AttributeError:
634 f.close() 634 fullCmds = self.__getExecutablePaths(cmd)
635 except (IOError, OSError): 635 for fullCmd in fullCmds:
636 l0 = "" 636 try:
637 if variant.lower() in l0.lower() or \ 637 f = open(fullCmd, 'r', encoding='utf-8')
638 "{0}.".format(variant[-1]) in l0 or \ 638 l0 = f.readline()
639 (variant == "Python2" and 639 f.close()
640 "python3" not in l0.lower() and 640 except (IOError, OSError):
641 "python" in l0.lower()): 641 l0 = ""
642 variants.append(variant) 642 if self.__isSuitableForVariant(variant, l0):
643 break 643 variants.append(variant)
644 break
644 645
645 return variants 646 return variants
647
648 def __isSuitableForVariant(self, variant, line0):
649 """
650 Private method to test, if a detected command file is suitable for the
651 given Python variant.
652
653 @param variant Python variant to test for
654 @type str (one of Python2 or Python3)
655 @param line0 first line of the executable
656 @type str
657 @return flag indicating a suitable command
658 @rtype bool
659 """
660 assert variant in ("Python2", "Python3")
661
662 l0 = line0.lower()
663 ok = (variant.lower() in l0 or
664 "{0}.".format(variant[-1]) in l0)
665 if variant == "Python2":
666 ok |= "python3" not in l0 and "python" in l0
667 ok |= "pypy2" in l0
668 ok |= "pypy3" not in l0 and "pypy" in l0
669 else:
670 ok |= "pypy3" in l0
671
672 return ok
646 673
647 def __getVirtualEnvironment(self, language=""): 674 def __getVirtualEnvironment(self, language=""):
648 """ 675 """
649 Private method to get the path of the virtual environment. 676 Private method to get the path of the virtual environment.
650 677
652 for (string, one of '', 'Python2' or 'Python3') 679 for (string, one of '', 'Python2' or 'Python3')
653 @return path of the virtual environment (string) 680 @return path of the virtual environment (string)
654 """ 681 """
655 if not language: 682 if not language:
656 language = self.__e5project.getProjectLanguage() 683 language = self.__e5project.getProjectLanguage()
657 if language == "Python3": 684 if self.__virtualEnvManager:
658 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3") 685 if language == "Python3":
659 elif language == "Python2": 686 venvName = self.__plugin.getPreferences(
660 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2") 687 "VirtualEnvironmentNamePy3")
688 elif language == "Python2":
689 venvName = self.__plugin.getPreferences(
690 "VirtualEnvironmentNamePy2")
691 else:
692 venvName = ""
693 if venvName:
694 virtEnv = self.__virtualEnvManager.getVirtualenvDirectory(
695 venvName)
696 if not virtEnv:
697 virtEnv = os.path.dirname(
698 self.__virtualEnvManager.getVirtualenvInterpreter(
699 venvName))
700 if virtEnv.endswith(("Scripts", "bin")):
701 virtEnv = os.path.dirname(virtEnv)
702 else:
703 virtEnv = ""
661 else: 704 else:
662 virtEnv = "" 705 # backward compatibility
706 if language == "Python3":
707 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3")
708 elif language == "Python2":
709 virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy2")
710 else:
711 virtEnv = ""
663 if virtEnv and not os.path.exists(virtEnv): 712 if virtEnv and not os.path.exists(virtEnv):
664 virtEnv = "" 713 virtEnv = ""
665 return virtEnv 714 return virtEnv
666 715
667 def __getDebugEnvironment(self, language=""): 716 def __getDebugEnvironment(self, language=""):
672 for (string, one of '', 'Python2' or 'Python3') 721 for (string, one of '', 'Python2' or 'Python3')
673 @return path of the debugger environment (string) 722 @return path of the debugger environment (string)
674 """ 723 """
675 if not language: 724 if not language:
676 language = self.__e5project.getProjectLanguage() 725 language = self.__e5project.getProjectLanguage()
677 if language == "Python3": 726 if self.__virtualEnvManager:
678 debugEnv = Preferences.getDebugger("Python3Interpreter") 727 debugEnv = self.__getVirtualEnvironment(language)
679 if not debugEnv and sys.version_info[0] == 3: 728 if not debugEnv:
729 if language == "Python3":
730 venvName = Preferences.getDebugger("Python3VirtualEnv")
731 elif language == "Python2":
732 venvName = Preferences.getDebugger("Python2VirtualEnv")
733 else:
734 venvName = ""
735
736 if venvName:
737 debugEnv = self.__virtualEnvManager.getVirtualenvDirectory(
738 venvName)
739 else:
740 debugEnv = ""
741 else:
742 # backward compatibility
743 if language == "Python3":
744 debugEnv = Preferences.getDebugger("Python3Interpreter")
745 if not debugEnv and sys.version_info[0] == 3:
746 debugEnv = sys.executable
747 elif language == "Python2":
748 debugEnv = Preferences.getDebugger("PythonInterpreter")
749 if not debugEnv and sys.version_info[0] == 2:
750 debugEnv = sys.executable
751 else:
680 debugEnv = sys.executable 752 debugEnv = sys.executable
681 elif language == "Python2": 753 debugEnv = os.path.dirname(debugEnv)
682 debugEnv = Preferences.getDebugger("PythonInterpreter") 754 if debugEnv and not os.path.exists(debugEnv):
683 if not debugEnv and sys.version_info[0] == 2: 755 if (language == "Python3" and sys.version_info[0] == 3) or \
684 debugEnv = sys.executable 756 (language == "Python2" and sys.version_info[0] == 2):
685 else: 757 debugEnv = sys.exec_prefix
686 debugEnv = sys.executable 758 else:
687 debugEnv = os.path.dirname(debugEnv) 759 debugEnv = ""
688 if debugEnv and not os.path.exists(debugEnv):
689 if (language == "Python3" and sys.version_info[0] == 3) or \
690 (language == "Python2" and sys.version_info[0] == 2):
691 debugEnv = sys.exec_prefix
692 else:
693 debugEnv = ""
694 return debugEnv 760 return debugEnv
695 761
696 def getPyramidCommand(self, cmd, language=""): 762 def getPyramidCommand(self, cmd, language=""):
697 """ 763 """
698 Public method to build a Pyramid command. 764 Public method to build a Pyramid command.
700 @param cmd command (string) 766 @param cmd command (string)
701 @param language Python variant to get the virtual environment 767 @param language Python variant to get the virtual environment
702 for (string, one of '', 'Python2' or 'Python3') 768 for (string, one of '', 'Python2' or 'Python3')
703 @return full pyramid command (string) 769 @return full pyramid command (string)
704 """ 770 """
771 if not language:
772 language = self.__e5project.getProjectLanguage()
773
705 virtualEnv = self.__getVirtualEnvironment(language) 774 virtualEnv = self.__getVirtualEnvironment(language)
706 if isWindowsPlatform() and not virtualEnv: 775 if isWindowsPlatform() and not virtualEnv:
707 virtualEnv = self.__getDebugEnvironment(language) 776 virtualEnv = self.__getDebugEnvironment(language)
708 if isWindowsPlatform(): 777 if isWindowsPlatform():
709 fullCmds = [ 778 fullCmds = [
731 Public method to build the Python command. 800 Public method to build the Python command.
732 801
733 @return python command (string) 802 @return python command (string)
734 """ 803 """
735 language = self.__e5project.getProjectLanguage() 804 language = self.__e5project.getProjectLanguage()
736 virtualEnv = self.__getVirtualEnvironment() 805 if self.__virtualEnvManager:
737 if isWindowsPlatform(): 806 if language == "Python3":
738 pythonExeList = ["python.exe", "pypy.exe"] 807 venvName = self.__plugin.getPreferences(
739 if not virtualEnv: 808 "VirtualEnvironmentNamePy3")
740 virtualEnv = self.__getDebugEnvironment(language) 809 if not venvName:
741 for pythonExe in pythonExeList: 810 # if none configured, use the global one
742 for python in [ 811 venvName = Preferences.getDebugger("Python3VirtualEnv")
743 os.path.join(virtualEnv, "Scripts", pythonExe), 812 elif language == "Python2":
744 os.path.join(virtualEnv, "bin", pythonExe), 813 venvName = self.__plugin.getPreferences(
745 os.path.join(virtualEnv, pythonExe) 814 "VirtualEnvironmentNamePy2")
746 ]: 815 if not venvName:
747 if os.path.exists(python): 816 # if none configured, use the global one
817 venvName = Preferences.getDebugger("Python2VirtualEnv")
818 else:
819 venvName = ""
820 if venvName:
821 python = self.__virtualEnvManager.getVirtualenvInterpreter(
822 venvName)
823 else:
824 python = ""
825 else:
826 # backward compatibility
827 virtualEnv = self.__getVirtualEnvironment()
828 if isWindowsPlatform():
829 pythonExeList = ["python.exe", "pypy.exe"]
830 if not virtualEnv:
831 virtualEnv = self.__getDebugEnvironment(language)
832 for pythonExe in pythonExeList:
833 for python in [
834 os.path.join(virtualEnv, "Scripts", pythonExe),
835 os.path.join(virtualEnv, "bin", pythonExe),
836 os.path.join(virtualEnv, pythonExe)
837 ]:
838 if os.path.exists(python):
839 break
840 else:
841 python = ""
842
843 if python:
748 break 844 break
749 else: 845 else:
750 python = "" 846 python = ""
847 else:
848 if language == "Python3":
849 pythonExeList = ["python3", "pypy3"]
850 elif language == "Python2":
851 pythonExeList = ["python2", "pypy2"]
852 if not virtualEnv:
853 virtualEnv = self.__getDebugEnvironment(language)
751 854
752 if python: 855 for pythonExe in pythonExeList:
753 break 856 for python in [
754 else: 857 os.path.join(virtualEnv, "bin", pythonExe),
755 python = "" 858 # omit the version character
756 else: 859 os.path.join(virtualEnv, "bin", pythonExe)[:-1],
757 if language == "Python3": 860 os.path.join(virtualEnv, pythonExe),
758 pythonExeList = ["python3", "pypy3"] 861 # omit the version character
759 elif language == "Python2": 862 os.path.join(virtualEnv, pythonExe)[:-1],
760 pythonExeList = ["python2", "pypy2"] 863 ]:
761 if not virtualEnv: 864 if os.path.exists(python):
762 virtualEnv = self.__getDebugEnvironment(language) 865 break
763 866 else:
764 for pythonExe in pythonExeList: 867 python = ""
765 for python in [ 868
766 os.path.join(virtualEnv, "bin", pythonExe), 869 if python:
767 # omit the version character
768 os.path.join(virtualEnv, "bin", pythonExe)[:-1],
769 os.path.join(virtualEnv, pythonExe),
770 # omit the version character
771 os.path.join(virtualEnv, pythonExe)[:-1],
772 ]:
773 if os.path.exists(python):
774 break 870 break
775 else: 871 else:
776 python = "" 872 python = ""
777
778 if python:
779 break
780 else:
781 python = ""
782 873
783 return python 874 return python
784 875
785 def __pyramidInfo(self): 876 def __pyramidInfo(self):
786 """ 877 """

eric ide

mercurial