blob: 31be7930d738e428fc81dbaebe11cad7fac354cc [file] [log] [blame]
Greg Warda4efe652000-05-25 01:20:15 +00001"""distutils.command.build_scripts
2
3Implements the Distutils 'build_scripts' command."""
4
Greg Warda4efe652000-05-25 01:20:15 +00005__revision__ = "$Id$"
6
Christian Heimes05e8be12008-02-23 18:30:17 +00007import os, re
Guido van Rossumbcb0e202003-01-24 14:56:52 +00008from stat import ST_MODE
Tarek Ziadé36797272010-07-22 12:50:05 +00009from distutils import sysconfig
Greg Warda4efe652000-05-25 01:20:15 +000010from distutils.core import Command
Greg Ward80fa55e2000-05-25 02:03:56 +000011from distutils.dep_util import newer
Martin v. Löwis6178db62008-12-01 04:38:52 +000012from distutils.util import convert_path, Mixin2to3
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000013from distutils import log
Victor Stinner19474772011-05-11 00:14:28 +020014import tokenize
Greg Warda4efe652000-05-25 01:20:15 +000015
Greg Ward7cf7e7e2001-07-25 20:20:11 +000016# check if Python is called on the first line with this expression
Victor Stinner19474772011-05-11 00:14:28 +020017first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$')
Greg Warda4efe652000-05-25 01:20:15 +000018
Collin Winter5b7e9d72007-08-30 03:52:21 +000019class build_scripts(Command):
Greg Warda4efe652000-05-25 01:20:15 +000020
Greg Ward80fa55e2000-05-25 02:03:56 +000021 description = "\"build\" scripts (copy and fixup #! line)"
Greg Warda4efe652000-05-25 01:20:15 +000022
23 user_options = [
24 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
25 ('force', 'f', "forcibly build everything (ignore file timestamps"),
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000026 ('executable=', 'e', "specify final destination interpreter path"),
Greg Warda4efe652000-05-25 01:20:15 +000027 ]
28
Greg Ward99b032e2000-09-25 01:41:15 +000029 boolean_options = ['force']
30
Greg Warda4efe652000-05-25 01:20:15 +000031
Collin Winter5b7e9d72007-08-30 03:52:21 +000032 def initialize_options(self):
Greg Warda4efe652000-05-25 01:20:15 +000033 self.build_dir = None
34 self.scripts = None
35 self.force = None
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000036 self.executable = None
Greg Warda4efe652000-05-25 01:20:15 +000037 self.outfiles = None
38
Collin Winter5b7e9d72007-08-30 03:52:21 +000039 def finalize_options(self):
Greg Wardcb1f4c42000-09-30 18:27:54 +000040 self.set_undefined_options('build',
41 ('build_scripts', 'build_dir'),
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000042 ('force', 'force'),
43 ('executable', 'executable'))
Greg Warda4efe652000-05-25 01:20:15 +000044 self.scripts = self.distribution.scripts
45
Fred Drake4b498232004-03-25 22:04:52 +000046 def get_source_files(self):
47 return self.scripts
Greg Warda4efe652000-05-25 01:20:15 +000048
Collin Winter5b7e9d72007-08-30 03:52:21 +000049 def run(self):
Greg Warda4efe652000-05-25 01:20:15 +000050 if not self.scripts:
51 return
Greg Ward80fa55e2000-05-25 02:03:56 +000052 self.copy_scripts()
53
Fred Drake53a79062001-03-02 07:28:03 +000054
Collin Winter5b7e9d72007-08-30 03:52:21 +000055 def copy_scripts(self):
Greg Ward80fa55e2000-05-25 02:03:56 +000056 """Copy each script listed in 'self.scripts'; if it's marked as a
57 Python script in the Unix way (first line matches 'first_line_re',
58 ie. starts with "\#!" and contains "python"), then adjust the first
Greg Ward612eb9f2000-07-27 02:13:20 +000059 line to refer to the current Python interpreter as we copy.
Greg Ward80fa55e2000-05-25 02:03:56 +000060 """
Greg Warda4efe652000-05-25 01:20:15 +000061 self.mkpath(self.build_dir)
Guido van Rossumbcb0e202003-01-24 14:56:52 +000062 outfiles = []
Martin v. Löwis6178db62008-12-01 04:38:52 +000063 updated_files = []
Greg Ward80fa55e2000-05-25 02:03:56 +000064 for script in self.scripts:
Collin Winter5b7e9d72007-08-30 03:52:21 +000065 adjust = False
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:
Victor Stinner19474772011-05-11 00:14:28 +020078 f = open(script, "rb")
Greg Ward80fa55e2000-05-25 02:03:56 +000079 except IOError:
80 if not self.dry_run:
81 raise
82 f = None
83 else:
Victor Stinner19474772011-05-11 00:14:28 +020084 encoding, lines = tokenize.detect_encoding(f.readline)
85 f.seek(0)
Greg Ward80fa55e2000-05-25 02:03:56 +000086 first_line = f.readline()
87 if not first_line:
88 self.warn("%s is an empty file (skipping)" % script)
Greg Warda4efe652000-05-25 01:20:15 +000089 continue
Greg Ward80fa55e2000-05-25 02:03:56 +000090
91 match = first_line_re.match(first_line)
92 if match:
Collin Winter5b7e9d72007-08-30 03:52:21 +000093 adjust = True
Victor Stinner19474772011-05-11 00:14:28 +020094 post_interp = match.group(1) or b''
Greg Ward80fa55e2000-05-25 02:03:56 +000095
96 if adjust:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000097 log.info("copying and adjusting %s -> %s", script,
98 self.build_dir)
Martin v. Löwis6178db62008-12-01 04:38:52 +000099 updated_files.append(outfile)
Greg Ward80fa55e2000-05-25 02:03:56 +0000100 if not self.dry_run:
Tarek Ziadé36797272010-07-22 12:50:05 +0000101 if not sysconfig.python_build:
Victor Stinner19474772011-05-11 00:14:28 +0200102 executable = self.executable
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +0000103 else:
Victor Stinner19474772011-05-11 00:14:28 +0200104 executable = os.path.join(
Tarek Ziadé36797272010-07-22 12:50:05 +0000105 sysconfig.get_config_var("BINDIR"),
106 "python%s%s" % (sysconfig.get_config_var("VERSION"),
Victor Stinner19474772011-05-11 00:14:28 +0200107 sysconfig.get_config_var("EXE")))
108 executable = os.fsencode(executable)
109 shebang = b"#!" + executable + post_interp + b"\n"
110 # Python parser starts to read a script using UTF-8 until
111 # it gets a #coding:xxx cookie. The shebang has to be the
112 # first line of a file, the #coding:xxx cookie cannot be
113 # written before. So the shebang has to be decodable from
114 # UTF-8.
115 try:
116 shebang.decode('utf-8')
117 except UnicodeDecodeError:
118 raise ValueError(
119 "The shebang ({!r}) is not decodable "
120 "from utf-8".format(shebang))
121 # If the script is encoded to a custom encoding (use a
122 # #coding:xxx cookie), the shebang has to be decodable from
123 # the script encoding too.
124 try:
125 shebang.decode(encoding)
126 except UnicodeDecodeError:
127 raise ValueError(
128 "The shebang ({!r}) is not decodable "
129 "from the script encoding ({})"
130 .format(shebang, encoding))
Victor Stinnercfd365b2011-05-19 15:18:36 +0200131 with open(outfile, "wb") as outf:
132 outf.write(shebang)
133 outf.writelines(f.readlines())
Greg Ward80fa55e2000-05-25 02:03:56 +0000134 if f:
135 f.close()
136 else:
Christian Heimes072c0f12008-01-03 23:01:04 +0000137 if f:
138 f.close()
Martin v. Löwis6178db62008-12-01 04:38:52 +0000139 updated_files.append(outfile)
Greg Ward80fa55e2000-05-25 02:03:56 +0000140 self.copy_file(script, outfile)
141
Guido van Rossumbcb0e202003-01-24 14:56:52 +0000142 if os.name == 'posix':
143 for file in outfiles:
144 if self.dry_run:
145 log.info("changing mode of %s", file)
146 else:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000147 oldmode = os.stat(file)[ST_MODE] & 0o7777
148 newmode = (oldmode | 0o555) & 0o7777
Guido van Rossumd3590f92003-01-29 16:58:31 +0000149 if newmode != oldmode:
150 log.info("changing mode of %s from %o to %o",
151 file, oldmode, newmode)
152 os.chmod(file, newmode)
Martin v. Löwis6178db62008-12-01 04:38:52 +0000153 # XXX should we modify self.outfiles?
154 return outfiles, updated_files
155
156class build_scripts_2to3(build_scripts, Mixin2to3):
157
158 def copy_scripts(self):
159 outfiles, updated_files = build_scripts.copy_scripts(self)
160 if not self.dry_run:
161 self.run_2to3(updated_files)
162 return outfiles, updated_files