/Lib/distutils/command/install.py

http://unladen-swallow.googlecode.com/ · Python · 686 lines · 540 code · 82 blank · 64 comment · 79 complexity · 84300044a107e42905c9a649551219f7 MD5 · raw file

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