home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / distutils / command / install.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  22.2 KB  |  592 lines

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