build_scripts.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """distutils.command.build_scripts
  2. Implements the Distutils 'build_scripts' command."""
  3. __revision__ = "$Id: build_scripts.py 77704 2010-01-23 09:23:15Z tarek.ziade $"
  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. if f:
  65. f.close()
  66. self.copy_file(script, outfile)
  67. if os.name == 'posix':
  68. for file in outfiles:
  69. if self.dry_run:
  70. log.info("changing mode of %s", file)
  71. else:
  72. oldmode = os.stat(file)[ST_MODE] & 07777
  73. newmode = (oldmode | 0555) & 07777
  74. if newmode != oldmode:
  75. log.info("changing mode of %s from %o to %o",
  76. file, oldmode, newmode)
  77. os.chmod(file, newmode)
  78. # copy_scripts ()
  79. # class build_scripts