install_scripts.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """distutils.command.install_scripts
  2. Implements the Distutils 'install_scripts' command, for installing
  3. Python scripts."""
  4. # contributed by Bastian Kleineidam
  5. import os
  6. from distutils.core import Command
  7. from distutils import log
  8. from stat import ST_MODE
  9. class install_scripts(Command):
  10. description = "install scripts (Python or otherwise)"
  11. user_options = [
  12. ('install-dir=', 'd', "directory to install scripts to"),
  13. ('build-dir=', 'b', "build directory (where to install from)"),
  14. ('force', 'f', "force installation (overwrite existing files)"),
  15. ('skip-build', None, "skip the build steps"),
  16. ]
  17. boolean_options = ['force', 'skip-build']
  18. def initialize_options(self):
  19. self.install_dir = None
  20. self.force = 0
  21. self.build_dir = None
  22. self.skip_build = None
  23. def finalize_options(self):
  24. self.set_undefined_options('build', ('build_scripts', 'build_dir'))
  25. self.set_undefined_options(
  26. 'install',
  27. ('install_scripts', 'install_dir'),
  28. ('force', 'force'),
  29. ('skip_build', 'skip_build'),
  30. )
  31. def run(self):
  32. if not self.skip_build:
  33. self.run_command('build_scripts')
  34. self.outfiles = self.copy_tree(self.build_dir, self.install_dir)
  35. if os.name == 'posix':
  36. # Set the executable bits (owner, group, and world) on
  37. # all the scripts we just installed.
  38. for file in self.get_outputs():
  39. if self.dry_run:
  40. log.info("changing mode of %s", file)
  41. else:
  42. mode = ((os.stat(file)[ST_MODE]) | 0o555) & 0o7777
  43. log.info("changing mode of %s to %o", file, mode)
  44. os.chmod(file, mode)
  45. def get_inputs(self):
  46. return self.distribution.scripts or []
  47. def get_outputs(self):
  48. return self.outfiles or []