eric6/Plugins/VcsPlugins/vcsMercurial/HgUtilities.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7192
a22eee00b052
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2010 - 2019 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing some common utility functions for the Mercurial package.
8 """
9
10 from __future__ import unicode_literals
11 try:
12 str = unicode
13 except NameError:
14 pass
15
16 import os
17 import re
18
19 from PyQt5.QtCore import QProcess, QProcessEnvironment, QCoreApplication
20
21 import Utilities
22
23
24 def getConfigPath():
25 """
26 Public function to get the filename of the config file.
27
28 @return filename of the config file (string)
29 """
30 if Utilities.isWindowsPlatform():
31 userprofile = os.environ["USERPROFILE"]
32 return os.path.join(userprofile, "Mercurial.ini")
33 else:
34 homedir = Utilities.getHomeDir()
35 return os.path.join(homedir, ".hgrc")
36
37
38 def prepareProcess(proc, encoding="", language=""):
39 """
40 Public function to prepare the given process.
41
42 @param proc reference to the process to be prepared (QProcess)
43 @param encoding encoding to be used by the process (string)
44 @param language language to be set (string)
45 """
46 env = QProcessEnvironment.systemEnvironment()
47 env.insert("HGPLAIN", '1')
48
49 # set the encoding for the process
50 if encoding:
51 env.insert("HGENCODING", encoding)
52
53 # set the language for the process
54 if language:
55 env.insert("LANGUAGE", language)
56
57 proc.setProcessEnvironment(env)
58
59
60 def hgVersion(plugin):
61 """
62 Public method to determine the Mercurial version.
63
64 @param plugin reference to the plugin object
65 @type VcsMercurialPlugin
66 @return tuple containing the Mercurial version as a string and as a tuple
67 and an error message.
68 @rtype tuple of str, tuple of int and str
69 """
70 versionStr = ""
71 version = ()
72 errorMsg = ""
73
74 args = ["version"]
75 args.extend(plugin.getGlobalOptions())
76 process = QProcess()
77 process.start('hg', args)
78 procStarted = process.waitForStarted(5000)
79 if procStarted:
80 finished = process.waitForFinished(30000)
81 if finished and process.exitCode() == 0:
82 output = str(process.readAllStandardOutput(),
83 plugin.getPreferences("Encoding"), 'replace')
84 versionStr = output.splitlines()[0].split()[-1][0:-1]
85 v = list(re.match(r'.*?(\d+)\.(\d+)\.?(\d+)?(\+[0-9a-f-]+)?',
86 versionStr).groups())
87 if v[-1] is None:
88 del v[-1]
89 for i in range(3):
90 try:
91 v[i] = int(v[i])
92 except TypeError:
93 v[i] = 0
94 except IndexError:
95 v.append(0)
96 version = tuple(v)
97 else:
98 if finished:
99 errorMsg = QCoreApplication.translate(
100 "HgUtilities",
101 "The hg process finished with the exit code {0}")\
102 .format(process.exitCode())
103 else:
104 errorMsg = QCoreApplication.translate(
105 "HgUtilities",
106 "The hg process did not finish within 30s.")
107 else:
108 errorMsg = QCoreApplication.translate(
109 "HgUtilities",
110 "Could not start the hg executable.")
111
112 return versionStr, version, errorMsg

eric ide

mercurial