eric6/Plugins/VcsPlugins/vcsMercurial/HgUtilities.py

Wed, 30 Dec 2020 11:00:05 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 30 Dec 2020 11:00:05 +0100
changeset 7923
91e843545d9a
parent 7780
41420f82c0ac
child 8288
809d5d5ac2ba
permissions
-rw-r--r--

Updated copyright for 2021.

178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7780
diff changeset
3 # Copyright (c) 2010 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing some common utility functions for the Mercurial package.
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10 import os
5292
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
11 import re
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
5292
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
13 from PyQt5.QtCore import QProcess, QProcessEnvironment, QCoreApplication
2816
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
14
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15 import Utilities
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
17
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 def getConfigPath():
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
3302
e92f0dd51979 Removed the Mercurial support for a command options dialog and added useable global options to the Mercurial config page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
20 Public function to get the filename of the config file.
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 @return filename of the config file (string)
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 """
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 if Utilities.isWindowsPlatform():
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 userprofile = os.environ["USERPROFILE"]
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 return os.path.join(userprofile, "Mercurial.ini")
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 else:
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 homedir = Utilities.getHomeDir()
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 return os.path.join(homedir, ".hgrc")
2816
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
30
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
31
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
32 def prepareProcess(proc, encoding="", language=""):
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
33 """
3302
e92f0dd51979 Removed the Mercurial support for a command options dialog and added useable global options to the Mercurial config page.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 3160
diff changeset
34 Public function to prepare the given process.
2816
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
35
6062
926d2ad79e6d Fixed an issue in the git plug-in differentiating between local and remote branches and fixed some typos.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
36 @param proc reference to the process to be prepared (QProcess)
2816
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
37 @param encoding encoding to be used by the process (string)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
38 @param language language to be set (string)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
39 """
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
40 env = QProcessEnvironment.systemEnvironment()
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
41 env.insert("HGPLAIN", '1')
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
42
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
43 # set the encoding for the process
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
44 if encoding:
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
45 env.insert("HGENCODING", encoding)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
46
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
47 # set the language for the process
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
48 if language:
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
49 env.insert("LANGUAGE", language)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
50
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
51 proc.setProcessEnvironment(env)
5292
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
52
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
53
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
54 def hgVersion(plugin):
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
55 """
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
56 Public method to determine the Mercurial version.
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
57
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
58 @param plugin reference to the plugin object
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
59 @type VcsMercurialPlugin
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
60 @return tuple containing the Mercurial version as a string and as a tuple
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
61 and an error message.
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
62 @rtype tuple of str, tuple of int and str
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
63 """
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
64 versionStr = ""
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
65 version = ()
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
66 errorMsg = ""
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
67
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
68 args = ["version"]
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
69 args.extend(plugin.getGlobalOptions())
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
70 process = QProcess()
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
71 process.start('hg', args)
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
72 procStarted = process.waitForStarted(5000)
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
73 if procStarted:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
74 finished = process.waitForFinished(30000)
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
75 if finished and process.exitCode() == 0:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
76 output = str(process.readAllStandardOutput(),
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
77 plugin.getPreferences("Encoding"), 'replace')
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
78 versionStr = output.splitlines()[0].split()[-1][0:-1]
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
79 v = list(re.match(r'.*?(\d+)\.(\d+)\.?(\d+)?(\+[0-9a-f-]+)?',
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
80 versionStr).groups())
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
81 if v[-1] is None:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
82 del v[-1]
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
83 for i in range(3):
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
84 try:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
85 v[i] = int(v[i])
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
86 except TypeError:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
87 v[i] = 0
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
88 except IndexError:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
89 v.append(0)
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
90 version = tuple(v)
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
91 else:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
92 if finished:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
93 errorMsg = QCoreApplication.translate(
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
94 "HgUtilities",
7257
c4d0cac9b5c9 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
95 "The hg process finished with the exit code {0}"
c4d0cac9b5c9 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
96 ).format(process.exitCode())
5292
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
97 else:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
98 errorMsg = QCoreApplication.translate(
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
99 "HgUtilities",
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
100 "The hg process did not finish within 30s.")
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
101 else:
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
102 errorMsg = QCoreApplication.translate(
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
103 "HgUtilities",
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
104 "Could not start the hg executable.")
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
105
ac8b476ba122 Added support for host fingerprints in the hostsecurity section of the Mercurial configuration file (as of hg 3.9.0).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4631
diff changeset
106 return versionStr, version, errorMsg

eric ide

mercurial