ProjectPyramid/Project.py

changeset 44
989c961c33ab
parent 39
94e448a362b2
child 48
c313efdb01de
diff -r 14e0bd707f16 -r 989c961c33ab ProjectPyramid/Project.py
--- a/ProjectPyramid/Project.py	Tue Mar 26 16:47:15 2013 +0100
+++ b/ProjectPyramid/Project.py	Mon Apr 01 19:40:41 2013 +0200
@@ -53,6 +53,8 @@
         self.__mainMenu = None
         
         self.__serverProc = None
+        
+        self.__pyramidVersion = ""
     
     def initActions(self):
         """
@@ -441,13 +443,88 @@
             self.__serverProcFinished()
         self.__setCurrentProject(None)
     
-    def __getVirtualEnvironment(self):
+    def __getExecutablePaths(self, file):
+        """
+        Private method to build all full path of an executable file from
+        the environment.
+        
+        @param file filename of the executable (string)
+        @return list of full executable names, if the executable file is accessible
+            via the searchpath defined by the PATH environment variable, or an
+            empty list otherwise.
+        """
+        paths = []
+        
+        if os.path.isabs(file):
+            if os.access(file, os.X_OK):
+                return [file]
+            else:
+                return []
+            
+        cur_path = os.path.join(os.curdir, file)
+        if os.path.exists(cur_path):
+            if os.access(cur_path, os.X_OK):
+                paths.append(cur_path)
+
+        path = os.getenv('PATH')
+        
+        # environment variable not defined
+        if path is not None:
+            dirs = path.split(os.pathsep)
+            for dir in dirs:
+                exe = os.path.join(dir, file)
+                if os.access(exe, os.X_OK) and exe not in paths:
+                    paths.append(exe)
+        
+        return paths
+    
+    def supportedPythonVariants(self):
+        """
+        Public method to get the supported Python variants.
+        
+        @return list of supported Python variants (list of strings)
+        """
+        variants = []
+        cmd = "pcreate"
+        
+        for variant in 'Python2', 'Python3':
+            virtEnv = self.__getVirtualEnvironment(variant)
+            if virtEnv:
+                fullCmd = self.getPyramidCommand(cmd, variant)
+                if fullCmd != cmd:
+                    variants.append(variant)
+            else:
+                try:
+                    fullCmds = Utilities.getExecutablePaths(cmd)
+                except AttributeError:
+                    fullCmds = self.__getExecutablePaths(cmd)
+                for fullCmd in fullCmds:
+                    try:
+                        f = open(fullCmd, 'r', encoding='utf-8')
+                        l0 = f.readline()
+                        f.close()
+                    except (IOError, OSError):
+                        l0 = ""
+                    if variant.lower() in l0.lower() or \
+                       "{0}.".format(variant[-1]) in l0 or \
+                       (variant == "Python2" and \
+                        "python3" not in l0.lower() and \
+                        "python" in l0.lower()):
+                        variants.append(variant)
+                        break
+        
+        return variants
+    
+    def __getVirtualEnvironment(self, language=""):
         """
         Private method to get the path of the virtual environment.
         
+        @param language Python variant to get the virtual environment
+            for (string, one of '', 'Python2' or 'Python3')
         @return path of the virtual environment (string)
         """
-        language = self.__e5project.getProjectLanguage()
+        if not language:
+            language = self.__e5project.getProjectLanguage()
         if language == "Python3":
             virtEnv = self.__plugin.getPreferences("VirtualEnvironmentPy3")
         elif language == "Python2":
@@ -458,19 +535,27 @@
             virtEnv = ""
         return virtEnv
     
-    def getPyramidCommand(self, cmd):
+    def getPyramidCommand(self, cmd, language=""):
         """
         Public method to build a Pyramid command.
         
         @param cmd command (string)
+        @param language Python variant to get the virtual environment
+            for (string, one of '', 'Python2' or 'Python3')
         @return full pyramid command (string)
         """
-        virtualEnv = self.__getVirtualEnvironment()
+        virtualEnv = self.__getVirtualEnvironment(language)
         if virtualEnv:
             if isWindowsPlatform():
                 cmd = os.path.join(virtualEnv, "Scripts", cmd)
             else:
-                cmd = os.path.join(virtualEnv, "bin", cmd)
+                fullCmd = os.path.join(virtualEnv, "bin", cmd)
+                if not os.path.exists(fullCmd):
+                    fullCmd = os.path.join(virtualEnv, "local", "bin", cmd)
+                    if not os.path.exists(fullCmd):
+                        # fall back to just cmd
+                        fullCmd = cmd
+                cmd = fullCmd
         return cmd
     
     def getPythonCommand(self):
@@ -505,20 +590,48 @@
         """
         Private slot to show some info about Pyramid.
         """
+        version = self.getPyramidVersion()
         url = "http://www.pylonsproject.org/projects/pyramid/about"
+        
         msgBox = E5MessageBox.E5MessageBox(E5MessageBox.Question,
             self.trUtf8("About Pyramid"),
             self.trUtf8(
                 "<p>Pyramid is a high-level Python Web framework that encourages rapid "
                 "development and clean, pragmatic design.</p>"
-                "<p>URL: <a href=\"{0}\">{0}</a></p>"
-            ).format(url),
+                "<p><table>"
+                "<tr><td>Version:</td><td>{0}</td></tr>"
+                "<tr><td>URL:</td><td><a href=\"{1}\">"
+                "{1}</a></td></tr>"
+                "</table></p>"
+            ).format(version, url),
             modal=True,
             buttons=E5MessageBox.Ok)
         msgBox.setIconPixmap(UI.PixmapCache.getPixmap(
             os.path.join("ProjectPyramid", "icons", "pyramid64.png")))
         msgBox.exec_()
     
+    def getPyramidVersion(self):
+        """
+        Public method to get the Pyramid version.
+        
+        @return Pyramid version (string)
+        """
+        if not self.__pyramidVersion:
+            cmd = self.getPyramidCommand("pcreate")
+            try:
+                f = open(cmd, 'r', encoding="utf-8")
+                lines = f.read().splitlines()
+                f.close()
+                for line in lines:
+                    if line.startswith("__requires__"):
+                        # sample: __requires__ = 'pyramid==1.4'
+                        vers = line.strip().split()[-1][1:-1].split("==")[1]
+                        self.__pyramidVersion = vers
+            except (IOError, OSError):
+                self.__pyramidVersion = ""
+        
+        return self.__pyramidVersion
+    
     def isSpawningConsole(self, consoleCmd):
         """
         Public method to check, if the given console is a spawning console.

eric ide

mercurial