home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / install.py < prev    next >
Encoding:
Python Source  |  2003-04-22  |  22.8 KB  |  593 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 2001/12/06 20:57:12 fdrake 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 = ['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.no_compile = None
  162.         self.optimize = None
  163.  
  164.         # These two are for putting non-packagized distributions into their
  165.         # own directory and creating a .pth file if it makes sense.
  166.         # 'extra_path' comes from the setup file; 'install_path_file' can
  167.         # be turned off if it makes no sense to install a .pth file.  (But
  168.         # better to install it uselessly than to guess wrong and not
  169.         # install it when it's necessary and would be used!)  Currently,
  170.         # 'install_path_file' is always true unless some outsider meddles
  171.         # with it.
  172.         self.extra_path = None
  173.         self.install_path_file = 1
  174.  
  175.         # 'force' forces installation, even if target files are not
  176.         # out-of-date.  'skip_build' skips running the "build" command,
  177.         # handy if you know it's not necessary.  'warn_dir' (which is *not*
  178.         # a user option, it's just there so the bdist_* commands can turn
  179.         # it off) determines whether we warn about installing to a
  180.         # directory not in sys.path.
  181.         self.force = 0
  182.         self.skip_build = 0
  183.         self.warn_dir = 1
  184.  
  185.         # These are only here as a conduit from the 'build' command to the
  186.         # 'install_*' commands that do the real work.  ('build_base' isn't
  187.         # actually used anywhere, but it might be useful in future.)  They
  188.         # are not user options, because if the user told the install
  189.         # command where the build directory is, that wouldn't affect the
  190.         # build command.
  191.         self.build_base = None
  192.         self.build_lib = None
  193.  
  194.         # Not defined yet because we don't know anything about
  195.         # documentation yet.
  196.         #self.install_man = None
  197.         #self.install_html = None
  198.         #self.install_info = None
  199.  
  200.         self.record = None
  201.  
  202.  
  203.     # -- Option finalizing methods -------------------------------------
  204.     # (This is rather more involved than for most commands,
  205.     # because this is where the policy for installing third-
  206.     # party Python modules on various platforms given a wide
  207.     # array of user input is decided.  Yes, it's quite complex!)
  208.  
  209.     def finalize_options (self):
  210.  
  211.         # This method (and its pliant slaves, like 'finalize_unix()',
  212.         # 'finalize_other()', and 'select_scheme()') is where the default
  213.         # installation directories for modules, extension modules, and
  214.         # anything else we care to install from a Python module
  215.         # distribution.  Thus, this code makes a pretty important policy
  216.         # statement about how third-party stuff is added to a Python
  217.         # installation!  Note that the actual work of installation is done
  218.         # by the relatively simple 'install_*' commands; they just take
  219.         # their orders from the installation directory options determined
  220.         # here.
  221.  
  222.         # Check for errors/inconsistencies in the options; first, stuff
  223.         # that's wrong on any platform.
  224.  
  225.         if ((self.prefix or self.exec_prefix or self.home) and
  226.             (self.install_base or self.install_platbase)):
  227.             raise DistutilsOptionError, \
  228.                   ("must supply either prefix/exec-prefix/home or " +
  229.                    "install-base/install-platbase -- not both")
  230.  
  231.         # Next, stuff that's wrong (or dubious) only on certain platforms.
  232.         if os.name == 'posix':
  233.             if self.home and (self.prefix or self.exec_prefix):
  234.                 raise DistutilsOptionError, \
  235.                       ("must supply either home or prefix/exec-prefix -- " +
  236.                        "not both")
  237.         else:
  238.             if self.exec_prefix:
  239.                 self.warn("exec-prefix option ignored on this platform")
  240.                 self.exec_prefix = None
  241.             if self.home:
  242.                 self.warn("home option ignored on this platform")
  243.                 self.home = None
  244.  
  245.         # Now the interesting logic -- so interesting that we farm it out
  246.         # to other methods.  The goal of these methods is to set the final
  247.         # values for the install_{lib,scripts,data,...}  options, using as
  248.         # input a heady brew of prefix, exec_prefix, home, install_base,
  249.         # install_platbase, user-supplied versions of
  250.         # install_{purelib,platlib,lib,scripts,data,...}, and the
  251.         # INSTALL_SCHEME dictionary above.  Phew!
  252.  
  253.         self.dump_dirs("pre-finalize_{unix,other}")
  254.  
  255.         if os.name == 'posix':
  256.             self.finalize_unix()
  257.         else:
  258.             self.finalize_other()
  259.  
  260.         self.dump_dirs("post-finalize_{unix,other}()")
  261.  
  262.         # Expand configuration variables, tilde, etc. in self.install_base
  263.         # and self.install_platbase -- that way, we can use $base or
  264.         # $platbase in the other installation directories and not worry
  265.         # about needing recursive variable expansion (shudder).
  266.  
  267.         py_version = (string.split(sys.version))[0]
  268.         (prefix, exec_prefix) = get_config_vars('prefix', 'exec_prefix')
  269.         self.config_vars = {'dist_name': self.distribution.get_name(),
  270.                             'dist_version': self.distribution.get_version(),
  271.                             'dist_fullname': self.distribution.get_fullname(),
  272.                             'py_version': py_version,
  273.                             'py_version_short': py_version[0:3],
  274.                             'sys_prefix': prefix,
  275.                             'prefix': prefix,
  276.                             'sys_exec_prefix': exec_prefix,
  277.                             'exec_prefix': exec_prefix,
  278.                            }
  279.         self.expand_basedirs()
  280.  
  281.         self.dump_dirs("post-expand_basedirs()")
  282.  
  283.         # Now define config vars for the base directories so we can expand
  284.         # everything else.
  285.         self.config_vars['base'] = self.install_base
  286.         self.config_vars['platbase'] = self.install_platbase
  287.  
  288.         if DEBUG:
  289.             from pprint import pprint
  290.             print "config vars:"
  291.             pprint(self.config_vars)
  292.  
  293.         # Expand "~" and configuration variables in the installation
  294.         # directories.
  295.         self.expand_dirs()
  296.  
  297.         self.dump_dirs("post-expand_dirs()")
  298.  
  299.         # Pick the actual directory to install all modules to: either
  300.         # install_purelib or install_platlib, depending on whether this
  301.         # module distribution is pure or not.  Of course, if the user
  302.         # already specified install_lib, use their selection.
  303.         if self.install_lib is None:
  304.             if self.distribution.ext_modules: # has extensions: non-pure
  305.                 self.install_lib = self.install_platlib
  306.             else:
  307.                 self.install_lib = self.install_purelib
  308.  
  309.  
  310.         # Convert directories from Unix /-separated syntax to the local
  311.         # convention.
  312.         self.convert_paths('lib', 'purelib', 'platlib',
  313.                            'scripts', 'data', 'headers')
  314.  
  315.         # Well, we're not actually fully completely finalized yet: we still
  316.         # have to deal with 'extra_path', which is the hack for allowing
  317.         # non-packagized module distributions (hello, Numerical Python!) to
  318.         # get their own directories.
  319.         self.handle_extra_path()
  320.         self.install_libbase = self.install_lib # needed for .pth file
  321.         self.install_lib = os.path.join(self.install_lib, self.extra_dirs)
  322.  
  323.         # If a new root directory was supplied, make all the installation
  324.         # dirs relative to it.
  325.         if self.root is not None:
  326.             self.change_roots('libbase', 'lib', 'purelib', 'platlib',
  327.                               'scripts', 'data', 'headers')
  328.  
  329.         self.dump_dirs("after prepending root")
  330.  
  331.         # Find out the build directories, ie. where to install from.
  332.         self.set_undefined_options('build',
  333.                                    ('build_base', 'build_base'),
  334.                                    ('build_lib', 'build_lib'))
  335.  
  336.         # Punt on doc directories for now -- after all, we're punting on
  337.         # documentation completely!
  338.  
  339.     # finalize_options ()
  340.  
  341.  
  342.     def dump_dirs (self, msg):
  343.         if DEBUG:
  344.             from distutils.fancy_getopt import longopt_xlate
  345.             print msg + ":"
  346.             for opt in self.user_options:
  347.                 opt_name = opt[0]
  348.                 if opt_name[-1] == "=":
  349.                     opt_name = opt_name[0:-1]
  350.                 opt_name = string.translate(opt_name, longopt_xlate)
  351.                 val = getattr(self, opt_name)
  352.                 print "  %s: %s" % (opt_name, val)
  353.  
  354.  
  355.     def finalize_unix (self):
  356.  
  357.         if self.install_base is not None or self.install_platbase is not None:
  358.             if ((self.install_lib is None and
  359.                  self.install_purelib is None and
  360.                  self.install_platlib is None) or
  361.                 self.install_headers is None or
  362.                 self.install_scripts is None or
  363.                 self.install_data is None):
  364.                 raise DistutilsOptionError, \
  365.                       "install-base or install-platbase supplied, but " + \
  366.                       "installation scheme is incomplete"
  367.             return
  368.  
  369.         if self.home is not None:
  370.             self.install_base = self.install_platbase = self.home
  371.             self.select_scheme("unix_home")
  372.         else:
  373.             if self.prefix is None:
  374.                 if self.exec_prefix is not None:
  375.                     raise DistutilsOptionError, \
  376.                           "must not supply exec-prefix without prefix"
  377.  
  378.                 self.prefix = os.path.normpath(sys.prefix)
  379.                 self.exec_prefix = os.path.normpath(sys.exec_prefix)
  380.  
  381.             else:
  382.                 if self.exec_prefix is None:
  383.                     self.exec_prefix = self.prefix
  384.  
  385.             self.install_base = self.prefix
  386.             self.install_platbase = self.exec_prefix
  387.             self.select_scheme("unix_prefix")
  388.  
  389.     # finalize_unix ()
  390.  
  391.  
  392.     def finalize_other (self):          # Windows and Mac OS for now
  393.  
  394.         if self.prefix is None:
  395.             self.prefix = os.path.normpath(sys.prefix)
  396.  
  397.         self.install_base = self.install_platbase = self.prefix
  398.         try:
  399.             self.select_scheme(os.name)
  400.         except KeyError:
  401.             raise DistutilsPlatformError, \
  402.                   "I don't know how to install stuff on '%s'" % os.name
  403.  
  404.     # finalize_other ()
  405.  
  406.  
  407.     def select_scheme (self, name):
  408.         # it's the caller's problem if they supply a bad name!
  409.         scheme = INSTALL_SCHEMES[name]
  410.         for key in SCHEME_KEYS:
  411.             attrname = 'install_' + key
  412.             if getattr(self, attrname) is None:
  413.                 setattr(self, attrname, scheme[key])
  414.  
  415.  
  416.     def _expand_attrs (self, attrs):
  417.         for attr in attrs:
  418.             val = getattr(self, attr)
  419.             if val is not None:
  420.                 if os.name == 'posix':
  421.                     val = os.path.expanduser(val)
  422.                 val = subst_vars(val, self.config_vars)
  423.                 setattr(self, attr, val)
  424.  
  425.  
  426.     def expand_basedirs (self):
  427.         self._expand_attrs(['install_base',
  428.                             'install_platbase',
  429.                             'root'])
  430.  
  431.     def expand_dirs (self):
  432.         self._expand_attrs(['install_purelib',
  433.                             'install_platlib',
  434.                             'install_lib',
  435.                             'install_headers',
  436.                             'install_scripts',
  437.                             'install_data',])
  438.  
  439.  
  440.     def convert_paths (self, *names):
  441.         for name in names:
  442.             attr = "install_" + name
  443.             setattr(self, attr, convert_path(getattr(self, attr)))
  444.  
  445.  
  446.     def handle_extra_path (self):
  447.  
  448.         if self.extra_path is None:
  449.             self.extra_path = self.distribution.extra_path
  450.  
  451.         if self.extra_path is not None:
  452.             if type(self.extra_path) is StringType:
  453.                 self.extra_path = string.split(self.extra_path, ',')
  454.  
  455.             if len(self.extra_path) == 1:
  456.                 path_file = extra_dirs = self.extra_path[0]
  457.             elif len(self.extra_path) == 2:
  458.                 (path_file, extra_dirs) = self.extra_path
  459.             else:
  460.                 raise DistutilsOptionError, \
  461.                       "'extra_path' option must be a list, tuple, or " + \
  462.                       "comma-separated string with 1 or 2 elements"
  463.  
  464.             # convert to local form in case Unix notation used (as it
  465.             # should be in setup scripts)
  466.             extra_dirs = convert_path(extra_dirs)
  467.  
  468.         else:
  469.             path_file = None
  470.             extra_dirs = ''
  471.  
  472.         # XXX should we warn if path_file and not extra_dirs? (in which
  473.         # case the path file would be harmless but pointless)
  474.         self.path_file = path_file
  475.         self.extra_dirs = extra_dirs
  476.  
  477.     # handle_extra_path ()
  478.  
  479.  
  480.     def change_roots (self, *names):
  481.         for name in names:
  482.             attr = "install_" + name
  483.             setattr(self, attr, change_root(self.root, getattr(self, attr)))
  484.  
  485.  
  486.     # -- Command execution methods -------------------------------------
  487.  
  488.     def run (self):
  489.  
  490.         # Obviously have to build before we can install
  491.         if not self.skip_build:
  492.             self.run_command('build')
  493.  
  494.         # Run all sub-commands (at least those that need to be run)
  495.         for cmd_name in self.get_sub_commands():
  496.             self.run_command(cmd_name)
  497.  
  498.         if self.path_file:
  499.             self.create_path_file()
  500.  
  501.         # write list of installed files, if requested.
  502.         if self.record:
  503.             outputs = self.get_outputs()
  504.             if self.root:               # strip any package prefix
  505.                 root_len = len(self.root)
  506.                 for counter in xrange(len(outputs)):
  507.                     outputs[counter] = outputs[counter][root_len:]
  508.             self.execute(write_file,
  509.                          (self.record, outputs),
  510.                          "writing list of installed files to '%s'" %
  511.                          self.record)
  512.  
  513.         sys_path = map(os.path.normpath, sys.path)
  514.         sys_path = map(os.path.normcase, sys_path)
  515.         install_lib = os.path.normcase(os.path.normpath(self.install_lib))
  516.         if (self.warn_dir and
  517.             not (self.path_file and self.install_path_file) and
  518.             install_lib not in sys_path):
  519.             self.warn(("modules installed to '%s', which is not in " +
  520.                        "Python's module search path (sys.path) -- " +
  521.                        "you'll have to change the search path yourself") %
  522.                       self.install_lib)
  523.  
  524.     # run ()
  525.  
  526.     def create_path_file (self):
  527.         filename = os.path.join(self.install_libbase,
  528.                                 self.path_file + ".pth")
  529.         if self.install_path_file:
  530.             self.execute(write_file,
  531.                          (filename, [self.extra_dirs]),
  532.                          "creating %s" % filename)
  533.         else:
  534.             self.warn("path file '%s' not created" % filename)
  535.  
  536.  
  537.     # -- Reporting methods ---------------------------------------------
  538.  
  539.     def get_outputs (self):
  540.         # Assemble the outputs of all the sub-commands.
  541.         outputs = []
  542.         for cmd_name in self.get_sub_commands():
  543.             cmd = self.get_finalized_command(cmd_name)
  544.             # Add the contents of cmd.get_outputs(), ensuring
  545.             # that outputs doesn't contain duplicate entries
  546.             for filename in cmd.get_outputs():
  547.                 if filename not in outputs:
  548.                     outputs.append(filename)
  549.  
  550.         if self.path_file and self.install_path_file:
  551.             outputs.append(os.path.join(self.install_libbase,
  552.                                         self.path_file + ".pth"))
  553.  
  554.         return outputs
  555.  
  556.     def get_inputs (self):
  557.         # XXX gee, this looks familiar ;-(
  558.         inputs = []
  559.         for cmd_name in self.get_sub_commands():
  560.             cmd = self.get_finalized_command(cmd_name)
  561.             inputs.extend(cmd.get_inputs())
  562.  
  563.         return inputs
  564.  
  565.  
  566.     # -- Predicates for sub-command list -------------------------------
  567.  
  568.     def has_lib (self):
  569.         """Return true if the current distribution has any Python
  570.         modules to install."""
  571.         return (self.distribution.has_pure_modules() or
  572.                 self.distribution.has_ext_modules())
  573.  
  574.     def has_headers (self):
  575.         return self.distribution.has_headers()
  576.  
  577.     def has_scripts (self):
  578.         return self.distribution.has_scripts()
  579.  
  580.     def has_data (self):
  581.         return self.distribution.has_data_files()
  582.  
  583.  
  584.     # 'sub_commands': a list of commands this command might have to run to
  585.     # get its work done.  See cmd.py for more info.
  586.     sub_commands = [('install_lib',     has_lib),
  587.                     ('install_headers', has_headers),
  588.                     ('install_scripts', has_scripts),
  589.                     ('install_data',    has_data),
  590.                    ]
  591.  
  592. # class install
  593.