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