comparison venv/lib/python2.7/site-packages/pip/commands/install.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 from __future__ import absolute_import
2
3 import logging
4 import operator
5 import os
6 import tempfile
7 import shutil
8 import warnings
9
10 from pip.req import InstallRequirement, RequirementSet, parse_requirements
11 from pip.locations import build_prefix, virtualenv_no_global, distutils_scheme
12 from pip.basecommand import Command
13 from pip.index import PackageFinder
14 from pip.exceptions import (
15 InstallationError, CommandError, PreviousBuildDirError,
16 )
17 from pip import cmdoptions
18 from pip.utils.build import BuildDirectory
19 from pip.utils.deprecation import RemovedInPip7Warning, RemovedInPip8Warning
20
21
22 logger = logging.getLogger(__name__)
23
24
25 class InstallCommand(Command):
26 """
27 Install packages from:
28
29 - PyPI (and other indexes) using requirement specifiers.
30 - VCS project urls.
31 - Local project directories.
32 - Local or remote source archives.
33
34 pip also supports installing from "requirements files", which provide
35 an easy way to specify a whole environment to be installed.
36 """
37 name = 'install'
38
39 usage = """
40 %prog [options] <requirement specifier> [package-index-options] ...
41 %prog [options] -r <requirements file> [package-index-options] ...
42 %prog [options] [-e] <vcs project url> ...
43 %prog [options] [-e] <local project path> ...
44 %prog [options] <archive url/path> ..."""
45
46 summary = 'Install packages.'
47
48 def __init__(self, *args, **kw):
49 super(InstallCommand, self).__init__(*args, **kw)
50
51 cmd_opts = self.cmd_opts
52
53 cmd_opts.add_option(cmdoptions.editable.make())
54 cmd_opts.add_option(cmdoptions.requirements.make())
55 cmd_opts.add_option(cmdoptions.build_dir.make())
56
57 cmd_opts.add_option(
58 '-t', '--target',
59 dest='target_dir',
60 metavar='dir',
61 default=None,
62 help='Install packages into <dir>. '
63 'By default this will not replace existing files/folders in '
64 '<dir>. Use --upgrade to replace existing packages in <dir> '
65 'with new versions.'
66 )
67
68 cmd_opts.add_option(
69 '-d', '--download', '--download-dir', '--download-directory',
70 dest='download_dir',
71 metavar='dir',
72 default=None,
73 help=("Download packages into <dir> instead of installing them, "
74 "regardless of what's already installed."),
75 )
76
77 cmd_opts.add_option(cmdoptions.download_cache.make())
78 cmd_opts.add_option(cmdoptions.src.make())
79
80 cmd_opts.add_option(
81 '-U', '--upgrade',
82 dest='upgrade',
83 action='store_true',
84 help='Upgrade all specified packages to the newest available '
85 'version. This process is recursive regardless of whether '
86 'a dependency is already satisfied.'
87 )
88
89 cmd_opts.add_option(
90 '--force-reinstall',
91 dest='force_reinstall',
92 action='store_true',
93 help='When upgrading, reinstall all packages even if they are '
94 'already up-to-date.')
95
96 cmd_opts.add_option(
97 '-I', '--ignore-installed',
98 dest='ignore_installed',
99 action='store_true',
100 help='Ignore the installed packages (reinstalling instead).')
101
102 cmd_opts.add_option(cmdoptions.no_deps.make())
103
104 cmd_opts.add_option(
105 '--no-install',
106 dest='no_install',
107 action='store_true',
108 help="DEPRECATED. Download and unpack all packages, but don't "
109 "actually install them."
110 )
111
112 cmd_opts.add_option(
113 '--no-download',
114 dest='no_download',
115 action="store_true",
116 help="DEPRECATED. Don't download any packages, just install the "
117 "ones already downloaded (completes an install run with "
118 "--no-install).")
119
120 cmd_opts.add_option(cmdoptions.install_options.make())
121 cmd_opts.add_option(cmdoptions.global_options.make())
122
123 cmd_opts.add_option(
124 '--user',
125 dest='use_user_site',
126 action='store_true',
127 help="Install to the Python user install directory for your "
128 "platform. Typically ~/.local/, or %APPDATA%\Python on "
129 "Windows. (See the Python documentation for site.USER_BASE "
130 "for full details.)")
131
132 cmd_opts.add_option(
133 '--egg',
134 dest='as_egg',
135 action='store_true',
136 help="Install packages as eggs, not 'flat', like pip normally "
137 "does. This option is not about installing *from* eggs. "
138 "(WARNING: Because this option overrides pip's normal install"
139 " logic, requirements files may not behave as expected.)")
140
141 cmd_opts.add_option(
142 '--root',
143 dest='root_path',
144 metavar='dir',
145 default=None,
146 help="Install everything relative to this alternate root "
147 "directory.")
148
149 cmd_opts.add_option(
150 "--compile",
151 action="store_true",
152 dest="compile",
153 default=True,
154 help="Compile py files to pyc",
155 )
156
157 cmd_opts.add_option(
158 "--no-compile",
159 action="store_false",
160 dest="compile",
161 help="Do not compile py files to pyc",
162 )
163
164 cmd_opts.add_option(cmdoptions.use_wheel.make())
165 cmd_opts.add_option(cmdoptions.no_use_wheel.make())
166
167 cmd_opts.add_option(
168 '--pre',
169 action='store_true',
170 default=False,
171 help="Include pre-release and development versions. By default, "
172 "pip only finds stable versions.")
173
174 cmd_opts.add_option(cmdoptions.no_clean.make())
175
176 index_opts = cmdoptions.make_option_group(
177 cmdoptions.index_group,
178 self.parser,
179 )
180
181 self.parser.insert_option_group(0, index_opts)
182 self.parser.insert_option_group(0, cmd_opts)
183
184 def _build_package_finder(self, options, index_urls, session):
185 """
186 Create a package finder appropriate to this install command.
187 This method is meant to be overridden by subclasses, not
188 called directly.
189 """
190 return PackageFinder(
191 find_links=options.find_links,
192 index_urls=index_urls,
193 use_wheel=options.use_wheel,
194 allow_external=options.allow_external,
195 allow_unverified=options.allow_unverified,
196 allow_all_external=options.allow_all_external,
197 trusted_hosts=options.trusted_hosts,
198 allow_all_prereleases=options.pre,
199 process_dependency_links=options.process_dependency_links,
200 session=session,
201 )
202
203 def run(self, options, args):
204
205 if (
206 options.no_install or
207 options.no_download
208 ):
209 warnings.warn(
210 "--no-install and --no-download are deprecated. "
211 "See https://github.com/pypa/pip/issues/906.",
212 RemovedInPip7Warning,
213 )
214
215 # If we have --no-install or --no-download and no --build we use the
216 # legacy static build dir
217 if (options.build_dir is None and
218 (options.no_install or options.no_download)):
219 options.build_dir = build_prefix
220
221 if options.download_dir:
222 options.no_install = True
223 options.ignore_installed = True
224
225 if options.build_dir:
226 options.build_dir = os.path.abspath(options.build_dir)
227
228 options.src_dir = os.path.abspath(options.src_dir)
229 install_options = options.install_options or []
230 if options.use_user_site:
231 if virtualenv_no_global():
232 raise InstallationError(
233 "Can not perform a '--user' install. User site-packages "
234 "are not visible in this virtualenv."
235 )
236 install_options.append('--user')
237
238 temp_target_dir = None
239 if options.target_dir:
240 options.ignore_installed = True
241 temp_target_dir = tempfile.mkdtemp()
242 options.target_dir = os.path.abspath(options.target_dir)
243 if (os.path.exists(options.target_dir) and not
244 os.path.isdir(options.target_dir)):
245 raise CommandError(
246 "Target path exists but is not a directory, will not "
247 "continue."
248 )
249 install_options.append('--home=' + temp_target_dir)
250
251 global_options = options.global_options or []
252 index_urls = [options.index_url] + options.extra_index_urls
253 if options.no_index:
254 logger.info('Ignoring indexes: %s', ','.join(index_urls))
255 index_urls = []
256
257 if options.use_mirrors:
258 warnings.warn(
259 "--use-mirrors has been deprecated and will be removed in the "
260 "future. Explicit uses of --index-url and/or --extra-index-url"
261 " is suggested.",
262 RemovedInPip7Warning,
263 )
264
265 if options.mirrors:
266 warnings.warn(
267 "--mirrors has been deprecated and will be removed in the "
268 "future. Explicit uses of --index-url and/or --extra-index-url"
269 " is suggested.",
270 RemovedInPip7Warning,
271 )
272 index_urls += options.mirrors
273
274 if options.download_cache:
275 warnings.warn(
276 "--download-cache has been deprecated and will be removed in "
277 "the future. Pip now automatically uses and configures its "
278 "cache.",
279 RemovedInPip8Warning,
280 )
281
282 with self._build_session(options) as session:
283
284 finder = self._build_package_finder(options, index_urls, session)
285
286 build_delete = (not (options.no_clean or options.build_dir))
287 with BuildDirectory(options.build_dir,
288 delete=build_delete) as build_dir:
289 requirement_set = RequirementSet(
290 build_dir=build_dir,
291 src_dir=options.src_dir,
292 download_dir=options.download_dir,
293 upgrade=options.upgrade,
294 as_egg=options.as_egg,
295 ignore_installed=options.ignore_installed,
296 ignore_dependencies=options.ignore_dependencies,
297 force_reinstall=options.force_reinstall,
298 use_user_site=options.use_user_site,
299 target_dir=temp_target_dir,
300 session=session,
301 pycompile=options.compile,
302 isolated=options.isolated_mode,
303 )
304
305 for name in args:
306 requirement_set.add_requirement(
307 InstallRequirement.from_line(
308 name, None, isolated=options.isolated_mode,
309 )
310 )
311
312 for name in options.editables:
313 requirement_set.add_requirement(
314 InstallRequirement.from_editable(
315 name,
316 default_vcs=options.default_vcs,
317 isolated=options.isolated_mode,
318 )
319 )
320
321 for filename in options.requirements:
322 for req in parse_requirements(
323 filename,
324 finder=finder, options=options, session=session):
325 requirement_set.add_requirement(req)
326
327 if not requirement_set.has_requirements:
328 opts = {'name': self.name}
329 if options.find_links:
330 msg = ('You must give at least one requirement to '
331 '%(name)s (maybe you meant "pip %(name)s '
332 '%(links)s"?)' %
333 dict(opts, links=' '.join(options.find_links)))
334 else:
335 msg = ('You must give at least one requirement '
336 'to %(name)s (see "pip help %(name)s")' % opts)
337 logger.warning(msg)
338 return
339
340 try:
341 if not options.no_download:
342 requirement_set.prepare_files(finder)
343 else:
344 # This is the only call site of locate_files. Nuke with
345 # fire.
346 requirement_set.locate_files()
347
348 if not options.no_install:
349 requirement_set.install(
350 install_options,
351 global_options,
352 root=options.root_path,
353 )
354 reqs = sorted(
355 requirement_set.successfully_installed,
356 key=operator.attrgetter('name'))
357 items = []
358 for req in reqs:
359 item = req.name
360 try:
361 if hasattr(req, 'installed_version'):
362 if req.installed_version:
363 item += '-' + req.installed_version
364 except Exception:
365 pass
366 items.append(item)
367 installed = ' '.join(items)
368 if installed:
369 logger.info('Successfully installed %s', installed)
370 else:
371 downloaded = ' '.join([
372 req.name
373 for req in requirement_set.successfully_downloaded
374 ])
375 if downloaded:
376 logger.info(
377 'Successfully downloaded %s', downloaded
378 )
379 except PreviousBuildDirError:
380 options.no_clean = True
381 raise
382 finally:
383 # Clean up
384 if ((not options.no_clean) and
385 ((not options.no_install) or
386 options.download_dir)):
387 requirement_set.cleanup_files()
388
389 if options.target_dir:
390 if not os.path.exists(options.target_dir):
391 os.makedirs(options.target_dir)
392
393 lib_dir = distutils_scheme('', home=temp_target_dir)['purelib']
394
395 for item in os.listdir(lib_dir):
396 target_item_dir = os.path.join(options.target_dir, item)
397 if os.path.exists(target_item_dir):
398 if not options.upgrade:
399 logger.warning(
400 'Target directory %s already exists. Specify '
401 '--upgrade to force replacement.',
402 target_item_dir
403 )
404 continue
405 if os.path.islink(target_item_dir):
406 logger.warning(
407 'Target directory %s already exists and is '
408 'a link. Pip will not automatically replace '
409 'links, please remove if replacement is '
410 'desired.',
411 target_item_dir
412 )
413 continue
414 if os.path.isdir(target_item_dir):
415 shutil.rmtree(target_item_dir)
416 else:
417 os.remove(target_item_dir)
418
419 shutil.move(
420 os.path.join(lib_dir, item),
421 target_item_dir
422 )
423 shutil.rmtree(temp_target_dir)
424 return requirement_set