install.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import sys
  2. if 'setuptools' in sys.modules:
  3. import setuptools.command.install as old_install_mod
  4. have_setuptools = True
  5. else:
  6. import distutils.command.install as old_install_mod
  7. have_setuptools = False
  8. from distutils.file_util import write_file
  9. old_install = old_install_mod.install
  10. class install(old_install):
  11. # Always run install_clib - the command is cheap, so no need to bypass it;
  12. # but it's not run by setuptools -- so it's run again in install_data
  13. sub_commands = old_install.sub_commands + [
  14. ('install_clib', lambda x: True)
  15. ]
  16. def finalize_options (self):
  17. old_install.finalize_options(self)
  18. self.install_lib = self.install_libbase
  19. def setuptools_run(self):
  20. """ The setuptools version of the .run() method.
  21. We must pull in the entire code so we can override the level used in the
  22. _getframe() call since we wrap this call by one more level.
  23. """
  24. from distutils.command.install import install as distutils_install
  25. # Explicit request for old-style install? Just do it
  26. if self.old_and_unmanageable or self.single_version_externally_managed:
  27. return distutils_install.run(self)
  28. # Attempt to detect whether we were called from setup() or by another
  29. # command. If we were called by setup(), our caller will be the
  30. # 'run_command' method in 'distutils.dist', and *its* caller will be
  31. # the 'run_commands' method. If we were called any other way, our
  32. # immediate caller *might* be 'run_command', but it won't have been
  33. # called by 'run_commands'. This is slightly kludgy, but seems to
  34. # work.
  35. #
  36. caller = sys._getframe(3)
  37. caller_module = caller.f_globals.get('__name__', '')
  38. caller_name = caller.f_code.co_name
  39. if caller_module != 'distutils.dist' or caller_name!='run_commands':
  40. # We weren't called from the command line or setup(), so we
  41. # should run in backward-compatibility mode to support bdist_*
  42. # commands.
  43. distutils_install.run(self)
  44. else:
  45. self.do_egg_install()
  46. def run(self):
  47. if not have_setuptools:
  48. r = old_install.run(self)
  49. else:
  50. r = self.setuptools_run()
  51. if self.record:
  52. # bdist_rpm fails when INSTALLED_FILES contains
  53. # paths with spaces. Such paths must be enclosed
  54. # with double-quotes.
  55. with open(self.record, 'r') as f:
  56. lines = []
  57. need_rewrite = False
  58. for l in f:
  59. l = l.rstrip()
  60. if ' ' in l:
  61. need_rewrite = True
  62. l = '"%s"' % (l)
  63. lines.append(l)
  64. if need_rewrite:
  65. self.execute(write_file,
  66. (self.record, lines),
  67. "re-writing list of installed files to '%s'" %
  68. self.record)
  69. return r