1"""distutils.command.install
2
3Implements the Distutils 'install' command."""
4
5from distutils import log
6
7# This module should be kept compatible with Python 2.1.
8
9__revision__ = "$Id$"
10
11import sys, os, string
12from types import *
13from distutils.core import Command
14from distutils.debug import DEBUG
15from distutils.sysconfig import get_config_vars
16from distutils.errors import DistutilsPlatformError
17from distutils.file_util import write_file
18from distutils.util import convert_path, subst_vars, change_root
19from distutils.util import get_platform
20from distutils.errors import DistutilsOptionError
21from site import USER_BASE
22from site import USER_SITE
23
24
25if sys.version < "2.2":
26    WINDOWS_SCHEME = {
27        'purelib': '$base',
28        'platlib': '$base',
29        'headers': '$base/Include/$dist_name',
30        'scripts': '$base/Scripts',
31        'data'   : '$base',
32    }
33else:
34    WINDOWS_SCHEME = {
35        'purelib': '$base/Lib/site-packages',
36        'platlib': '$base/Lib/site-packages',
37        'headers': '$base/Include/$dist_name',
38        'scripts': '$base/Scripts',
39        'data'   : '$base',
40    }
41
42INSTALL_SCHEMES = {
43    'unix_prefix': {
44        'purelib': '$base/lib/python-legacy/site-packages',
45        'platlib': '$platbase/lib/python-legacy/site-packages',
46        'headers': '$base/include/python-legacy/$dist_name',
47        'scripts': '$base/bin',
48        'data'   : '$base',
49        },
50    'unix_home': {
51        'purelib': '$base/lib/python',
52        'platlib': '$base/lib/python',
53        'headers': '$base/include/python/$dist_name',
54        'scripts': '$base/bin',
55        'data'   : '$base',
56        },
57    'unix_user': {
58        'purelib': '$usersite',
59        'platlib': '$usersite',
60        'headers': '$userbase/include/python-legacy/$dist_name',
61        'scripts': '$userbase/bin',
62        'data'   : '$userbase',
63        },
64    'nt': WINDOWS_SCHEME,
65    'nt_user': {
66        'purelib': '$usersite',
67        'platlib': '$usersite',
68        'headers': '$userbase/Python$py_version_nodot/Include/$dist_name',
69        'scripts': '$userbase/Scripts',
70        'data'   : '$userbase',
71        },
72    'os2': {
73        'purelib': '$base/Lib/site-packages',
74        'platlib': '$base/Lib/site-packages',
75        'headers': '$base/Include/$dist_name',
76        'scripts': '$base/Scripts',
77        'data'   : '$base',
78        },
79    'os2_home': {
80        'purelib': '$usersite',
81        'platlib': '$usersite',
82        'headers': '$userbase/include/python-legacy/$dist_name',
83        'scripts': '$userbase/bin',
84        'data'   : '$userbase',
85        },
86    }
87
88# The keys to an installation scheme; if any new types of files are to be
89# installed, be sure to add an entry to every installation scheme above,
90# and to SCHEME_KEYS here.
91SCHEME_KEYS = ('purelib', 'platlib', 'headers', 'scripts', 'data')
92
93
94class install (Command):
95
96    description = "install everything from build directory"
97
98    user_options = [
99        # Select installation scheme and set base director(y|ies)
100        ('prefix=', None,
101         "installation prefix"),
102        ('exec-prefix=', None,
103         "(Unix only) prefix for platform-specific files"),
104        ('home=', None,
105         "(Unix only) home directory to install under"),
106        ('user', None,
107         "install in user site-package '%s'" % USER_SITE),
108
109        # Or, just set the base director(y|ies)
110        ('install-base=', None,
111         "base installation directory (instead of --prefix or --home)"),
112        ('install-platbase=', None,
113         "base installation directory for platform-specific files " +
114         "(instead of --exec-prefix or --home)"),
115        ('root=', None,
116         "install everything relative to this alternate root directory"),
117
118        # Or, explicitly set the installation scheme
119        ('install-purelib=', None,
120         "installation directory for pure Python module distributions"),
121        ('install-platlib=', None,
122         "installation directory for non-pure module distributions"),
123        ('install-lib=', None,
124         "installation directory for all module distributions " +
125         "(overrides --install-purelib and --install-platlib)"),
126
127        ('install-headers=', None,
128         "installation directory for C/C++ headers"),
129        ('install-scripts=', None,
130         "installation directory for Python scripts"),
131        ('install-data=', None,
132         "installation directory for data files"),
133
134        # Byte-compilation options -- see install_lib.py for details, as
135        # these are duplicated from there (but only install_lib does
136        # anything with them).
137        ('compile', 'c', "compile .py to .pyc [default]"),
138        ('no-compile', None, "don't compile .py files"),
139        ('optimize=', 'O',
140         "also compile with optimization: -O1 for \"python -O\", "
141         "-O2 for \"python -OO\", and -O0 to disable [default: -O0]"),
142
143        # Miscellaneous control options
144        ('force', 'f',
145         "force installation (overwrite any existing files)"),
146        ('skip-build', None,
147         "skip rebuilding everything (for testing/debugging)"),
148
149        # Where to install documentation (eventually!)
150        #('doc-format=', None, "format of documentation to generate"),
151        #('install-man=', None, "directory for Unix man pages"),
152        #('install-html=', None, "directory for HTML documentation"),
153        #('install-info=', None, "directory for GNU info files"),
154
155        ('record=', None,
156         "filename in which to record list of installed files"),
157        ]
158
159    boolean_options = ['compile', 'force', 'skip-build', 'user']
160    negative_opt = {'no-compile' : 'compile'}
161
162
163    def initialize_options (self):
164
165        # High-level options: these select both an installation base
166        # and scheme.
167        self.prefix = None
168        self.exec_prefix = None
169        self.home = None
170        self.user = 0
171
172        # These select only the installation base; it's up to the user to
173        # specify the installation scheme (currently, that means supplying
174        # the --install-{platlib,purelib,scripts,data} options).
175        self.install_base = None
176        self.install_platbase = None
177        self.root = None
178
179        # These options are the actual installation directories; if not
180        # supplied by the user, they are filled in using the installation
181        # scheme implied by prefix/exec-prefix/home and the contents of
182        # that installation scheme.
183        self.install_purelib = None     # for pure module distributions
184        self.install_platlib = None     # non-pure (dists w/ extensions)
185        self.install_headers = None     # for C/C++ headers
186        self.install_lib = None         # set to either purelib or platlib
187        self.install_scripts = None
188        self.install_data = None
189        self.install_userbase = USER_BASE
190        self.install_usersite = USER_SITE
191
192        self.compile = None
193        self.optimize = None
194
195        # These two are for putting non-packagized distributions into their
196        # own directory and creating a .pth file if it makes sense.
197        # 'extra_path' comes from the setup file; 'install_path_file' can
198        # be turned off if it makes no sense to install a .pth file.  (But
199        # better to install it uselessly than to guess wrong and not
200        # install it when it's necessary and would be used!)  Currently,
201        # 'install_path_file' is always true unless some outsider meddles
202        # with it.
203        self.extra_path = None
204        self.install_path_file = 1
205
206        # 'force' forces installation, even if target files are not
207        # out-of-date.  'skip_build' skips running the "build" command,
208        # handy if you know it's not necessary.  'warn_dir' (which is *not*
209        # a user option, it's just there so the bdist_* commands can turn
210        # it off) determines whether we warn about installing to a
211        # directory not in sys.path.
212        self.force = 0
213        self.skip_build = 0
214        self.warn_dir = 1
215
216        # These are only here as a conduit from the 'build' command to the
217        # 'install_*' commands that do the real work.  ('build_base' isn't
218        # actually used anywhere, but it might be useful in future.)  They
219        # are not user options, because if the user told the install
220        # command where the build directory is, that wouldn't affect the
221        # build command.
222        self.build_base = None
223        self.build_lib = None
224
225        # Not defined yet because we don't know anything about
226        # documentation yet.
227        #self.install_man = None
228        #self.install_html = None
229        #self.install_info = None
230
231        self.record = None
232
233
234    # -- Option finalizing methods -------------------------------------
235    # (This is rather more involved than for most commands,
236    # because this is where the policy for installing third-
237    # party Python modules on various platforms given a wide
238    # array of user input is decided.  Yes, it's quite complex!)
239
240    def finalize_options (self):
241
242        # This method (and its pliant slaves, like 'finalize_unix()',
243        # 'finalize_other()', and 'select_scheme()') is where the default
244        # installation directories for modules, extension modules, and
245        # anything else we care to install from a Python module
246        # distribution.  Thus, this code makes a pretty important policy
247        # statement about how third-party stuff is added to a Python
248        # installation!  Note that the actual work of installation is done
249        # by the relatively simple 'install_*' commands; they just take
250        # their orders from the installation directory options determined
251        # here.
252
253        # Check for errors/inconsistencies in the options; first, stuff
254        # that's wrong on any platform.
255
256        if ((self.prefix or self.exec_prefix or self.home) and
257            (self.install_base or self.install_platbase)):
258            raise DistutilsOptionError, \
259                  ("must supply either prefix/exec-prefix/home or " +
260                   "install-base/install-platbase -- not both")
261
262        if self.home and (self.prefix or self.exec_prefix):
263            raise DistutilsOptionError, \
264                  "must supply either home or prefix/exec-prefix -- not both"
265
266        if self.user and (self.prefix or self.exec_prefix or self.home or
267                self.install_base or self.install_platbase):
268            raise DistutilsOptionError("can't combine user with prefix, "
269                                       "exec_prefix/home, or install_(plat)base")
270
271        # Next, stuff that's wrong (or dubious) only on certain platforms.
272        if os.name != "posix":
273            if self.exec_prefix:
274                self.warn("exec-prefix option ignored on this platform")
275                self.exec_prefix = None
276
277        # Now the interesting logic -- so interesting that we farm it out
278        # to other methods.  The goal of these methods is to set the final
279        # values for the install_{lib,scripts,data,...}  options, using as
280        # input a heady brew of prefix, exec_prefix, home, install_base,
281        # install_platbase, user-supplied versions of
282        # install_{purelib,platlib,lib,scripts,data,...}, and the
283        # INSTALL_SCHEME dictionary above.  Phew!
284
285        self.dump_dirs("pre-finalize_{unix,other}")
286
287        if os.name == 'posix':
288            self.finalize_unix()
289        else:
290            self.finalize_other()
291
292        self.dump_dirs("post-finalize_{unix,other}()")
293
294        # Expand configuration variables, tilde, etc. in self.install_base
295        # and self.install_platbase -- that way, we can use $base or
296        # $platbase in the other installation directories and not worry
297        # about needing recursive variable expansion (shudder).
298
299        py_version = (string.split(sys.version))[0]
300        (prefix, exec_prefix) = get_config_vars('prefix', 'exec_prefix')
301        self.config_vars = {'dist_name': self.distribution.get_name(),
302                            'dist_version': self.distribution.get_version(),
303                            'dist_fullname': self.distribution.get_fullname(),
304                            'py_version': py_version,
305                            'py_version_short': py_version[0:3],
306                            'py_version_nodot': py_version[0] + py_version[2],
307                            'sys_prefix': prefix,
308                            'prefix': prefix,
309                            'sys_exec_prefix': exec_prefix,
310                            'exec_prefix': exec_prefix,
311                            'userbase': self.install_userbase,
312                            'usersite': self.install_usersite,
313                           }
314        self.expand_basedirs()
315
316        self.dump_dirs("post-expand_basedirs()")
317
318        # Now define config vars for the base directories so we can expand
319        # everything else.
320        self.config_vars['base'] = self.install_base
321        self.config_vars['platbase'] = self.install_platbase
322
323        if DEBUG:
324            from pprint import pprint
325            print "config vars:"
326            pprint(self.config_vars)
327
328        # Expand "~" and configuration variables in the installation
329        # directories.
330        self.expand_dirs()
331
332        self.dump_dirs("post-expand_dirs()")
333
334        # Create directories in the home dir:
335        if self.user:
336            self.create_home_path()
337
338        # Pick the actual directory to install all modules to: either
339        # install_purelib or install_platlib, depending on whether this
340        # module distribution is pure or not.  Of course, if the user
341        # already specified install_lib, use their selection.
342        if self.install_lib is None:
343            if self.distribution.ext_modules: # has extensions: non-pure
344                self.install_lib = self.install_platlib
345            else:
346                self.install_lib = self.install_purelib
347
348
349        # Convert directories from Unix /-separated syntax to the local
350        # convention.
351        self.convert_paths('lib', 'purelib', 'platlib',
352                           'scripts', 'data', 'headers',
353                           'userbase', 'usersite')
354
355        # Well, we're not actually fully completely finalized yet: we still
356        # have to deal with 'extra_path', which is the hack for allowing
357        # non-packagized module distributions (hello, Numerical Python!) to
358        # get their own directories.
359        self.handle_extra_path()
360        self.install_libbase = self.install_lib # needed for .pth file
361        self.install_lib = os.path.join(self.install_lib, self.extra_dirs)
362
363        # If a new root directory was supplied, make all the installation
364        # dirs relative to it.
365        if self.root is not None:
366            self.change_roots('libbase', 'lib', 'purelib', 'platlib',
367                              'scripts', 'data', 'headers')
368
369        self.dump_dirs("after prepending root")
370
371        # Find out the build directories, ie. where to install from.
372        self.set_undefined_options('build',
373                                   ('build_base', 'build_base'),
374                                   ('build_lib', 'build_lib'))
375
376        # Punt on doc directories for now -- after all, we're punting on
377        # documentation completely!
378
379    # finalize_options ()
380
381
382    def dump_dirs (self, msg):
383        if DEBUG:
384            from distutils.fancy_getopt import longopt_xlate
385            print msg + ":"
386            for opt in self.user_options:
387                opt_name = opt[0]
388                if opt_name[-1] == "=":
389                    opt_name = opt_name[0:-1]
390                if opt_name in self.negative_opt:
391                    opt_name = string.translate(self.negative_opt[opt_name],
392                                                longopt_xlate)
393                    val = not getattr(self, opt_name)
394                else:
395                    opt_name = string.translate(opt_name, longopt_xlate)
396                    val = getattr(self, opt_name)
397                print "  %s: %s" % (opt_name, val)
398
399
400    def finalize_unix (self):
401
402        if self.install_base is not None or self.install_platbase is not None:
403            if ((self.install_lib is None and
404                 self.install_purelib is None and
405                 self.install_platlib is None) or
406                self.install_headers is None or
407                self.install_scripts is None or
408                self.install_data is None):
409                raise DistutilsOptionError, \
410                      ("install-base or install-platbase supplied, but "
411                      "installation scheme is incomplete")
412            return
413
414        if self.user:
415            if self.install_userbase is None:
416                raise DistutilsPlatformError(
417                    "User base directory is not specified")
418            self.install_base = self.install_platbase = self.install_userbase
419            self.select_scheme("unix_user")
420        elif self.home is not None:
421            self.install_base = self.install_platbase = self.home
422            self.select_scheme("unix_home")
423        else:
424            if self.prefix is None:
425                if self.exec_prefix is not None:
426                    raise DistutilsOptionError, \
427                          "must not supply exec-prefix without prefix"
428
429                self.prefix = os.path.normpath(sys.prefix)
430                self.exec_prefix = os.path.normpath(sys.exec_prefix)
431
432            else:
433                if self.exec_prefix is None:
434                    self.exec_prefix = self.prefix
435
436            self.install_base = self.prefix
437            self.install_platbase = self.exec_prefix
438            self.select_scheme("unix_prefix")
439
440    # finalize_unix ()
441
442
443    def finalize_other (self):          # Windows and Mac OS for now
444
445        if self.user:
446            if self.install_userbase is None:
447                raise DistutilsPlatformError(
448                    "User base directory is not specified")
449            self.install_base = self.install_platbase = self.install_userbase
450            self.select_scheme(os.name + "_user")
451        elif self.home is not None:
452            self.install_base = self.install_platbase = self.home
453            self.select_scheme("unix_home")
454        else:
455            if self.prefix is None:
456                self.prefix = os.path.normpath(sys.prefix)
457
458            self.install_base = self.install_platbase = self.prefix
459            try:
460                self.select_scheme(os.name)
461            except KeyError:
462                raise DistutilsPlatformError, \
463                      "I don't know how to install stuff on '%s'" % os.name
464
465    # finalize_other ()
466
467
468    def select_scheme (self, name):
469        # it's the caller's problem if they supply a bad name!
470        scheme = INSTALL_SCHEMES[name]
471        for key in SCHEME_KEYS:
472            attrname = 'install_' + key
473            if getattr(self, attrname) is None:
474                setattr(self, attrname, scheme[key])
475
476
477    def _expand_attrs (self, attrs):
478        for attr in attrs:
479            val = getattr(self, attr)
480            if val is not None:
481                if os.name == 'posix' or os.name == 'nt':
482                    val = os.path.expanduser(val)
483                val = subst_vars(val, self.config_vars)
484                setattr(self, attr, val)
485
486
487    def expand_basedirs (self):
488        self._expand_attrs(['install_base',
489                            'install_platbase',
490                            'root'])
491
492    def expand_dirs (self):
493        self._expand_attrs(['install_purelib',
494                            'install_platlib',
495                            'install_lib',
496                            'install_headers',
497                            'install_scripts',
498                            'install_data',])
499
500
501    def convert_paths (self, *names):
502        for name in names:
503            attr = "install_" + name
504            setattr(self, attr, convert_path(getattr(self, attr)))
505
506
507    def handle_extra_path (self):
508
509        if self.extra_path is None:
510            self.extra_path = self.distribution.extra_path
511
512        if self.extra_path is not None:
513            if type(self.extra_path) is StringType:
514                self.extra_path = string.split(self.extra_path, ',')
515
516            if len(self.extra_path) == 1:
517                path_file = extra_dirs = self.extra_path[0]
518            elif len(self.extra_path) == 2:
519                (path_file, extra_dirs) = self.extra_path
520            else:
521                raise DistutilsOptionError, \
522                      ("'extra_path' option must be a list, tuple, or "
523                      "comma-separated string with 1 or 2 elements")
524
525            # convert to local form in case Unix notation used (as it
526            # should be in setup scripts)
527            extra_dirs = convert_path(extra_dirs)
528
529        else:
530            path_file = None
531            extra_dirs = ''
532
533        # XXX should we warn if path_file and not extra_dirs? (in which
534        # case the path file would be harmless but pointless)
535        self.path_file = path_file
536        self.extra_dirs = extra_dirs
537
538    # handle_extra_path ()
539
540
541    def change_roots (self, *names):
542        for name in names:
543            attr = "install_" + name
544            setattr(self, attr, change_root(self.root, getattr(self, attr)))
545
546    def create_home_path(self):
547        """Create directories under ~
548        """
549        if not self.user:
550            return
551        home = convert_path(os.path.expanduser("~"))
552        for name, path in self.config_vars.iteritems():
553            if path.startswith(home) and not os.path.isdir(path):
554                self.debug_print("os.makedirs('%s', 0700)" % path)
555                os.makedirs(path, 0700)
556
557    # -- Command execution methods -------------------------------------
558
559    def run (self):
560
561        # Obviously have to build before we can install
562        if not self.skip_build:
563            self.run_command('build')
564            # If we built for any other platform, we can't install.
565            build_plat = self.distribution.get_command_obj('build').plat_name
566            # check warn_dir - it is a clue that the 'install' is happening
567            # internally, and not to sys.path, so we don't check the platform
568            # matches what we are running.
569            if self.warn_dir and build_plat != get_platform():
570                raise DistutilsPlatformError("Can't install when "
571                                             "cross-compiling")
572
573        # Run all sub-commands (at least those that need to be run)
574        for cmd_name in self.get_sub_commands():
575            self.run_command(cmd_name)
576
577        if self.path_file:
578            self.create_path_file()
579
580        # write list of installed files, if requested.
581        if self.record:
582            outputs = self.get_outputs()
583            if self.root:               # strip any package prefix
584                root_len = len(self.root)
585                for counter in xrange(len(outputs)):
586                    outputs[counter] = outputs[counter][root_len:]
587            self.execute(write_file,
588                         (self.record, outputs),
589                         "writing list of installed files to '%s'" %
590                         self.record)
591
592        sys_path = map(os.path.normpath, sys.path)
593        sys_path = map(os.path.normcase, sys_path)
594        install_lib = os.path.normcase(os.path.normpath(self.install_lib))
595        if (self.warn_dir and
596            not (self.path_file and self.install_path_file) and
597            install_lib not in sys_path):
598            log.debug(("modules installed to '%s', which is not in "
599                       "Python's module search path (sys.path) -- "
600                       "you'll have to change the search path yourself"),
601                       self.install_lib)
602
603    # run ()
604
605    def create_path_file (self):
606        filename = os.path.join(self.install_libbase,
607                                self.path_file + ".pth")
608        if self.install_path_file:
609            self.execute(write_file,
610                         (filename, [self.extra_dirs]),
611                         "creating %s" % filename)
612        else:
613            self.warn("path file '%s' not created" % filename)
614
615
616    # -- Reporting methods ---------------------------------------------
617
618    def get_outputs (self):
619        # Assemble the outputs of all the sub-commands.
620        outputs = []
621        for cmd_name in self.get_sub_commands():
622            cmd = self.get_finalized_command(cmd_name)
623            # Add the contents of cmd.get_outputs(), ensuring
624            # that outputs doesn't contain duplicate entries
625            for filename in cmd.get_outputs():
626                if filename not in outputs:
627                    outputs.append(filename)
628
629        if self.path_file and self.install_path_file:
630            outputs.append(os.path.join(self.install_libbase,
631                                        self.path_file + ".pth"))
632
633        return outputs
634
635    def get_inputs (self):
636        # XXX gee, this looks familiar ;-(
637        inputs = []
638        for cmd_name in self.get_sub_commands():
639            cmd = self.get_finalized_command(cmd_name)
640            inputs.extend(cmd.get_inputs())
641
642        return inputs
643
644
645    # -- Predicates for sub-command list -------------------------------
646
647    def has_lib (self):
648        """Return true if the current distribution has any Python
649        modules to install."""
650        return (self.distribution.has_pure_modules() or
651                self.distribution.has_ext_modules())
652
653    def has_headers (self):
654        return self.distribution.has_headers()
655
656    def has_scripts (self):
657        return self.distribution.has_scripts()
658
659    def has_data (self):
660        return self.distribution.has_data_files()
661
662
663    # 'sub_commands': a list of commands this command might have to run to
664    # get its work done.  See cmd.py for more info.
665    sub_commands = [('install_lib',     has_lib),
666                    ('install_headers', has_headers),
667                    ('install_scripts', has_scripts),
668                    ('install_data',    has_data),
669                    ('install_egg_info', lambda self:True),
670                   ]
671
672# class install
673