install.py 3.1 KB

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