eric7/Plugins/VcsPlugins/vcsMercurial/HgUtilities.py

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

eric ide

mercurial