eric6/Plugins/VcsPlugins/vcsMercurial/HgUtilities.py

Sat, 21 Sep 2019 22:03:03 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 21 Sep 2019 22:03:03 +0200
changeset 7257
c4d0cac9b5c9
parent 7229
53054eb5b15a
child 7360
9190402e4505
permissions
-rw-r--r--

Continued to resolve code style issue M841.

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
6645
ad476851d7e0 Updated copyright for 2019.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6062
diff changeset
3 # Copyright (c) 2010 - 2019 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
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
10
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 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
12 import re
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13
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
14 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
15
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 import Utilities
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
945
8cd4d08fa9f6 Made code mostly PEP 8 compliant (except all whitespace and line length).
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 791
diff changeset
18
178
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 def getConfigPath():
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 """
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
21 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
22
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 @return filename of the config file (string)
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 if Utilities.isWindowsPlatform():
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 userprofile = os.environ["USERPROFILE"]
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 return os.path.join(userprofile, "Mercurial.ini")
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 else:
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 homedir = Utilities.getHomeDir()
dd9f0bca5e2f Added plugin for Mercurial version control system.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 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
31
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
32
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
33 def prepareProcess(proc, encoding="", language=""):
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
34 """
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
35 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
36
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
37 @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
38 @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
39 @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
40 """
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
41 env = QProcessEnvironment.systemEnvironment()
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
42 env.insert("HGPLAIN", '1')
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
43
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
44 # set the encoding for the process
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
45 if encoding:
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
46 env.insert("HGENCODING", encoding)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
47
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
48 # set the language for the process
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
49 if language:
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
50 env.insert("LANGUAGE", language)
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
51
05aab5164d64 A little optimization for the Mercurial interface.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 2302
diff changeset
52 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
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
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 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
56 """
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 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
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 @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
60 @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
61 @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
62 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
63 @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
64 """
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 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
66 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
67 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
68
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 = ["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
70 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
71 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
72 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
73 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
74 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
75 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
76 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
77 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
78 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
79 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
80 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 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
95 "HgUtilities",
7257
c4d0cac9b5c9 Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7229
diff changeset
96 "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
97 ).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
98 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
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 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
102 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
103 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
104 "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
105 "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
106
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 return versionStr, version, errorMsg

eric ide

mercurial