1 """distutils.command.build_scripts
3 Implements the Distutils 'build_scripts' command."""
5 # This module should be kept compatible with Python 2.1.
7 __revision__ = "$Id: build_scripts.py 69599 2009-02-13 23:02:44Z tarek.ziade $"
10 from stat import ST_MODE
11 from distutils import sysconfig
12 from distutils.core import Command
13 from distutils.dep_util import newer
14 from distutils.util import convert_path
15 from distutils import log
17 # check if Python is called on the first line with this expression
18 first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
20 class build_scripts (Command):
22 description = "\"build\" scripts (copy and fixup #! line)"
25 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
26 ('force', 'f', "forcibly build everything (ignore file timestamps"),
27 ('executable=', 'e', "specify final destination interpreter path"),
30 boolean_options = ['force']
33 def initialize_options (self):
37 self.executable = None
40 def finalize_options (self):
41 self.set_undefined_options('build',
42 ('build_scripts', 'build_dir'),
44 ('executable', 'executable'))
45 self.scripts = self.distribution.scripts
47 def get_source_files(self):
56 def copy_scripts (self):
57 """Copy each script listed in 'self.scripts'; if it's marked as a
58 Python script in the Unix way (first line matches 'first_line_re',
59 ie. starts with "\#!" and contains "python"), then adjust the first
60 line to refer to the current Python interpreter as we copy.
62 self.mkpath(self.build_dir)
64 for script in self.scripts:
66 script = convert_path(script)
67 outfile = os.path.join(self.build_dir, os.path.basename(script))
68 outfiles.append(outfile)
70 if not self.force and not newer(script, outfile):
71 log.debug("not copying %s (up-to-date)", script)
74 # Always open the file, but ignore failures in dry-run mode --
75 # that way, we'll get accurate feedback if we can read the
84 first_line = f.readline()
86 self.warn("%s is an empty file (skipping)" % script)
89 match = first_line_re.match(first_line)
92 post_interp = match.group(1) or ''
95 log.info("copying and adjusting %s -> %s", script,
98 outf = open(outfile, "w")
99 if not sysconfig.python_build:
100 outf.write("#!%s%s\n" %
104 outf.write("#!%s%s\n" %
106 sysconfig.get_config_var("BINDIR"),
107 "python%s%s" % (sysconfig.get_config_var("VERSION"),
108 sysconfig.get_config_var("EXE"))),
110 outf.writelines(f.readlines())
117 self.copy_file(script, outfile)
119 if os.name == 'posix':
120 for file in outfiles:
122 log.info("changing mode of %s", file)
124 oldmode = os.stat(file)[ST_MODE] & 07777
125 newmode = (oldmode | 0555) & 07777
126 if newmode != oldmode:
127 log.info("changing mode of %s from %o to %o",
128 file, oldmode, newmode)
129 os.chmod(file, newmode)
133 # class build_scripts