/bangkokhotel/lib/python2.5/site-packages/pip-1.2.1-py2.5.egg/pip/commands/install.py
Python | 290 lines | 264 code | 20 blank | 6 comment | 33 complexity | 1510eb7ec43e1ddd7763b4dfaf387337 MD5 | raw file
1import os
2import sys
3import tempfile
4import shutil
5from pip.req import InstallRequirement, RequirementSet
6from pip.req import parse_requirements
7from pip.log import logger
8from pip.locations import build_prefix, src_prefix, virtualenv_no_global
9from pip.basecommand import Command
10from pip.index import PackageFinder
11from pip.exceptions import InstallationError, CommandError
12from pip.backwardcompat import home_lib
13
14
15class InstallCommand(Command):
16 name = 'install'
17 usage = '%prog [OPTIONS] PACKAGE_NAMES...'
18 summary = 'Install packages'
19 bundle = False
20
21 def __init__(self):
22 super(InstallCommand, self).__init__()
23 self.parser.add_option(
24 '-e', '--editable',
25 dest='editables',
26 action='append',
27 default=[],
28 metavar='VCS+REPOS_URL[@REV]#egg=PACKAGE',
29 help='Install a package directly from a checkout. Source will be checked '
30 'out into src/PACKAGE (lower-case) and installed in-place (using '
31 'setup.py develop). You can run this on an existing directory/checkout (like '
32 'pip install -e src/mycheckout). This option may be provided multiple times. '
33 'Possible values for VCS are: svn, git, hg and bzr.')
34 self.parser.add_option(
35 '-r', '--requirement',
36 dest='requirements',
37 action='append',
38 default=[],
39 metavar='FILENAME',
40 help='Install all the packages listed in the given requirements file. '
41 'This option can be used multiple times.')
42 self.parser.add_option(
43 '-f', '--find-links',
44 dest='find_links',
45 action='append',
46 default=[],
47 metavar='URL',
48 help='URL to look for packages at')
49 self.parser.add_option(
50 '-i', '--index-url', '--pypi-url',
51 dest='index_url',
52 metavar='URL',
53 default='http://pypi.python.org/simple/',
54 help='Base URL of Python Package Index (default %default)')
55 self.parser.add_option(
56 '--extra-index-url',
57 dest='extra_index_urls',
58 metavar='URL',
59 action='append',
60 default=[],
61 help='Extra URLs of package indexes to use in addition to --index-url')
62 self.parser.add_option(
63 '--no-index',
64 dest='no_index',
65 action='store_true',
66 default=False,
67 help='Ignore package index (only looking at --find-links URLs instead)')
68 self.parser.add_option(
69 '-M', '--use-mirrors',
70 dest='use_mirrors',
71 action='store_true',
72 default=False,
73 help='Use the PyPI mirrors as a fallback in case the main index is down.')
74 self.parser.add_option(
75 '--mirrors',
76 dest='mirrors',
77 metavar='URL',
78 action='append',
79 default=[],
80 help='Specific mirror URLs to query when --use-mirrors is used')
81
82 self.parser.add_option(
83 '-b', '--build', '--build-dir', '--build-directory',
84 dest='build_dir',
85 metavar='DIR',
86 default=build_prefix,
87 help='Unpack packages into DIR (default %default) and build from there')
88 self.parser.add_option(
89 '-t', '--target',
90 dest='target_dir',
91 metavar='DIR',
92 default=None,
93 help='Install packages into DIR.')
94 self.parser.add_option(
95 '-d', '--download', '--download-dir', '--download-directory',
96 dest='download_dir',
97 metavar='DIR',
98 default=None,
99 help='Download packages into DIR instead of installing them')
100 self.parser.add_option(
101 '--download-cache',
102 dest='download_cache',
103 metavar='DIR',
104 default=None,
105 help='Cache downloaded packages in DIR')
106 self.parser.add_option(
107 '--src', '--source', '--source-dir', '--source-directory',
108 dest='src_dir',
109 metavar='DIR',
110 default=src_prefix,
111 help='Check out --editable packages into DIR (default %default)')
112
113 self.parser.add_option(
114 '-U', '--upgrade',
115 dest='upgrade',
116 action='store_true',
117 help='Upgrade all packages to the newest available version')
118 self.parser.add_option(
119 '--force-reinstall',
120 dest='force_reinstall',
121 action='store_true',
122 help='When upgrading, reinstall all packages even if they are '
123 'already up-to-date.')
124 self.parser.add_option(
125 '-I', '--ignore-installed',
126 dest='ignore_installed',
127 action='store_true',
128 help='Ignore the installed packages (reinstalling instead)')
129 self.parser.add_option(
130 '--no-deps', '--no-dependencies',
131 dest='ignore_dependencies',
132 action='store_true',
133 default=False,
134 help='Ignore package dependencies')
135 self.parser.add_option(
136 '--no-install',
137 dest='no_install',
138 action='store_true',
139 help="Download and unpack all packages, but don't actually install them")
140 self.parser.add_option(
141 '--no-download',
142 dest='no_download',
143 action="store_true",
144 help="Don't download any packages, just install the ones already downloaded "
145 "(completes an install run with --no-install)")
146
147 self.parser.add_option(
148 '--install-option',
149 dest='install_options',
150 action='append',
151 help="Extra arguments to be supplied to the setup.py install "
152 "command (use like --install-option=\"--install-scripts=/usr/local/bin\"). "
153 "Use multiple --install-option options to pass multiple options to setup.py install. "
154 "If you are using an option with a directory path, be sure to use absolute path.")
155
156 self.parser.add_option(
157 '--global-option',
158 dest='global_options',
159 action='append',
160 help="Extra global options to be supplied to the setup.py "
161 "call before the install command")
162
163 self.parser.add_option(
164 '--user',
165 dest='use_user_site',
166 action='store_true',
167 help='Install to user-site')
168
169 self.parser.add_option(
170 '--egg',
171 dest='as_egg',
172 action='store_true',
173 help="Install as self contained egg file, like easy_install does.")
174
175 def _build_package_finder(self, options, index_urls):
176 """
177 Create a package finder appropriate to this install command.
178 This method is meant to be overridden by subclasses, not
179 called directly.
180 """
181 return PackageFinder(find_links=options.find_links,
182 index_urls=index_urls,
183 use_mirrors=options.use_mirrors,
184 mirrors=options.mirrors)
185
186 def run(self, options, args):
187 if options.download_dir:
188 options.no_install = True
189 options.ignore_installed = True
190 options.build_dir = os.path.abspath(options.build_dir)
191 options.src_dir = os.path.abspath(options.src_dir)
192 install_options = options.install_options or []
193 if options.use_user_site:
194 if virtualenv_no_global():
195 raise InstallationError("Can not perform a '--user' install. User site-packages are not visible in this virtualenv.")
196 install_options.append('--user')
197 if options.target_dir:
198 options.ignore_installed = True
199 temp_target_dir = tempfile.mkdtemp()
200 options.target_dir = os.path.abspath(options.target_dir)
201 if os.path.exists(options.target_dir) and not os.path.isdir(options.target_dir):
202 raise CommandError("Target path exists but is not a directory, will not continue.")
203 install_options.append('--home=' + temp_target_dir)
204 global_options = options.global_options or []
205 index_urls = [options.index_url] + options.extra_index_urls
206 if options.no_index:
207 logger.notify('Ignoring indexes: %s' % ','.join(index_urls))
208 index_urls = []
209
210 finder = self._build_package_finder(options, index_urls)
211
212 requirement_set = RequirementSet(
213 build_dir=options.build_dir,
214 src_dir=options.src_dir,
215 download_dir=options.download_dir,
216 download_cache=options.download_cache,
217 upgrade=options.upgrade,
218 as_egg=options.as_egg,
219 ignore_installed=options.ignore_installed,
220 ignore_dependencies=options.ignore_dependencies,
221 force_reinstall=options.force_reinstall,
222 use_user_site=options.use_user_site)
223 for name in args:
224 requirement_set.add_requirement(
225 InstallRequirement.from_line(name, None))
226 for name in options.editables:
227 requirement_set.add_requirement(
228 InstallRequirement.from_editable(name, default_vcs=options.default_vcs))
229 for filename in options.requirements:
230 for req in parse_requirements(filename, finder=finder, options=options):
231 requirement_set.add_requirement(req)
232 if not requirement_set.has_requirements:
233 opts = {'name': self.name}
234 if options.find_links:
235 msg = ('You must give at least one requirement to %(name)s '
236 '(maybe you meant "pip %(name)s %(links)s"?)' %
237 dict(opts, links=' '.join(options.find_links)))
238 else:
239 msg = ('You must give at least one requirement '
240 'to %(name)s (see "pip help %(name)s")' % opts)
241 logger.warn(msg)
242 return
243
244 if (options.use_user_site and
245 sys.version_info < (2, 6)):
246 raise InstallationError('--user is only supported in Python version 2.6 and newer')
247
248 import setuptools
249 if (options.use_user_site and
250 requirement_set.has_editables and
251 not getattr(setuptools, '_distribute', False)):
252
253 raise InstallationError('--user --editable not supported with setuptools, use distribute')
254
255 if not options.no_download:
256 requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
257 else:
258 requirement_set.locate_files()
259
260 if not options.no_install and not self.bundle:
261 requirement_set.install(install_options, global_options)
262 installed = ' '.join([req.name for req in
263 requirement_set.successfully_installed])
264 if installed:
265 logger.notify('Successfully installed %s' % installed)
266 elif not self.bundle:
267 downloaded = ' '.join([req.name for req in
268 requirement_set.successfully_downloaded])
269 if downloaded:
270 logger.notify('Successfully downloaded %s' % downloaded)
271 elif self.bundle:
272 requirement_set.create_bundle(self.bundle_filename)
273 logger.notify('Created bundle in %s' % self.bundle_filename)
274 # Clean up
275 if not options.no_install or options.download_dir:
276 requirement_set.cleanup_files(bundle=self.bundle)
277 if options.target_dir:
278 if not os.path.exists(options.target_dir):
279 os.makedirs(options.target_dir)
280 lib_dir = home_lib(temp_target_dir)
281 for item in os.listdir(lib_dir):
282 shutil.move(
283 os.path.join(lib_dir, item),
284 os.path.join(options.target_dir, item)
285 )
286 shutil.rmtree(temp_target_dir)
287 return requirement_set
288
289
290InstallCommand()