blob: fb73719f57e00e2f48690f41537991740f859f28 [file] [log] [blame]
Greg Warda4efe652000-05-25 01:20:15 +00001"""distutils.command.build_scripts
2
3Implements the Distutils 'build_scripts' command."""
4
Andrew M. Kuchlingd448f662002-11-19 13:12:28 +00005# This module should be kept compatible with Python 1.5.2.
6
Greg Warda4efe652000-05-25 01:20:15 +00007__revision__ = "$Id$"
8
Greg Ward80fa55e2000-05-25 02:03:56 +00009import sys, os, re
Guido van Rossumbcb0e202003-01-24 14:56:52 +000010from stat import ST_MODE
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +000011from distutils import sysconfig
Greg Warda4efe652000-05-25 01:20:15 +000012from distutils.core import Command
Greg Ward80fa55e2000-05-25 02:03:56 +000013from distutils.dep_util import newer
Just van Rossum8d8e7a32001-07-29 21:39:18 +000014from distutils.util import convert_path
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000015from distutils import log
Greg Warda4efe652000-05-25 01:20:15 +000016
Greg Ward7cf7e7e2001-07-25 20:20:11 +000017# check if Python is called on the first line with this expression
Gustavo Niemeyer6cf26192003-06-27 19:33:38 +000018first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
Greg Warda4efe652000-05-25 01:20:15 +000019
20class build_scripts (Command):
21
Greg Ward80fa55e2000-05-25 02:03:56 +000022 description = "\"build\" scripts (copy and fixup #! line)"
Greg Warda4efe652000-05-25 01:20:15 +000023
24 user_options = [
25 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
26 ('force', 'f', "forcibly build everything (ignore file timestamps"),
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000027 ('executable=', 'e', "specify final destination interpreter path"),
Greg Warda4efe652000-05-25 01:20:15 +000028 ]
29
Greg Ward99b032e2000-09-25 01:41:15 +000030 boolean_options = ['force']
31
Greg Warda4efe652000-05-25 01:20:15 +000032
33 def initialize_options (self):
34 self.build_dir = None
35 self.scripts = None
36 self.force = None
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000037 self.executable = None
Greg Warda4efe652000-05-25 01:20:15 +000038 self.outfiles = None
39
40 def finalize_options (self):
Greg Wardcb1f4c42000-09-30 18:27:54 +000041 self.set_undefined_options('build',
42 ('build_scripts', 'build_dir'),
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000043 ('force', 'force'),
44 ('executable', 'executable'))
Greg Warda4efe652000-05-25 01:20:15 +000045 self.scripts = self.distribution.scripts
46
Fred Drake4b498232004-03-25 22:04:52 +000047 def get_source_files(self):
48 return self.scripts
Greg Warda4efe652000-05-25 01:20:15 +000049
50 def run (self):
51 if not self.scripts:
52 return
Greg Ward80fa55e2000-05-25 02:03:56 +000053 self.copy_scripts()
54
Fred Drake53a79062001-03-02 07:28:03 +000055
Greg Ward80fa55e2000-05-25 02:03:56 +000056 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
Greg Ward612eb9f2000-07-27 02:13:20 +000060 line to refer to the current Python interpreter as we copy.
Greg Ward80fa55e2000-05-25 02:03:56 +000061 """
Greg Warda4efe652000-05-25 01:20:15 +000062 self.mkpath(self.build_dir)
Guido van Rossumbcb0e202003-01-24 14:56:52 +000063 outfiles = []
Greg Ward80fa55e2000-05-25 02:03:56 +000064 for script in self.scripts:
65 adjust = 0
Just van Rossum8d8e7a32001-07-29 21:39:18 +000066 script = convert_path(script)
Greg Wardfe55e862000-05-25 20:05:52 +000067 outfile = os.path.join(self.build_dir, os.path.basename(script))
Guido van Rossumbcb0e202003-01-24 14:56:52 +000068 outfiles.append(outfile)
Greg Ward80fa55e2000-05-25 02:03:56 +000069
70 if not self.force and not newer(script, outfile):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000071 log.debug("not copying %s (up-to-date)", script)
Greg Ward80fa55e2000-05-25 02:03:56 +000072 continue
73
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
76 # script.
77 try:
78 f = open(script, "r")
79 except IOError:
80 if not self.dry_run:
81 raise
82 f = None
83 else:
84 first_line = f.readline()
85 if not first_line:
86 self.warn("%s is an empty file (skipping)" % script)
Greg Warda4efe652000-05-25 01:20:15 +000087 continue
Greg Ward80fa55e2000-05-25 02:03:56 +000088
89 match = first_line_re.match(first_line)
90 if match:
91 adjust = 1
Marc-André Lemburg90294d02001-12-11 20:44:42 +000092 post_interp = match.group(1) or ''
Greg Ward80fa55e2000-05-25 02:03:56 +000093
94 if adjust:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000095 log.info("copying and adjusting %s -> %s", script,
96 self.build_dir)
Greg Ward80fa55e2000-05-25 02:03:56 +000097 if not self.dry_run:
98 outf = open(outfile, "w")
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +000099 if not sysconfig.python_build:
Tim Peters182b5ac2004-07-18 06:16:08 +0000100 outf.write("#!%s%s\n" %
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +0000101 (self.executable,
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +0000102 post_interp))
103 else:
Gustavo Niemeyer6cf26192003-06-27 19:33:38 +0000104 outf.write("#!%s%s\n" %
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +0000105 (os.path.join(
106 sysconfig.get_config_var("BINDIR"),
107 "python" + sysconfig.get_config_var("EXE")),
108 post_interp))
Greg Ward80fa55e2000-05-25 02:03:56 +0000109 outf.writelines(f.readlines())
110 outf.close()
111 if f:
112 f.close()
113 else:
114 f.close()
115 self.copy_file(script, outfile)
116
Guido van Rossumbcb0e202003-01-24 14:56:52 +0000117 if os.name == 'posix':
118 for file in outfiles:
119 if self.dry_run:
120 log.info("changing mode of %s", file)
121 else:
Guido van Rossumd3590f92003-01-29 16:58:31 +0000122 oldmode = os.stat(file)[ST_MODE] & 07777
123 newmode = (oldmode | 0555) & 07777
124 if newmode != oldmode:
125 log.info("changing mode of %s from %o to %o",
126 file, oldmode, newmode)
127 os.chmod(file, newmode)
Guido van Rossumbcb0e202003-01-24 14:56:52 +0000128
Greg Ward80fa55e2000-05-25 02:03:56 +0000129 # copy_scripts ()
130
131# class build_scripts