Plugins/VcsPlugins/vcsMercurial/HgUtilities.py

Sun, 31 Dec 2017 16:52:09 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 31 Dec 2017 16:52:09 +0100
changeset 6048
82ad8ec9548c
parent 5624
cdd346d8858b
child 6062
926d2ad79e6d
permissions
-rw-r--r--

Updated copyright for 2018.

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
6048
82ad8ec9548c Updated copyright for 2018.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5624
diff changeset
3 # Copyright (c) 2010 - 2018 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
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2847
diff changeset
10 from __future__ import unicode_literals
5293
26f1e696a2a5 Fixed a Python 2 related issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5292
diff changeset
11 try:
5624
cdd346d8858b Removed a bunch of __IGNORE_WARNING_M131__ markers.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5587
diff changeset
12 str = unicode
5293
26f1e696a2a5 Fixed a Python 2 related issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5292
diff changeset
13 except NameError:
26f1e696a2a5 Fixed a Python 2 related issue.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 5292
diff changeset
14 pass
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2302
diff changeset
15
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 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
17 import re
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18
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
19 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
20
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 import Utilities
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
23
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 def getConfigPath():
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
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
26 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
27
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @return filename of the config file (string)
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 if Utilities.isWindowsPlatform():
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 userprofile = os.environ["USERPROFILE"]
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 return os.path.join(userprofile, "Mercurial.ini")
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 else:
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 homedir = Utilities.getHomeDir()
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 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
36
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
37
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
38 def prepareProcess(proc, encoding="", language=""):
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
39 """
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
40 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
41
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
42 @param proc reference to the proces to be prepared (QProcess)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
43 @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
44 @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
45 """
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
46 env = QProcessEnvironment.systemEnvironment()
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
47 env.insert("HGPLAIN", '1')
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
48
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
49 # set the encoding for the process
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
50 if encoding:
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
51 env.insert("HGENCODING", encoding)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
52
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
53 # set the language for the process
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
54 if language:
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
55 env.insert("LANGUAGE", language)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
56
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
57 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
58
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
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 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
61 """
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 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
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 @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
65 @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
66 @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
67 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
68 @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
69 """
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 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
71 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
72 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
73
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 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 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
96 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
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 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
99 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
100 "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
101 "The hg process finished with the exit code {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
102 .format(process.exitCode())
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 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
104 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
105 "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
106 "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
107 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
108 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
109 "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
110 "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
111
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
112 return versionStr, version, errorMsg

eric ide

mercurial