install_scripts.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. bw_cmd = self.get_finalized_command("bdist_wininst")
  29. is_wininst = getattr(bw_cmd, '_is_running', False)
  30. writer = ei.ScriptWriter
  31. if is_wininst:
  32. exec_param = "python.exe"
  33. writer = ei.WindowsScriptWriter
  34. if exec_param == sys.executable:
  35. # In case the path to the Python executable contains a space, wrap
  36. # it so it's not split up.
  37. exec_param = [exec_param]
  38. # resolve the writer to the environment
  39. writer = writer.best()
  40. cmd = writer.command_spec_class.best().from_param(exec_param)
  41. for args in writer.get_args(dist, cmd.as_header()):
  42. self.write_script(*args)
  43. def write_script(self, script_name, contents, mode="t", *ignored):
  44. """Write an executable file to the scripts directory"""
  45. from setuptools.command.easy_install import chmod, current_umask
  46. log.info("Installing %s script to %s", script_name, self.install_dir)
  47. target = os.path.join(self.install_dir, script_name)
  48. self.outfiles.append(target)
  49. mask = current_umask()
  50. if not self.dry_run:
  51. ensure_directory(target)
  52. f = open(target, "w" + mode)
  53. f.write(contents)
  54. f.close()
  55. chmod(target, 0o777 - mask)