build_scripts.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. """distutils.command.build_scripts
  2. Implements the Distutils 'build_scripts' command."""
  3. import os
  4. import re
  5. from stat import ST_MODE
  6. from distutils import sysconfig
  7. from distutils.core import Command
  8. from distutils.dep_util import newer
  9. from distutils.util import convert_path
  10. from distutils import log
  11. import tokenize
  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 = [
  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 = ['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): # noqa: C901
  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("%s is an empty file (skipping)" % script)
  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. if not sysconfig.python_build:
  87. executable = self.executable
  88. else:
  89. executable = os.path.join(
  90. sysconfig.get_config_var("BINDIR"),
  91. "python%s%s"
  92. % (
  93. sysconfig.get_config_var("VERSION"),
  94. sysconfig.get_config_var("EXE"),
  95. ),
  96. )
  97. post_interp = shebang_match.group(1) or ''
  98. shebang = "#!" + executable + post_interp + "\n"
  99. self._validate_shebang(shebang, f.encoding)
  100. with open(outfile, "w", encoding=f.encoding) as outf:
  101. outf.write(shebang)
  102. outf.writelines(f.readlines())
  103. if f:
  104. f.close()
  105. else:
  106. if f:
  107. f.close()
  108. self.copy_file(script, outfile)
  109. def _change_modes(self, outfiles):
  110. if os.name != 'posix':
  111. return
  112. for file in outfiles:
  113. self._change_mode(file)
  114. def _change_mode(self, file):
  115. if self.dry_run:
  116. log.info("changing mode of %s", file)
  117. return
  118. oldmode = os.stat(file)[ST_MODE] & 0o7777
  119. newmode = (oldmode | 0o555) & 0o7777
  120. if newmode != oldmode:
  121. log.info("changing mode of %s from %o to %o", file, oldmode, newmode)
  122. os.chmod(file, newmode)
  123. @staticmethod
  124. def _validate_shebang(shebang, encoding):
  125. # Python parser starts to read a script using UTF-8 until
  126. # it gets a #coding:xxx cookie. The shebang has to be the
  127. # first line of a file, the #coding:xxx cookie cannot be
  128. # written before. So the shebang has to be encodable to
  129. # UTF-8.
  130. try:
  131. shebang.encode('utf-8')
  132. except UnicodeEncodeError:
  133. raise ValueError(
  134. "The shebang ({!r}) is not encodable " "to utf-8".format(shebang)
  135. )
  136. # If the script is encoded to a custom encoding (use a
  137. # #coding:xxx cookie), the shebang has to be encodable to
  138. # the script encoding too.
  139. try:
  140. shebang.encode(encoding)
  141. except UnicodeEncodeError:
  142. raise ValueError(
  143. "The shebang ({!r}) is not encodable "
  144. "to the script encoding ({})".format(shebang, encoding)
  145. )