/bangkokhotel/lib/python2.5/site-packages/pip-1.2.1-py2.5.egg/pip/commands/install.py

https://bitbucket.org/luisrodriguez/bangkokhotel · Python · 290 lines · 264 code · 20 blank · 6 comment · 33 complexity · 1510eb7ec43e1ddd7763b4dfaf387337 MD5 · raw file

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