1010from types import *
1111from distutils .core import Command
1212from distutils import sysconfig
13- from distutils .util import write_file , native_path , subst_vars
13+ from distutils .util import write_file , native_path , subst_vars , change_root
1414from distutils .errors import DistutilsOptionError
1515
1616INSTALL_SCHEMES = {
@@ -60,6 +60,8 @@ class install (Command):
6060 ('install-platbase=' , None ,
6161 "base installation directory for platform-specific files " +
6262 "(instead of --exec-prefix or --home)" ),
63+ ('root=' , None ,
64+ "install everything relative to this alternate root directory" ),
6365
6466 # Or, explicitly set the installation scheme
6567 ('install-purelib=' , None ,
@@ -104,6 +106,7 @@ def initialize_options (self):
104106 # the --install-{platlib,purelib,scripts,data} options).
105107 self .install_base = None
106108 self .install_platbase = None
109+ self .root = None
107110
108111 # These options are the actual installation directories; if not
109112 # supplied by the user, they are filled in using the installation
@@ -183,17 +186,14 @@ def finalize_options (self):
183186 # install_{purelib,platlib,lib,scripts,data,...}, and the
184187 # INSTALL_SCHEME dictionary above. Phew!
185188
186- from pprint import pprint
187- print "pre-finalize:"
188- pprint (self .__dict__ )
189+ self .dump_dirs ("pre-finalize_xxx" )
189190
190191 if os .name == 'posix' :
191192 self .finalize_unix ()
192193 else :
193194 self .finalize_other ()
194195
195- print "post-finalize:"
196- pprint (self .__dict__ )
196+ self .dump_dirs ("post-finalize_xxx()" )
197197
198198 # Expand configuration variables, tilde, etc. in self.install_base
199199 # and self.install_platbase -- that way, we can use $base or
@@ -206,23 +206,22 @@ def finalize_options (self):
206206 }
207207 self .expand_basedirs ()
208208
209- print "post-expand_basedirs:"
210- pprint (self .__dict__ )
209+ self .dump_dirs ("post-expand_basedirs()" )
211210
212211 # Now define config vars for the base directories so we can expand
213212 # everything else.
214213 self .config_vars ['base' ] = self .install_base
215214 self .config_vars ['platbase' ] = self .install_platbase
216215
216+ from pprint import pprint
217217 print "config vars:"
218218 pprint (self .config_vars )
219219
220220 # Expand "~" and configuration variables in the installation
221221 # directories.
222222 self .expand_dirs ()
223223
224- print "post-expand:"
225- pprint (self .__dict__ )
224+ self .dump_dirs ("post-expand_dirs()" )
226225
227226 # Pick the actual directory to install all modules to: either
228227 # install_purelib or install_platlib, depending on whether this
@@ -242,6 +241,16 @@ def finalize_options (self):
242241 self .install_libbase = self .install_lib # needed for .pth file
243242 self .install_lib = os .path .join (self .install_lib , self .extra_dirs )
244243
244+ # If a new root directory was supplied, make all the installation
245+ # dirs relative to it.
246+ if self .root is not None :
247+ for name in ('lib' , 'purelib' , 'platlib' , 'scripts' , 'data' ):
248+ attr = "install_" + name
249+ new_val = change_root (self .root , getattr (self , attr ))
250+ setattr (self , attr , new_val )
251+
252+ self .dump_dirs ("after prepending root" )
253+
245254 # Find out the build directories, ie. where to install from.
246255 self .set_undefined_options ('build' ,
247256 ('build_base' , 'build_base' ),
@@ -253,6 +262,16 @@ def finalize_options (self):
253262 # finalize_options ()
254263
255264
265+ # hack for debugging output
266+ def dump_dirs (self , msg ):
267+ from distutils .fancy_getopt import longopt_xlate
268+ print msg + ":"
269+ for opt in self .user_options :
270+ opt_name = string .translate (opt [0 ][0 :- 1 ], longopt_xlate )
271+ val = getattr (self , opt_name )
272+ print " %s: %s" % (opt_name , val )
273+
274+
256275 def finalize_unix (self ):
257276
258277 if self .install_base is not None or self .install_platbase is not None :
@@ -339,7 +358,8 @@ def _expand_attrs (self, attrs):
339358
340359 def expand_basedirs (self ):
341360 self ._expand_attrs (['install_base' ,
342- 'install_platbase' ])
361+ 'install_platbase' ,
362+ 'root' ])
343363
344364 def expand_dirs (self ):
345365 self ._expand_attrs (['install_purelib' ,
0 commit comments