build_scripts.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. try:
  71. f = tokenize.open(script)
  72. except OSError:
  73. if not self.dry_run:
  74. raise
  75. f = None
  76. else:
  77. first_line = f.readline()
  78. if not first_line:
  79. self.warn(f"{script} is an empty file (skipping)")
  80. return
  81. shebang_match = shebang_pattern.match(first_line)
  82. updated_files.append(outfile)
  83. if shebang_match:
  84. log.info("copying and adjusting %s -> %s", script, self.build_dir)
  85. if not self.dry_run:
  86. post_interp = shebang_match.group(1) or ''
  87. shebang = "#!" + self.executable + post_interp + "\n"
  88. self._validate_shebang(shebang, f.encoding)
  89. with open(outfile, "w", encoding=f.encoding) as outf:
  90. outf.write(shebang)
  91. outf.writelines(f.readlines())
  92. if f:
  93. f.close()
  94. else:
  95. if f:
  96. f.close()
  97. self.copy_file(script, outfile)
  98. def _change_modes(self, outfiles):
  99. if os.name != 'posix':
  100. return
  101. for file in outfiles:
  102. self._change_mode(file)
  103. def _change_mode(self, file):
  104. if self.dry_run:
  105. log.info("changing mode of %s", file)
  106. return
  107. oldmode = os.stat(file)[ST_MODE] & 0o7777
  108. newmode = (oldmode | 0o555) & 0o7777
  109. if newmode != oldmode:
  110. log.info("changing mode of %s from %o to %o", file, oldmode, newmode)
  111. os.chmod(file, newmode)
  112. @staticmethod
  113. def _validate_shebang(shebang, encoding):
  114. # Python parser starts to read a script using UTF-8 until
  115. # it gets a #coding:xxx cookie. The shebang has to be the
  116. # first line of a file, the #coding:xxx cookie cannot be
  117. # written before. So the shebang has to be encodable to
  118. # UTF-8.
  119. try:
  120. shebang.encode('utf-8')
  121. except UnicodeEncodeError:
  122. raise ValueError(f"The shebang ({shebang!r}) is not encodable to utf-8")
  123. # If the script is encoded to a custom encoding (use a
  124. # #coding:xxx cookie), the shebang has to be encodable to
  125. # the script encoding too.
  126. try:
  127. shebang.encode(encoding)
  128. except UnicodeEncodeError:
  129. raise ValueError(
  130. f"The shebang ({shebang!r}) is not encodable "
  131. f"to the script encoding ({encoding})"
  132. )