PageRenderTime 37ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib-python/modified-2.7/distutils/command/build_scripts.py

https://bitbucket.org/evelyn559/pypy
Python | 131 lines | 115 code | 12 blank | 4 comment | 12 complexity | c8ba5a7f6d5d7758024fbc7c6b16bd05 MD5 | raw file
  1. """distutils.command.build_scripts
  2. Implements the Distutils 'build_scripts' command."""
  3. __revision__ = "$Id$"
  4. import os, re
  5. from stat import ST_MODE
  6. from distutils.core import Command
  7. from distutils.dep_util import newer
  8. from distutils.util import convert_path
  9. from distutils import log
  10. # check if Python is called on the first line with this expression
  11. first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
  12. class build_scripts (Command):
  13. description = "\"build\" scripts (copy and fixup #! line)"
  14. user_options = [
  15. ('build-dir=', 'd', "directory to \"build\" (copy) to"),
  16. ('force', 'f', "forcibly build everything (ignore file timestamps"),
  17. ('executable=', 'e', "specify final destination interpreter path"),
  18. ]
  19. boolean_options = ['force']
  20. def initialize_options (self):
  21. self.build_dir = None
  22. self.scripts = None
  23. self.force = None
  24. self.executable = None
  25. self.outfiles = None
  26. def finalize_options (self):
  27. self.set_undefined_options('build',
  28. ('build_scripts', 'build_dir'),
  29. ('force', 'force'),
  30. ('executable', 'executable'))
  31. self.scripts = self.distribution.scripts
  32. def get_source_files(self):
  33. return self.scripts
  34. def run (self):
  35. if not self.scripts:
  36. return
  37. self.copy_scripts()
  38. def copy_scripts (self):
  39. """Copy each script listed in 'self.scripts'; if it's marked as a
  40. Python script in the Unix way (first line matches 'first_line_re',
  41. ie. starts with "\#!" and contains "python"), then adjust the first
  42. line to refer to the current Python interpreter as we copy.
  43. """
  44. _sysconfig = __import__('sysconfig')
  45. self.mkpath(self.build_dir)
  46. outfiles = []
  47. for script in self.scripts:
  48. adjust = 0
  49. script = convert_path(script)
  50. outfile = os.path.join(self.build_dir, os.path.basename(script))
  51. outfiles.append(outfile)
  52. if not self.force and not newer(script, outfile):
  53. log.debug("not copying %s (up-to-date)", script)
  54. continue
  55. # Always open the file, but ignore failures in dry-run mode --
  56. # that way, we'll get accurate feedback if we can read the
  57. # script.
  58. try:
  59. f = open(script, "r")
  60. except IOError:
  61. if not self.dry_run:
  62. raise
  63. f = None
  64. else:
  65. first_line = f.readline()
  66. if not first_line:
  67. self.warn("%s is an empty file (skipping)" % script)
  68. continue
  69. match = first_line_re.match(first_line)
  70. if match:
  71. adjust = 1
  72. post_interp = match.group(1) or ''
  73. if adjust:
  74. log.info("copying and adjusting %s -> %s", script,
  75. self.build_dir)
  76. if not self.dry_run:
  77. outf = open(outfile, "w")
  78. if not _sysconfig.is_python_build():
  79. outf.write("#!%s%s\n" %
  80. (self.executable,
  81. post_interp))
  82. else:
  83. outf.write("#!%s%s\n" %
  84. (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. post_interp))
  89. outf.writelines(f.readlines())
  90. outf.close()
  91. if f:
  92. f.close()
  93. else:
  94. if f:
  95. f.close()
  96. self.copy_file(script, outfile)
  97. if os.name == 'posix':
  98. for file in outfiles:
  99. if self.dry_run:
  100. log.info("changing mode of %s", file)
  101. else:
  102. oldmode = os.stat(file)[ST_MODE] & 07777
  103. newmode = (oldmode | 0555) & 07777
  104. if newmode != oldmode:
  105. log.info("changing mode of %s from %o to %o",
  106. file, oldmode, newmode)
  107. os.chmod(file, newmode)
  108. # copy_scripts ()
  109. # class build_scripts