install_scripts.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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('install',
  26. ('install_scripts', 'install_dir'),
  27. ('force', 'force'),
  28. ('skip_build', 'skip_build'),
  29. )
  30. def run(self):
  31. if not self.skip_build:
  32. self.run_command('build_scripts')
  33. self.outfiles = self.copy_tree(self.build_dir, self.install_dir)
  34. if os.name == 'posix':
  35. # Set the executable bits (owner, group, and world) on
  36. # all the scripts we just installed.
  37. for file in self.get_outputs():
  38. if self.dry_run:
  39. log.info("changing mode of %s", file)
  40. else:
  41. mode = ((os.stat(file)[ST_MODE]) | 0o555) & 0o7777
  42. log.info("changing mode of %s to %o", file, mode)
  43. os.chmod(file, mode)
  44. def get_inputs(self):
  45. return self.distribution.scripts or []
  46. def get_outputs(self):
  47. return self.outfiles or []