Plugins/VcsPlugins/vcsMercurial/HgUtilities.py

changeset 5292
ac8b476ba122
parent 4631
5c1a96925da4
child 5293
26f1e696a2a5
--- a/Plugins/VcsPlugins/vcsMercurial/HgUtilities.py	Sun Nov 06 13:39:36 2016 +0100
+++ b/Plugins/VcsPlugins/vcsMercurial/HgUtilities.py	Sun Nov 06 17:02:45 2016 +0100
@@ -10,8 +10,9 @@
 from __future__ import unicode_literals
 
 import os
+import re
 
-from PyQt5.QtCore import QProcessEnvironment
+from PyQt5.QtCore import QProcess, QProcessEnvironment, QCoreApplication
 
 import Utilities
 
@@ -50,3 +51,59 @@
         env.insert("LANGUAGE", language)
     
     proc.setProcessEnvironment(env)
+
+
+def hgVersion(plugin):
+    """
+    Public method to determine the Mercurial version.
+    
+    @param plugin reference to the plugin object
+    @type VcsMercurialPlugin
+    @return tuple containing the Mercurial version as a string and as a tuple
+        and an error message.
+    @rtype tuple of str, tuple of int and str
+    """
+    versionStr = ""
+    version = ()
+    errorMsg = ""
+    
+    
+    args = ["version"]
+    args.extend(plugin.getGlobalOptions())
+    process = QProcess()
+    process.start('hg', args)
+    procStarted = process.waitForStarted(5000)
+    if procStarted:
+        finished = process.waitForFinished(30000)
+        if finished and process.exitCode() == 0:
+            output = str(process.readAllStandardOutput(),
+                         plugin.getPreferences("Encoding"), 'replace')
+            versionStr = output.splitlines()[0].split()[-1][0:-1]
+            v = list(re.match(r'.*?(\d+)\.(\d+)\.?(\d+)?(\+[0-9a-f-]+)?',
+                              versionStr).groups())
+            if v[-1] is None:
+                del v[-1]
+            for i in range(3):
+                try:
+                    v[i] = int(v[i])
+                except TypeError:
+                    v[i] = 0
+                except IndexError:
+                    v.append(0)
+            version = tuple(v)
+        else:
+            if finished:
+                errorMsg = QCoreApplication.translate(
+                    "HgUtilities",
+                    "The hg process finished with the exit code {0}")\
+                    .format(process.exitCode())
+            else:
+                errorMsg = QCoreApplication.translate(
+                    "HgUtilities",
+                    "The hg process did not finish within 30s.")
+    else:
+        errorMsg = QCoreApplication.translate(
+            "HgUtilities",
+            "Could not start the hg executable.")
+    
+    return versionStr, version, errorMsg

eric ide

mercurial