build_scripts.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. """Copy each script listed in 'self.scripts'
  41. """
  42. self.mkpath(self.build_dir)
  43. outfiles = []
  44. updated_files = []
  45. for script in self.scripts:
  46. adjust = False
  47. script = convert_path(script)
  48. outfile = os.path.join(self.build_dir, os.path.basename(script))
  49. outfiles.append(outfile)
  50. if not self.force and not newer(script, outfile):
  51. log.debug("not copying %s (up-to-date)", script)
  52. continue
  53. # Always open the file, but ignore failures in dry-run mode --
  54. # that way, we'll get accurate feedback if we can read the
  55. # script.
  56. try:
  57. f = open(script, "rb")
  58. except OSError:
  59. if not self.dry_run:
  60. raise
  61. f = None
  62. if f:
  63. f.close()
  64. updated_files.append(outfile)
  65. self.copy_file(script, outfile)
  66. if os.name == 'posix':
  67. for file in outfiles:
  68. if self.dry_run:
  69. log.info("changing mode of %s", file)
  70. else:
  71. oldmode = os.stat(file)[ST_MODE] & 0o7777
  72. newmode = (oldmode | 0o555) & 0o7777
  73. if newmode != oldmode:
  74. log.info("changing mode of %s from %o to %o",
  75. file, oldmode, newmode)
  76. os.chmod(file, newmode)
  77. # XXX should we modify self.outfiles?
  78. return outfiles, updated_files
  79. class build_scripts_2to3(build_scripts, Mixin2to3):
  80. def copy_scripts(self):
  81. outfiles, updated_files = build_scripts.copy_scripts(self)
  82. if not self.dry_run:
  83. self.run_2to3(updated_files)
  84. return outfiles, updated_files