2011-05-04 09:44:02 +02:00
import os
import sys
2012-02-03 04:23:07 +01:00
import tempfile
import shutil
2013-02-16 19:02:41 +01:00
from pip . req import InstallRequirement , RequirementSet , parse_requirements
2009-11-20 19:20:52 +01:00
from pip . log import logger
2013-04-19 07:38:56 +02:00
from pip . locations import src_prefix , virtualenv_no_global , distutils_scheme
2009-11-20 19:20:52 +01:00
from pip . basecommand import Command
2009-11-21 00:39:44 +01:00
from pip . index import PackageFinder
2013-07-24 11:02:08 +02:00
from pip . exceptions import InstallationError , CommandError , PreviousBuildDirError
2013-02-03 07:40:30 +01:00
from pip import cmdoptions
2009-11-20 19:20:52 +01:00
2010-06-03 04:25:26 +02:00
2009-11-20 19:20:52 +01:00
class InstallCommand ( Command ) :
2013-02-16 19:00:29 +01:00
"""
Install packages from :
- PyPI ( and other indexes ) using requirement specifiers .
- VCS project urls .
- Local project directories .
- Local or remote source archives .
pip also supports installing from " requirements files " , which provide
an easy way to specify a whole environment to be installed .
See http : / / www . pip - installer . org for details on VCS url formats and
requirements files .
"""
2009-11-20 19:20:52 +01:00
name = ' install '
2013-01-18 22:25:15 +01:00
usage = """
% prog [ options ] < requirement specifier > . . .
% prog [ options ] - r < requirements file > . . .
% prog [ options ] [ - e ] < vcs project url > . . .
% prog [ options ] [ - e ] < local project path > . . .
% prog [ options ] < archive url / path > . . . """
summary = ' Install packages. '
2009-11-20 19:20:52 +01:00
bundle = False
2012-11-10 05:15:45 +01:00
def __init__ ( self , * args , * * kw ) :
super ( InstallCommand , self ) . __init__ ( * args , * * kw )
2012-12-10 02:14:39 +01:00
2012-12-10 02:22:28 +01:00
cmd_opts = self . cmd_opts
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2009-11-20 19:20:52 +01:00
' -e ' , ' --editable ' ,
dest = ' editables ' ,
action = ' append ' ,
default = [ ] ,
2013-01-18 22:25:15 +01:00
metavar = ' path/url ' ,
help = ' Install a project in editable mode (i.e. setuptools " develop mode " ) from a local project path or a VCS url. ' )
2012-12-10 02:14:39 +01:00
2013-09-17 07:21:15 +02:00
cmd_opts . add_option ( cmdoptions . requirements . make ( ) )
cmd_opts . add_option ( cmdoptions . build_dir . make ( ) )
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2012-02-03 12:22:51 +01:00
' -t ' , ' --target ' ,
2012-02-03 04:23:07 +01:00
dest = ' target_dir ' ,
2013-01-18 22:25:15 +01:00
metavar = ' dir ' ,
2012-02-03 04:23:07 +01:00
default = None ,
2013-01-18 22:25:15 +01:00
help = ' Install packages into <dir>. ' )
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2009-11-20 19:20:52 +01:00
' -d ' , ' --download ' , ' --download-dir ' , ' --download-directory ' ,
dest = ' download_dir ' ,
2013-01-18 22:25:15 +01:00
metavar = ' dir ' ,
2009-11-20 19:20:52 +01:00
default = None ,
2013-04-19 23:30:56 +02:00
help = " Download packages into <dir> instead of installing them, regardless of what ' s already installed. " )
2012-12-10 02:14:39 +01:00
2013-09-17 07:21:15 +02:00
cmd_opts . add_option ( cmdoptions . download_cache . make ( ) )
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2009-11-20 19:20:52 +01:00
' --src ' , ' --source ' , ' --source-dir ' , ' --source-directory ' ,
dest = ' src_dir ' ,
2013-01-18 22:25:15 +01:00
metavar = ' dir ' ,
2011-08-22 19:38:46 +02:00
default = src_prefix ,
2013-01-18 22:25:15 +01:00
help = ' Directory to check out editable projects into. '
' The default in a virtualenv is " <venv path>/src " . '
' The default for global installs is " <current dir>/src " . ' )
2009-11-20 19:20:52 +01:00
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2009-11-20 19:20:52 +01:00
' -U ' , ' --upgrade ' ,
dest = ' upgrade ' ,
action = ' store_true ' ,
2013-01-18 22:25:15 +01:00
help = ' Upgrade all packages to the newest available version. '
2013-04-19 23:30:56 +02:00
' This process is recursive regardless of whether a dependency is already satisfied. ' )
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2011-08-24 20:07:16 +02:00
' --force-reinstall ' ,
dest = ' force_reinstall ' ,
action = ' store_true ' ,
help = ' When upgrading, reinstall all packages even if they are '
' already up-to-date. ' )
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2009-11-20 19:20:52 +01:00
' -I ' , ' --ignore-installed ' ,
dest = ' ignore_installed ' ,
action = ' store_true ' ,
2013-01-18 22:25:15 +01:00
help = ' Ignore the installed packages (reinstalling instead). ' )
2012-12-10 02:14:39 +01:00
2013-09-17 07:21:15 +02:00
cmd_opts . add_option ( cmdoptions . no_deps . make ( ) )
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2009-11-20 19:20:52 +01:00
' --no-install ' ,
dest = ' no_install ' ,
action = ' store_true ' ,
2013-01-18 22:25:15 +01:00
help = " Download and unpack all packages, but don ' t actually install them. " )
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2010-04-15 14:03:36 +02:00
' --no-download ' ,
dest = ' no_download ' ,
action = " store_true " ,
help = " Don ' t download any packages, just install the ones already downloaded "
2013-01-18 22:25:15 +01:00
" (completes an install run with --no-install). " )
2009-11-20 19:20:52 +01:00
2013-09-17 07:21:15 +02:00
cmd_opts . add_option ( cmdoptions . install_options . make ( ) )
cmd_opts . add_option ( cmdoptions . global_options . make ( ) )
2010-05-30 14:39:11 +02:00
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2010-08-18 23:59:33 +02:00
' --user ' ,
dest = ' use_user_site ' ,
action = ' store_true ' ,
2013-01-18 22:25:15 +01:00
help = ' Install using the user scheme. ' )
2010-08-18 23:59:33 +02:00
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2012-05-20 05:18:36 +02:00
' --egg ' ,
dest = ' as_egg ' ,
action = ' store_true ' ,
2013-11-16 01:41:38 +01:00
help = " Install packages as eggs, not ' flat ' , like pip normally does. This option is not about installing *from* eggs. (WARNING: Because this option overrides pip ' s normal install logic, requirements files may not behave as expected.) " )
2012-05-20 05:18:36 +02:00
2012-12-10 02:14:39 +01:00
cmd_opts . add_option (
2012-10-01 10:27:35 +02:00
' --root ' ,
dest = ' root_path ' ,
2013-01-18 22:25:15 +01:00
metavar = ' dir ' ,
2012-10-01 10:27:35 +02:00
default = None ,
2013-01-18 22:25:15 +01:00
help = " Install everything relative to this alternate root directory. " )
2012-10-01 10:27:35 +02:00
2013-11-22 03:59:31 +01:00
cmd_opts . add_option (
" --compile " ,
action = " store_true " ,
dest = " compile " ,
default = True ,
help = " Compile py files to pyc " ,
)
cmd_opts . add_option (
" --no-compile " ,
action = " store_false " ,
dest = " compile " ,
help = " Do not compile py files to pyc " ,
)
2013-09-17 07:21:15 +02:00
cmd_opts . add_option ( cmdoptions . use_wheel . make ( ) )
2013-11-02 18:47:11 +01:00
cmd_opts . add_option ( cmdoptions . no_use_wheel . make ( ) )
2013-02-03 07:40:30 +01:00
2013-03-09 15:19:15 +01:00
cmd_opts . add_option (
' --pre ' ,
action = ' store_true ' ,
default = False ,
2013-03-12 05:56:36 +01:00
help = " Include pre-release and development versions. By default, pip only finds stable versions. " )
2013-03-09 15:19:15 +01:00
2013-09-17 07:21:15 +02:00
cmd_opts . add_option ( cmdoptions . no_clean . make ( ) )
2013-03-27 06:26:52 +01:00
2013-02-03 07:40:30 +01:00
index_opts = cmdoptions . make_option_group ( cmdoptions . index_group , self . parser )
2012-12-16 05:26:21 +01:00
self . parser . insert_option_group ( 0 , index_opts )
2012-12-12 14:06:05 +01:00
self . parser . insert_option_group ( 0 , cmd_opts )
2012-10-01 10:27:35 +02:00
2013-08-16 14:04:27 +02:00
def _build_package_finder ( self , options , index_urls , session ) :
2010-05-28 01:27:13 +02:00
"""
Create a package finder appropriate to this install command .
This method is meant to be overridden by subclasses , not
called directly .
"""
2010-08-16 01:46:23 +02:00
return PackageFinder ( find_links = options . find_links ,
index_urls = index_urls ,
2013-06-02 19:03:56 +02:00
use_wheel = options . use_wheel ,
allow_external = options . allow_external ,
2013-10-27 04:47:57 +01:00
allow_unverified = options . allow_unverified ,
2013-06-07 15:48:34 +02:00
allow_all_external = options . allow_all_external ,
2013-07-11 18:21:30 +02:00
allow_all_prereleases = options . pre ,
2013-10-27 23:10:37 +01:00
process_dependency_links =
options . process_dependency_links ,
2013-08-16 14:04:27 +02:00
session = session ,
2013-06-02 19:03:56 +02:00
)
2010-05-28 01:27:13 +02:00
2009-11-20 19:20:52 +01:00
def run ( self , options , args ) :
if options . download_dir :
options . no_install = True
options . ignore_installed = True
2010-04-15 13:16:11 +02:00
options . build_dir = os . path . abspath ( options . build_dir )
options . src_dir = os . path . abspath ( options . src_dir )
2009-11-20 19:20:52 +01:00
install_options = options . install_options or [ ]
2010-08-18 23:59:33 +02:00
if options . use_user_site :
2012-06-06 05:01:35 +02:00
if virtualenv_no_global ( ) :
raise InstallationError ( " Can not perform a ' --user ' install. User site-packages are not visible in this virtualenv. " )
2010-08-18 23:59:33 +02:00
install_options . append ( ' --user ' )
2012-11-15 01:53:22 +01:00
temp_target_dir = None
2012-02-03 04:23:07 +01:00
if options . target_dir :
2012-02-07 08:13:02 +01:00
options . ignore_installed = True
2012-02-03 04:23:07 +01:00
temp_target_dir = tempfile . mkdtemp ( )
options . target_dir = os . path . abspath ( options . target_dir )
if os . path . exists ( options . target_dir ) and not os . path . isdir ( options . target_dir ) :
raise CommandError ( " Target path exists but is not a directory, will not continue. " )
install_options . append ( ' --home= ' + temp_target_dir )
2012-11-15 01:53:22 +01:00
2010-05-30 14:39:11 +02:00
global_options = options . global_options or [ ]
2009-11-20 19:20:52 +01:00
index_urls = [ options . index_url ] + options . extra_index_urls
if options . no_index :
logger . notify ( ' Ignoring indexes: %s ' % ' , ' . join ( index_urls ) )
index_urls = [ ]
2010-05-28 01:27:13 +02:00
2013-07-30 12:26:13 +02:00
if options . use_mirrors :
2013-08-05 05:17:04 +02:00
logger . deprecated ( " 1.7 " ,
" --use-mirrors has been deprecated and will be removed "
2013-07-30 12:26:13 +02:00
" in the future. Explicit uses of --index-url and/or "
" --extra-index-url is suggested. " )
if options . mirrors :
2013-08-05 05:17:04 +02:00
logger . deprecated ( " 1.7 " ,
" --mirrors has been deprecated and will be removed in "
2013-07-30 12:26:13 +02:00
" the future. Explicit uses of --index-url and/or "
" --extra-index-url is suggested. " )
index_urls + = options . mirrors
2013-08-16 14:04:27 +02:00
session = self . _build_session ( options )
finder = self . _build_package_finder ( options , index_urls , session )
2010-05-28 01:27:13 +02:00
2009-11-21 01:26:59 +01:00
requirement_set = RequirementSet (
2009-11-20 19:20:52 +01:00
build_dir = options . build_dir ,
src_dir = options . src_dir ,
download_dir = options . download_dir ,
download_cache = options . download_cache ,
upgrade = options . upgrade ,
2012-05-20 05:18:36 +02:00
as_egg = options . as_egg ,
2009-11-20 19:20:52 +01:00
ignore_installed = options . ignore_installed ,
2011-08-24 20:07:16 +02:00
ignore_dependencies = options . ignore_dependencies ,
2012-06-11 03:16:48 +02:00
force_reinstall = options . force_reinstall ,
2012-11-15 01:53:22 +01:00
use_user_site = options . use_user_site ,
2013-08-18 09:54:47 +02:00
target_dir = temp_target_dir ,
session = session ,
2013-11-22 03:59:31 +01:00
pycompile = options . compile ,
2013-08-18 09:54:47 +02:00
)
2009-11-20 19:20:52 +01:00
for name in args :
requirement_set . add_requirement (
2013-07-11 18:21:30 +02:00
InstallRequirement . from_line ( name , None ) )
2009-11-20 19:20:52 +01:00
for name in options . editables :
requirement_set . add_requirement (
2009-11-21 01:26:59 +01:00
InstallRequirement . from_editable ( name , default_vcs = options . default_vcs ) )
2009-11-20 19:20:52 +01:00
for filename in options . requirements :
2013-08-16 14:04:27 +02:00
for req in parse_requirements ( filename , finder = finder , options = options , session = session ) :
2009-11-20 19:20:52 +01:00
requirement_set . add_requirement ( req )
2010-08-31 03:01:03 +02:00
if not requirement_set . has_requirements :
2011-08-25 21:08:17 +02:00
opts = { ' name ' : self . name }
2010-08-31 03:01:03 +02:00
if options . find_links :
2011-08-25 21:08:17 +02:00
msg = ( ' You must give at least one requirement to %(name)s '
' (maybe you meant " pip %(name)s %(links)s " ?) ' %
dict ( opts , links = ' ' . join ( options . find_links ) ) )
else :
msg = ( ' You must give at least one requirement '
' to %(name)s (see " pip help %(name)s " ) ' % opts )
logger . warn ( msg )
return
2010-08-31 03:01:03 +02:00
2013-03-27 06:26:52 +01:00
try :
if not options . no_download :
requirement_set . prepare_files ( finder , force_root_egg_info = self . bundle , bundle = self . bundle )
else :
requirement_set . locate_files ( )
if not options . no_install and not self . bundle :
requirement_set . install ( install_options , global_options , root = options . root_path )
installed = ' ' . join ( [ req . name for req in
requirement_set . successfully_installed ] )
if installed :
logger . notify ( ' Successfully installed %s ' % installed )
elif not self . bundle :
downloaded = ' ' . join ( [ req . name for req in
requirement_set . successfully_downloaded ] )
if downloaded :
logger . notify ( ' Successfully downloaded %s ' % downloaded )
elif self . bundle :
requirement_set . create_bundle ( self . bundle_filename )
logger . notify ( ' Created bundle in %s ' % self . bundle_filename )
2013-07-24 11:02:08 +02:00
except PreviousBuildDirError :
2013-08-22 22:17:01 +02:00
options . no_clean = True
raise
2013-03-27 06:26:52 +01:00
finally :
# Clean up
if ( not options . no_clean ) and ( ( not options . no_install ) or options . download_dir ) :
requirement_set . cleanup_files ( bundle = self . bundle )
2012-02-03 04:23:07 +01:00
if options . target_dir :
if not os . path . exists ( options . target_dir ) :
os . makedirs ( options . target_dir )
2013-04-19 07:38:56 +02:00
lib_dir = distutils_scheme ( ' ' , home = temp_target_dir ) [ ' purelib ' ]
2012-02-03 04:23:07 +01:00
for item in os . listdir ( lib_dir ) :
2012-02-03 20:37:53 +01:00
shutil . move (
os . path . join ( lib_dir , item ) ,
os . path . join ( options . target_dir , item )
)
2012-02-03 12:22:51 +01:00
shutil . rmtree ( temp_target_dir )
2009-11-20 19:20:52 +01:00
return requirement_set