build_scripts.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """distutils.command.build_scripts
  2. Implements the Distutils 'build_scripts' command."""
  3. import os, re
  4. from stat import ST_MODE
  5. from distutils import sysconfig
  6. from distutils.core import Command
  7. from distutils.dep_util import newer
  8. from distutils.util import convert_path, Mixin2to3
  9. from distutils import log
  10. import tokenize
  11. # check if Python is called on the first line with this expression
  12. first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$')
  13. class build_scripts(Command):
  14. description = "\"build\" scripts (copy and fixup #! line)"
  15. user_options = [
  16. ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  17. ('force', 'f', "forcibly build everything (ignore file timestamps"),
  18. ('executable=', 'e', "specify final destination interpreter path"),
  19. ]
  20. boolean_options = ['force']
  21. def initialize_options(self):
  22. self.build_dir = None
  23. self.scripts = None
  24. self.force = None
  25. self.executable = None
  26. self.outfiles = None
  27. def finalize_options(self):
  28. self.set_undefined_options('build',
  29. ('build_scripts', 'build_dir'),
  30. ('force', 'force'),
  31. ('executable', 'executable'))
  32. self.scripts = self.distribution.scripts
  33. def get_source_files(self):
  34. return self.scripts
  35. def run(self):
  36. if not self.scripts:
  37. return
  38. self.copy_scripts()
  39. def copy_scripts(self):
  40. r"""Copy each script listed in 'self.scripts'; if it's marked as a
  41. Python script in the Unix way (first line matches 'first_line_re',
  42. ie. starts with "\#!" and contains "python"), then adjust the first
  43. line to refer to the current Python interpreter as we copy.
  44. """
  45. self.mkpath(self.build_dir)
  46. outfiles = []
  47. updated_files = []
  48. for script in self.scripts:
  49. adjust = False
  50. script = convert_path(script)
  51. outfile = os.path.join(self.build_dir, os.path.basename(script))
  52. outfiles.append(outfile)
  53. if not self.force and not newer(script, outfile):
  54. log.debug("not copying %s (up-to-date)", script)
  55. continue
  56. # Always open the file, but ignore failures in dry-run mode --
  57. # that way, we'll get accurate feedback if we can read the
  58. # script.
  59. try:
  60. f = open(script, "rb")
  61. except OSError:
  62. if not self.dry_run:
  63. raise
  64. f = None
  65. else:
  66. encoding, lines = tokenize.detect_encoding(f.readline)
  67. f.seek(0)
  68. first_line = f.readline()
  69. if not first_line:
  70. self.warn("%s is an empty file (skipping)" % script)
  71. continue
  72. match = first_line_re.match(first_line)
  73. if match:
  74. adjust = True
  75. post_interp = match.group(1) or b''
  76. if adjust:
  77. log.info("copying and adjusting %s -> %s", script,
  78. self.build_dir)
  79. updated_files.append(outfile)
  80. if not self.dry_run:
  81. if not sysconfig.python_build:
  82. executable = self.executable
  83. else:
  84. executable = os.path.join(
  85. sysconfig.get_config_var("BINDIR"),
  86. "python%s%s" % (sysconfig.get_config_var("VERSION"),
  87. sysconfig.get_config_var("EXE")))
  88. executable = os.fsencode(executable)
  89. shebang = b"#!" + executable + post_interp + b"\n"
  90. # Python parser starts to read a script using UTF-8 until
  91. # it gets a #coding:xxx cookie. The shebang has to be the
  92. # first line of a file, the #coding:xxx cookie cannot be
  93. # written before. So the shebang has to be decodable from
  94. # UTF-8.
  95. try:
  96. shebang.decode('utf-8')
  97. except UnicodeDecodeError:
  98. raise ValueError(
  99. "The shebang ({!r}) is not decodable "
  100. "from utf-8".format(shebang))
  101. # If the script is encoded to a custom encoding (use a
  102. # #coding:xxx cookie), the shebang has to be decodable from
  103. # the script encoding too.
  104. try:
  105. shebang.decode(encoding)
  106. except UnicodeDecodeError:
  107. raise ValueError(
  108. "The shebang ({!r}) is not decodable "
  109. "from the script encoding ({})"
  110. .format(shebang, encoding))
  111. with open(outfile, "wb") as outf:
  112. outf.write(shebang)
  113. outf.writelines(f.readlines())
  114. if f:
  115. f.close()
  116. else:
  117. if f:
  118. f.close()
  119. updated_files.append(outfile)
  120. self.copy_file(script, outfile)
  121. if os.name == 'posix':
  122. for file in outfiles:
  123. if self.dry_run:
  124. log.info("changing mode of %s", file)
  125. else:
  126. oldmode = os.stat(file)[ST_MODE] & 0o7777
  127. newmode = (oldmode | 0o555) & 0o7777
  128. if newmode != oldmode:
  129. log.info("changing mode of %s from %o to %o",
  130. file, oldmode, newmode)
  131. os.chmod(file, newmode)
  132. # XXX should we modify self.outfiles?
  133. return outfiles, updated_files
  134. class build_scripts_2to3(build_scripts, Mixin2to3):
  135. def copy_scripts(self):
  136. outfiles, updated_files = build_scripts.copy_scripts(self)
  137. if not self.dry_run:
  138. self.run_2to3(updated_files)
  139. return outfiles, updated_files