Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hhhrrrttt222111
GitHub Repository: hhhrrrttt222111/Dorkify
Path: blob/master/venv/Lib/site-packages/pip/_internal/vcs/mercurial.py
811 views
1
# The following comment should be removed at some point in the future.
2
# mypy: disallow-untyped-defs=False
3
4
from __future__ import absolute_import
5
6
import logging
7
import os
8
9
from pip._vendor.six.moves import configparser
10
11
from pip._internal.exceptions import BadCommand, InstallationError
12
from pip._internal.utils.misc import display_path
13
from pip._internal.utils.subprocess import make_command
14
from pip._internal.utils.temp_dir import TempDirectory
15
from pip._internal.utils.typing import MYPY_CHECK_RUNNING
16
from pip._internal.utils.urls import path_to_url
17
from pip._internal.vcs.versioncontrol import (
18
VersionControl,
19
find_path_to_setup_from_repo_root,
20
vcs,
21
)
22
23
if MYPY_CHECK_RUNNING:
24
from pip._internal.utils.misc import HiddenText
25
from pip._internal.vcs.versioncontrol import RevOptions
26
27
28
logger = logging.getLogger(__name__)
29
30
31
class Mercurial(VersionControl):
32
name = 'hg'
33
dirname = '.hg'
34
repo_name = 'clone'
35
schemes = (
36
'hg', 'hg+file', 'hg+http', 'hg+https', 'hg+ssh', 'hg+static-http',
37
)
38
39
@staticmethod
40
def get_base_rev_args(rev):
41
return [rev]
42
43
def export(self, location, url):
44
# type: (str, HiddenText) -> None
45
"""Export the Hg repository at the url to the destination location"""
46
with TempDirectory(kind="export") as temp_dir:
47
self.unpack(temp_dir.path, url=url)
48
49
self.run_command(
50
['archive', location], show_stdout=False, cwd=temp_dir.path
51
)
52
53
def fetch_new(self, dest, url, rev_options):
54
# type: (str, HiddenText, RevOptions) -> None
55
rev_display = rev_options.to_display()
56
logger.info(
57
'Cloning hg %s%s to %s',
58
url,
59
rev_display,
60
display_path(dest),
61
)
62
self.run_command(make_command('clone', '--noupdate', '-q', url, dest))
63
self.run_command(
64
make_command('update', '-q', rev_options.to_args()),
65
cwd=dest,
66
)
67
68
def switch(self, dest, url, rev_options):
69
# type: (str, HiddenText, RevOptions) -> None
70
repo_config = os.path.join(dest, self.dirname, 'hgrc')
71
config = configparser.RawConfigParser()
72
try:
73
config.read(repo_config)
74
config.set('paths', 'default', url.secret)
75
with open(repo_config, 'w') as config_file:
76
config.write(config_file)
77
except (OSError, configparser.NoSectionError) as exc:
78
logger.warning(
79
'Could not switch Mercurial repository to %s: %s', url, exc,
80
)
81
else:
82
cmd_args = make_command('update', '-q', rev_options.to_args())
83
self.run_command(cmd_args, cwd=dest)
84
85
def update(self, dest, url, rev_options):
86
# type: (str, HiddenText, RevOptions) -> None
87
self.run_command(['pull', '-q'], cwd=dest)
88
cmd_args = make_command('update', '-q', rev_options.to_args())
89
self.run_command(cmd_args, cwd=dest)
90
91
@classmethod
92
def get_remote_url(cls, location):
93
url = cls.run_command(
94
['showconfig', 'paths.default'],
95
show_stdout=False, cwd=location).strip()
96
if cls._is_local_repository(url):
97
url = path_to_url(url)
98
return url.strip()
99
100
@classmethod
101
def get_revision(cls, location):
102
"""
103
Return the repository-local changeset revision number, as an integer.
104
"""
105
current_revision = cls.run_command(
106
['parents', '--template={rev}'],
107
show_stdout=False, cwd=location).strip()
108
return current_revision
109
110
@classmethod
111
def get_requirement_revision(cls, location):
112
"""
113
Return the changeset identification hash, as a 40-character
114
hexadecimal string
115
"""
116
current_rev_hash = cls.run_command(
117
['parents', '--template={node}'],
118
show_stdout=False, cwd=location).strip()
119
return current_rev_hash
120
121
@classmethod
122
def is_commit_id_equal(cls, dest, name):
123
"""Always assume the versions don't match"""
124
return False
125
126
@classmethod
127
def get_subdirectory(cls, location):
128
"""
129
Return the path to setup.py, relative to the repo root.
130
Return None if setup.py is in the repo root.
131
"""
132
# find the repo root
133
repo_root = cls.run_command(
134
['root'], show_stdout=False, cwd=location).strip()
135
if not os.path.isabs(repo_root):
136
repo_root = os.path.abspath(os.path.join(location, repo_root))
137
return find_path_to_setup_from_repo_root(location, repo_root)
138
139
@classmethod
140
def get_repository_root(cls, location):
141
loc = super(Mercurial, cls).get_repository_root(location)
142
if loc:
143
return loc
144
try:
145
r = cls.run_command(
146
['root'],
147
cwd=location,
148
show_stdout=False,
149
on_returncode='raise',
150
log_failed_cmd=False,
151
)
152
except BadCommand:
153
logger.debug("could not determine if %s is under hg control "
154
"because hg is not available", location)
155
return None
156
except InstallationError:
157
return None
158
return os.path.normpath(r.rstrip('\r\n'))
159
160
161
vcs.register(Mercurial)
162
163