diff options
author | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
---|---|---|
committer | Shubham Saini <shubham6405@gmail.com> | 2018-12-11 10:01:23 +0000 |
commit | 68df54d6629ec019142eb149dd037774f2d11e7c (patch) | |
tree | 345bc22d46b4e01a4ba8303b94278952a4ed2b9e /venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/__init__.py |
First commit
Diffstat (limited to 'venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/__init__.py')
-rw-r--r-- | venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/__init__.py | 246 |
1 files changed, 246 insertions, 0 deletions
diff --git a/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/__init__.py b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/__init__.py new file mode 100644 index 0000000..d713b0d --- /dev/null +++ b/venv/lib/python3.7/site-packages/pip-10.0.1-py3.7.egg/pip/_internal/__init__.py | |||
@@ -0,0 +1,246 @@ | |||
1 | #!/usr/bin/env python | ||
2 | from __future__ import absolute_import | ||
3 | |||
4 | import locale | ||
5 | import logging | ||
6 | import os | ||
7 | import optparse | ||
8 | import warnings | ||
9 | |||
10 | import sys | ||
11 | |||
12 | # 2016-06-17 barry@debian.org: urllib3 1.14 added optional support for socks, | ||
13 | # but if invoked (i.e. imported), it will issue a warning to stderr if socks | ||
14 | # isn't available. requests unconditionally imports urllib3's socks contrib | ||
15 | # module, triggering this warning. The warning breaks DEP-8 tests (because of | ||
16 | # the stderr output) and is just plain annoying in normal usage. I don't want | ||
17 | # to add socks as yet another dependency for pip, nor do I want to allow-stder | ||
18 | # in the DEP-8 tests, so just suppress the warning. pdb tells me this has to | ||
19 | # be done before the import of pip.vcs. | ||
20 | from pip._vendor.urllib3.exceptions import DependencyWarning | ||
21 | warnings.filterwarnings("ignore", category=DependencyWarning) # noqa | ||
22 | |||
23 | # We want to inject the use of SecureTransport as early as possible so that any | ||
24 | # references or sessions or what have you are ensured to have it, however we | ||
25 | # only want to do this in the case that we're running on macOS and the linked | ||
26 | # OpenSSL is too old to handle TLSv1.2 | ||
27 | try: | ||
28 | import ssl | ||
29 | except ImportError: | ||
30 | pass | ||
31 | else: | ||
32 | # Checks for OpenSSL 1.0.1 on MacOS | ||
33 | if sys.platform == "darwin" and ssl.OPENSSL_VERSION_NUMBER < 0x1000100f: | ||
34 | try: | ||
35 | from pip._vendor.urllib3.contrib import securetransport | ||
36 | except (ImportError, OSError): | ||
37 | pass | ||
38 | else: | ||
39 | securetransport.inject_into_urllib3() | ||
40 | |||
41 | from pip import __version__ | ||
42 | from pip._internal import cmdoptions | ||
43 | from pip._internal.exceptions import CommandError, PipError | ||
44 | from pip._internal.utils.misc import get_installed_distributions, get_prog | ||
45 | from pip._internal.utils import deprecation | ||
46 | from pip._internal.vcs import git, mercurial, subversion, bazaar # noqa | ||
47 | from pip._internal.baseparser import ( | ||
48 | ConfigOptionParser, UpdatingDefaultsHelpFormatter, | ||
49 | ) | ||
50 | from pip._internal.commands import get_summaries, get_similar_commands | ||
51 | from pip._internal.commands import commands_dict | ||
52 | from pip._vendor.urllib3.exceptions import InsecureRequestWarning | ||
53 | |||
54 | logger = logging.getLogger(__name__) | ||
55 | |||
56 | # Hide the InsecureRequestWarning from urllib3 | ||
57 | warnings.filterwarnings("ignore", category=InsecureRequestWarning) | ||
58 | |||
59 | |||
60 | def autocomplete(): | ||
61 | """Command and option completion for the main option parser (and options) | ||
62 | and its subcommands (and options). | ||
63 | |||
64 | Enable by sourcing one of the completion shell scripts (bash, zsh or fish). | ||
65 | """ | ||
66 | # Don't complete if user hasn't sourced bash_completion file. | ||
67 | if 'PIP_AUTO_COMPLETE' not in os.environ: | ||
68 | return | ||
69 | cwords = os.environ['COMP_WORDS'].split()[1:] | ||
70 | cword = int(os.environ['COMP_CWORD']) | ||
71 | try: | ||
72 | current = cwords[cword - 1] | ||
73 | except IndexError: | ||
74 | current = '' | ||
75 | |||
76 | subcommands = [cmd for cmd, summary in get_summaries()] | ||
77 | options = [] | ||
78 | # subcommand | ||
79 | try: | ||
80 | subcommand_name = [w for w in cwords if w in subcommands][0] | ||
81 | except IndexError: | ||
82 | subcommand_name = None | ||
83 | |||
84 | parser = create_main_parser() | ||
85 | # subcommand options | ||
86 | if subcommand_name: | ||
87 | # special case: 'help' subcommand has no options | ||
88 | if subcommand_name == 'help': | ||
89 | sys.exit(1) | ||
90 | # special case: list locally installed dists for show and uninstall | ||
91 | should_list_installed = ( | ||
92 | subcommand_name in ['show', 'uninstall'] and | ||
93 | not current.startswith('-') | ||
94 | ) | ||
95 | if should_list_installed: | ||
96 | installed = [] | ||
97 | lc = current.lower() | ||
98 | for dist in get_installed_distributions(local_only=True): | ||
99 | if dist.key.startswith(lc) and dist.key not in cwords[1:]: | ||
100 | installed.append(dist.key) | ||
101 | # if there are no dists installed, fall back to option completion | ||
102 | if installed: | ||
103 | for dist in installed: | ||
104 | print(dist) | ||
105 | sys.exit(1) | ||
106 | |||
107 | subcommand = commands_dict[subcommand_name]() | ||
108 | |||
109 | for opt in subcommand.parser.option_list_all: | ||
110 | if opt.help != optparse.SUPPRESS_HELP: | ||
111 | for opt_str in opt._long_opts + opt._short_opts: | ||
112 | options.append((opt_str, opt.nargs)) | ||
113 | |||
114 | # filter out previously specified options from available options | ||
115 | prev_opts = [x.split('=')[0] for x in cwords[1:cword - 1]] | ||
116 | options = [(x, v) for (x, v) in options if x not in prev_opts] | ||
117 | # filter options by current input | ||
118 | options = [(k, v) for k, v in options if k.startswith(current)] | ||
119 | for option in options: | ||
120 | opt_label = option[0] | ||
121 | # append '=' to options which require args | ||
122 | if option[1] and option[0][:2] == "--": | ||
123 | opt_label += '=' | ||
124 | print(opt_label) | ||
125 | else: | ||
126 | # show main parser options only when necessary | ||
127 | if current.startswith('-') or current.startswith('--'): | ||
128 | opts = [i.option_list for i in parser.option_groups] | ||
129 | opts.append(parser.option_list) | ||
130 | opts = (o for it in opts for o in it) | ||
131 | |||
132 | for opt in opts: | ||
133 | if opt.help != optparse.SUPPRESS_HELP: | ||
134 | subcommands += opt._long_opts + opt._short_opts | ||
135 | |||
136 | print(' '.join([x for x in subcommands if x.startswith(current)])) | ||
137 | sys.exit(1) | ||
138 | |||
139 | |||
140 | def create_main_parser(): | ||
141 | parser_kw = { | ||
142 | 'usage': '\n%prog <command> [options]', | ||
143 | 'add_help_option': False, | ||
144 | 'formatter': UpdatingDefaultsHelpFormatter(), | ||
145 | 'name': 'global', | ||
146 | 'prog': get_prog(), | ||
147 | } | ||
148 | |||
149 | parser = ConfigOptionParser(**parser_kw) | ||
150 | parser.disable_interspersed_args() | ||
151 | |||
152 | pip_pkg_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
153 | parser.version = 'pip %s from %s (python %s)' % ( | ||
154 | __version__, pip_pkg_dir, sys.version[:3], | ||
155 | ) | ||
156 | |||
157 | # add the general options | ||
158 | gen_opts = cmdoptions.make_option_group(cmdoptions.general_group, parser) | ||
159 | parser.add_option_group(gen_opts) | ||
160 | |||
161 | parser.main = True # so the help formatter knows | ||
162 | |||
163 | # create command listing for description | ||
164 | command_summaries = get_summaries() | ||
165 | description = [''] + ['%-27s %s' % (i, j) for i, j in command_summaries] | ||
166 | parser.description = '\n'.join(description) | ||
167 | |||
168 | return parser | ||
169 | |||
170 | |||
171 | def parseopts(args): | ||
172 | parser = create_main_parser() | ||
173 | |||
174 | # Note: parser calls disable_interspersed_args(), so the result of this | ||
175 | # call is to split the initial args into the general options before the | ||
176 | # subcommand and everything else. | ||
177 | # For example: | ||
178 | # args: ['--timeout=5', 'install', '--user', 'INITools'] | ||
179 | # general_options: ['--timeout==5'] | ||
180 | # args_else: ['install', '--user', 'INITools'] | ||
181 | general_options, args_else = parser.parse_args(args) | ||
182 | |||
183 | # --version | ||
184 | if general_options.version: | ||
185 | sys.stdout.write(parser.version) | ||
186 | sys.stdout.write(os.linesep) | ||
187 | sys.exit() | ||
188 | |||
189 | # pip || pip help -> print_help() | ||
190 | if not args_else or (args_else[0] == 'help' and len(args_else) == 1): | ||
191 | parser.print_help() | ||
192 | sys.exit() | ||
193 | |||
194 | # the subcommand name | ||
195 | cmd_name = args_else[0] | ||
196 | |||
197 | if cmd_name not in commands_dict: | ||
198 | guess = get_similar_commands(cmd_name) | ||
199 | |||
200 | msg = ['unknown command "%s"' % cmd_name] | ||
201 | if guess: | ||
202 | msg.append('maybe you meant "%s"' % guess) | ||
203 | |||
204 | raise CommandError(' - '.join(msg)) | ||
205 | |||
206 | # all the args without the subcommand | ||
207 | cmd_args = args[:] | ||
208 | cmd_args.remove(cmd_name) | ||
209 | |||
210 | return cmd_name, cmd_args | ||
211 | |||
212 | |||
213 | def check_isolated(args): | ||
214 | isolated = False | ||
215 | |||
216 | if "--isolated" in args: | ||
217 | isolated = True | ||
218 | |||
219 | return isolated | ||
220 | |||
221 | |||
222 | def main(args=None): | ||
223 | if args is None: | ||
224 | args = sys.argv[1:] | ||
225 | |||
226 | # Configure our deprecation warnings to be sent through loggers | ||
227 | deprecation.install_warning_logger() | ||
228 | |||
229 | autocomplete() | ||
230 | |||
231 | try: | ||
232 | cmd_name, cmd_args = parseopts(args) | ||
233 | except PipError as exc: | ||
234 | sys.stderr.write("ERROR: %s" % exc) | ||
235 | sys.stderr.write(os.linesep) | ||
236 | sys.exit(1) | ||
237 | |||
238 | # Needed for locale.getpreferredencoding(False) to work | ||
239 | # in pip._internal.utils.encoding.auto_decode | ||
240 | try: | ||
241 | locale.setlocale(locale.LC_ALL, '') | ||
242 | except locale.Error as e: | ||
243 | # setlocale can apparently crash if locale are uninitialized | ||
244 | logger.debug("Ignoring error %s when setting locale", e) | ||
245 | command = commands_dict[cmd_name](isolated=check_isolated(cmd_args)) | ||
246 | return command.main(cmd_args) | ||