install_scripts.py 1.8 KB

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