install_scripts.py 2.6 KB

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