install_scripts.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from distutils import log
  2. import distutils.command.install_scripts as orig
  3. from distutils.errors import DistutilsModuleError
  4. import os
  5. import sys
  6. from pkg_resources import Distribution, PathMetadata, ensure_directory
  7. class install_scripts(orig.install_scripts):
  8. """Do normal script install, plus any egg_info wrapper scripts"""
  9. def initialize_options(self):
  10. orig.install_scripts.initialize_options(self)
  11. self.no_ep = False
  12. def run(self):
  13. import setuptools.command.easy_install as ei
  14. self.run_command("egg_info")
  15. if self.distribution.scripts:
  16. orig.install_scripts.run(self) # run first to set up self.outfiles
  17. else:
  18. self.outfiles = []
  19. if self.no_ep:
  20. # don't install entry point scripts into .egg file!
  21. return
  22. ei_cmd = self.get_finalized_command("egg_info")
  23. dist = Distribution(
  24. ei_cmd.egg_base, PathMetadata(ei_cmd.egg_base, ei_cmd.egg_info),
  25. ei_cmd.egg_name, ei_cmd.egg_version,
  26. )
  27. bs_cmd = self.get_finalized_command('build_scripts')
  28. exec_param = getattr(bs_cmd, 'executable', None)
  29. try:
  30. bw_cmd = self.get_finalized_command("bdist_wininst")
  31. is_wininst = getattr(bw_cmd, '_is_running', False)
  32. except (ImportError, DistutilsModuleError):
  33. is_wininst = False
  34. writer = ei.ScriptWriter
  35. if is_wininst:
  36. exec_param = "python.exe"
  37. writer = ei.WindowsScriptWriter
  38. if exec_param == sys.executable:
  39. # In case the path to the Python executable contains a space, wrap
  40. # it so it's not split up.
  41. exec_param = [exec_param]
  42. # resolve the writer to the environment
  43. writer = writer.best()
  44. cmd = writer.command_spec_class.best().from_param(exec_param)
  45. for args in writer.get_args(dist, cmd.as_header()):
  46. self.write_script(*args)
  47. def write_script(self, script_name, contents, mode="t", *ignored):
  48. """Write an executable file to the scripts directory"""
  49. from setuptools.command.easy_install import chmod, current_umask
  50. log.info("Installing %s script to %s", script_name, self.install_dir)
  51. target = os.path.join(self.install_dir, script_name)
  52. self.outfiles.append(target)
  53. mask = current_umask()
  54. if not self.dry_run:
  55. ensure_directory(target)
  56. f = open(target, "w" + mode)
  57. f.write(contents)
  58. f.close()
  59. chmod(target, 0o777 - mask)