/Lib/distutils/command/build_scripts.py

http://unladen-swallow.googlecode.com/ · Python · 133 lines · 105 code · 14 blank · 14 comment · 13 complexity · 7ca58818d45e6b18631afa13cf45fcd8 MD5 · raw file

  1. """distutils.command.build_scripts
  2. Implements the Distutils 'build_scripts' command."""
  3. # This module should be kept compatible with Python 2.1.
  4. __revision__ = "$Id: build_scripts.py 69599 2009-02-13 23:02:44Z tarek.ziade $"
  5. import os, re
  6. from stat import ST_MODE
  7. from distutils import sysconfig
  8. from distutils.core import Command
  9. from distutils.dep_util import newer
  10. from distutils.util import convert_path
  11. from distutils import log
  12. # check if Python is called on the first line with this expression
  13. first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
  14. class build_scripts (Command):
  15. description = "\"build\" scripts (copy and fixup #! line)"
  16. user_options = [
  17. ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  18. ('force', 'f', "forcibly build everything (ignore file timestamps"),
  19. ('executable=', 'e', "specify final destination interpreter path"),
  20. ]
  21. boolean_options = ['force']
  22. def initialize_options (self):
  23. self.build_dir = None
  24. self.scripts = None
  25. self.force = None
  26. self.executable = None
  27. self.outfiles = None
  28. def finalize_options (self):
  29. self.set_undefined_options('build',
  30. ('build_scripts', 'build_dir'),
  31. ('force', 'force'),
  32. ('executable', 'executable'))
  33. self.scripts = self.distribution.scripts
  34. def get_source_files(self):
  35. return self.scripts
  36. def run (self):
  37. if not self.scripts:
  38. return
  39. self.copy_scripts()
  40. def copy_scripts (self):
  41. """Copy each script listed in 'self.scripts'; if it's marked as a
  42. Python script in the Unix way (first line matches 'first_line_re',
  43. ie. starts with "\#!" and contains "python"), then adjust the first
  44. line to refer to the current Python interpreter as we copy.
  45. """
  46. self.mkpath(self.build_dir)
  47. outfiles = []
  48. for script in self.scripts:
  49. adjust = 0
  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, "r")
  61. except IOError:
  62. if not self.dry_run:
  63. raise
  64. f = None
  65. else:
  66. first_line = f.readline()
  67. if not first_line:
  68. self.warn("%s is an empty file (skipping)" % script)
  69. continue
  70. match = first_line_re.match(first_line)
  71. if match:
  72. adjust = 1
  73. post_interp = match.group(1) or ''
  74. if adjust:
  75. log.info("copying and adjusting %s -> %s", script,
  76. self.build_dir)
  77. if not self.dry_run:
  78. outf = open(outfile, "w")
  79. if not sysconfig.python_build:
  80. outf.write("#!%s%s\n" %
  81. (self.executable,
  82. post_interp))
  83. else:
  84. outf.write("#!%s%s\n" %
  85. (os.path.join(
  86. sysconfig.get_config_var("BINDIR"),
  87. "python%s%s" % (sysconfig.get_config_var("VERSION"),
  88. sysconfig.get_config_var("EXE"))),
  89. post_interp))
  90. outf.writelines(f.readlines())
  91. outf.close()
  92. if f:
  93. f.close()
  94. else:
  95. if f:
  96. f.close()
  97. self.copy_file(script, outfile)
  98. if os.name == 'posix':
  99. for file in outfiles:
  100. if self.dry_run:
  101. log.info("changing mode of %s", file)
  102. else:
  103. oldmode = os.stat(file)[ST_MODE] & 07777
  104. newmode = (oldmode | 0555) & 07777
  105. if newmode != oldmode:
  106. log.info("changing mode of %s from %o to %o",
  107. file, oldmode, newmode)
  108. os.chmod(file, newmode)
  109. # copy_scripts ()
  110. # class build_scripts