build_scripts.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. """distutils.command.build_scripts
  2. Implements the Distutils 'build_scripts' command."""
  3. import os
  4. import re
  5. import tokenize
  6. from distutils._log import log
  7. from stat import ST_MODE
  8. from typing import ClassVar
  9. from .._modified import newer
  10. from ..core import Command
  11. from ..util import convert_path
  12. shebang_pattern = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
  13. """
  14. Pattern matching a Python interpreter indicated in first line of a script.
  15. """
  16. # for Setuptools compatibility
  17. first_line_re = shebang_pattern
  18. class build_scripts(Command):
  19. description = "\"build\" scripts (copy and fixup #! line)"
  20. user_options: ClassVar[list[tuple[str, str, str]]] = [
  21. ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  22. ('force', 'f', "forcibly build everything (ignore file timestamps"),
  23. ('executable=', 'e', "specify final destination interpreter path"),
  24. ]
  25. boolean_options: ClassVar[list[str]] = ['force']
  26. def initialize_options(self):
  27. self.build_dir = None
  28. self.scripts = None
  29. self.force = None
  30. self.executable = None
  31. def finalize_options(self):
  32. self.set_undefined_options(
  33. 'build',
  34. ('build_scripts', 'build_dir'),
  35. ('force', 'force'),
  36. ('executable', 'executable'),
  37. )
  38. self.scripts = self.distribution.scripts
  39. def get_source_files(self):
  40. return self.scripts
  41. def run(self):
  42. if not self.scripts:
  43. return
  44. self.copy_scripts()
  45. def copy_scripts(self):
  46. """
  47. Copy each script listed in ``self.scripts``.
  48. If a script is marked as a Python script (first line matches
  49. 'shebang_pattern', i.e. starts with ``#!`` and contains
  50. "python"), then adjust in the copy the first line to refer to
  51. the current Python interpreter.
  52. """
  53. self.mkpath(self.build_dir)
  54. outfiles = []
  55. updated_files = []
  56. for script in self.scripts:
  57. self._copy_script(script, outfiles, updated_files)
  58. self._change_modes(outfiles)
  59. return outfiles, updated_files
  60. def _copy_script(self, script, outfiles, updated_files):
  61. shebang_match = None
  62. script = convert_path(script)
  63. outfile = os.path.join(self.build_dir, os.path.basename(script))
  64. outfiles.append(outfile)
  65. if not self.force and not newer(script, outfile):
  66. log.debug("not copying %s (up-to-date)", script)
  67. return
  68. # Always open the file, but ignore failures in dry-run mode
  69. # in order to attempt to copy directly.
  70. f = tokenize.open(script)
  71. first_line = f.readline()
  72. if not first_line:
  73. self.warn(f"{script} is an empty file (skipping)")
  74. return
  75. shebang_match = shebang_pattern.match(first_line)
  76. updated_files.append(outfile)
  77. if shebang_match:
  78. log.info("copying and adjusting %s -> %s", script, self.build_dir)
  79. post_interp = shebang_match.group(1) or ''
  80. shebang = "#!" + self.executable + post_interp + "\n"
  81. self._validate_shebang(shebang, f.encoding)
  82. with open(outfile, "w", encoding=f.encoding) as outf:
  83. outf.write(shebang)
  84. outf.writelines(f.readlines())
  85. if f:
  86. f.close()
  87. else:
  88. if f:
  89. f.close()
  90. self.copy_file(script, outfile)
  91. def _change_modes(self, outfiles):
  92. if os.name != 'posix':
  93. return
  94. for file in outfiles:
  95. self._change_mode(file)
  96. def _change_mode(self, file):
  97. oldmode = os.stat(file)[ST_MODE] & 0o7777
  98. newmode = (oldmode | 0o555) & 0o7777
  99. if newmode != oldmode:
  100. log.info("changing mode of %s from %o to %o", file, oldmode, newmode)
  101. os.chmod(file, newmode)
  102. @staticmethod
  103. def _validate_shebang(shebang, encoding):
  104. # Python parser starts to read a script using UTF-8 until
  105. # it gets a #coding:xxx cookie. The shebang has to be the
  106. # first line of a file, the #coding:xxx cookie cannot be
  107. # written before. So the shebang has to be encodable to
  108. # UTF-8.
  109. try:
  110. shebang.encode('utf-8')
  111. except UnicodeEncodeError:
  112. raise ValueError(f"The shebang ({shebang!r}) is not encodable to utf-8")
  113. # If the script is encoded to a custom encoding (use a
  114. # #coding:xxx cookie), the shebang has to be encodable to
  115. # the script encoding too.
  116. try:
  117. shebang.encode(encoding)
  118. except UnicodeEncodeError:
  119. raise ValueError(
  120. f"The shebang ({shebang!r}) is not encodable "
  121. f"to the script encoding ({encoding})"
  122. )