install_scripts.py 2.3 KB

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