install_scripts.py 2.5 KB

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