blob: 90a8380a04321a7b45d3a836860b68bcd9c1e62c [file] [log] [blame]
Greg Warda4efe652000-05-25 01:20:15 +00001"""distutils.command.build_scripts
2
3Implements the Distutils 'build_scripts' command."""
4
Christian Heimes05e8be12008-02-23 18:30:17 +00005import os, re
Guido van Rossumbcb0e202003-01-24 14:56:52 +00006from stat import ST_MODE
Tarek Ziadé36797272010-07-22 12:50:05 +00007from distutils import sysconfig
Greg Warda4efe652000-05-25 01:20:15 +00008from distutils.core import Command
Greg Ward80fa55e2000-05-25 02:03:56 +00009from distutils.dep_util import newer
Martin v. Löwis6178db62008-12-01 04:38:52 +000010from distutils.util import convert_path, Mixin2to3
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000011from distutils import log
Victor Stinner19474772011-05-11 00:14:28 +020012import tokenize
Greg Warda4efe652000-05-25 01:20:15 +000013
Greg Ward7cf7e7e2001-07-25 20:20:11 +000014# check if Python is called on the first line with this expression
Victor Stinner19474772011-05-11 00:14:28 +020015first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$')
Greg Warda4efe652000-05-25 01:20:15 +000016
Collin Winter5b7e9d72007-08-30 03:52:21 +000017class build_scripts(Command):
Greg Warda4efe652000-05-25 01:20:15 +000018
Greg Ward80fa55e2000-05-25 02:03:56 +000019 description = "\"build\" scripts (copy and fixup #! line)"
Greg Warda4efe652000-05-25 01:20:15 +000020
21 user_options = [
22 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
23 ('force', 'f', "forcibly build everything (ignore file timestamps"),
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000024 ('executable=', 'e', "specify final destination interpreter path"),
Greg Warda4efe652000-05-25 01:20:15 +000025 ]
26
Greg Ward99b032e2000-09-25 01:41:15 +000027 boolean_options = ['force']
28
Greg Warda4efe652000-05-25 01:20:15 +000029
Collin Winter5b7e9d72007-08-30 03:52:21 +000030 def initialize_options(self):
Greg Warda4efe652000-05-25 01:20:15 +000031 self.build_dir = None
32 self.scripts = None
33 self.force = None
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000034 self.executable = None
Greg Warda4efe652000-05-25 01:20:15 +000035 self.outfiles = None
36
Collin Winter5b7e9d72007-08-30 03:52:21 +000037 def finalize_options(self):
Greg Wardcb1f4c42000-09-30 18:27:54 +000038 self.set_undefined_options('build',
39 ('build_scripts', 'build_dir'),
Martin v. Löwis9f5c0c42004-08-25 11:37:43 +000040 ('force', 'force'),
41 ('executable', 'executable'))
Greg Warda4efe652000-05-25 01:20:15 +000042 self.scripts = self.distribution.scripts
43
Fred Drake4b498232004-03-25 22:04:52 +000044 def get_source_files(self):
45 return self.scripts
Greg Warda4efe652000-05-25 01:20:15 +000046
Collin Winter5b7e9d72007-08-30 03:52:21 +000047 def run(self):
Greg Warda4efe652000-05-25 01:20:15 +000048 if not self.scripts:
49 return
Greg Ward80fa55e2000-05-25 02:03:56 +000050 self.copy_scripts()
51
Fred Drake53a79062001-03-02 07:28:03 +000052
Collin Winter5b7e9d72007-08-30 03:52:21 +000053 def copy_scripts(self):
Greg Ward80fa55e2000-05-25 02:03:56 +000054 """Copy each script listed in 'self.scripts'; if it's marked as a
55 Python script in the Unix way (first line matches 'first_line_re',
56 ie. starts with "\#!" and contains "python"), then adjust the first
Greg Ward612eb9f2000-07-27 02:13:20 +000057 line to refer to the current Python interpreter as we copy.
Greg Ward80fa55e2000-05-25 02:03:56 +000058 """
Greg Warda4efe652000-05-25 01:20:15 +000059 self.mkpath(self.build_dir)
Guido van Rossumbcb0e202003-01-24 14:56:52 +000060 outfiles = []
Martin v. Löwis6178db62008-12-01 04:38:52 +000061 updated_files = []
Greg Ward80fa55e2000-05-25 02:03:56 +000062 for script in self.scripts:
Collin Winter5b7e9d72007-08-30 03:52:21 +000063 adjust = False
Just van Rossum8d8e7a32001-07-29 21:39:18 +000064 script = convert_path(script)
Greg Wardfe55e862000-05-25 20:05:52 +000065 outfile = os.path.join(self.build_dir, os.path.basename(script))
Guido van Rossumbcb0e202003-01-24 14:56:52 +000066 outfiles.append(outfile)
Greg Ward80fa55e2000-05-25 02:03:56 +000067
68 if not self.force and not newer(script, outfile):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000069 log.debug("not copying %s (up-to-date)", script)
Greg Ward80fa55e2000-05-25 02:03:56 +000070 continue
71
72 # Always open the file, but ignore failures in dry-run mode --
73 # that way, we'll get accurate feedback if we can read the
74 # script.
75 try:
Victor Stinner19474772011-05-11 00:14:28 +020076 f = open(script, "rb")
Andrew Svetlovf7a17b42012-12-25 16:47:37 +020077 except OSError:
Greg Ward80fa55e2000-05-25 02:03:56 +000078 if not self.dry_run:
79 raise
80 f = None
81 else:
Victor Stinner19474772011-05-11 00:14:28 +020082 encoding, lines = tokenize.detect_encoding(f.readline)
83 f.seek(0)
Greg Ward80fa55e2000-05-25 02:03:56 +000084 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:
Collin Winter5b7e9d72007-08-30 03:52:21 +000091 adjust = True
Victor Stinner19474772011-05-11 00:14:28 +020092 post_interp = match.group(1) or b''
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)
Martin v. Löwis6178db62008-12-01 04:38:52 +000097 updated_files.append(outfile)
Greg Ward80fa55e2000-05-25 02:03:56 +000098 if not self.dry_run:
Tarek Ziadé36797272010-07-22 12:50:05 +000099 if not sysconfig.python_build:
Victor Stinner19474772011-05-11 00:14:28 +0200100 executable = self.executable
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +0000101 else:
Victor Stinner19474772011-05-11 00:14:28 +0200102 executable = os.path.join(
Tarek Ziadé36797272010-07-22 12:50:05 +0000103 sysconfig.get_config_var("BINDIR"),
104 "python%s%s" % (sysconfig.get_config_var("VERSION"),
Victor Stinner19474772011-05-11 00:14:28 +0200105 sysconfig.get_config_var("EXE")))
106 executable = os.fsencode(executable)
107 shebang = b"#!" + executable + post_interp + b"\n"
108 # Python parser starts to read a script using UTF-8 until
109 # it gets a #coding:xxx cookie. The shebang has to be the
110 # first line of a file, the #coding:xxx cookie cannot be
111 # written before. So the shebang has to be decodable from
112 # UTF-8.
113 try:
114 shebang.decode('utf-8')
115 except UnicodeDecodeError:
116 raise ValueError(
117 "The shebang ({!r}) is not decodable "
118 "from utf-8".format(shebang))
119 # If the script is encoded to a custom encoding (use a
120 # #coding:xxx cookie), the shebang has to be decodable from
121 # the script encoding too.
122 try:
123 shebang.decode(encoding)
124 except UnicodeDecodeError:
125 raise ValueError(
126 "The shebang ({!r}) is not decodable "
127 "from the script encoding ({})"
128 .format(shebang, encoding))
Victor Stinnercfd365b2011-05-19 15:18:36 +0200129 with open(outfile, "wb") as outf:
130 outf.write(shebang)
131 outf.writelines(f.readlines())
Greg Ward80fa55e2000-05-25 02:03:56 +0000132 if f:
133 f.close()
134 else:
Christian Heimes072c0f12008-01-03 23:01:04 +0000135 if f:
136 f.close()
Martin v. Löwis6178db62008-12-01 04:38:52 +0000137 updated_files.append(outfile)
Greg Ward80fa55e2000-05-25 02:03:56 +0000138 self.copy_file(script, outfile)
139
Guido van Rossumbcb0e202003-01-24 14:56:52 +0000140 if os.name == 'posix':
141 for file in outfiles:
142 if self.dry_run:
143 log.info("changing mode of %s", file)
144 else:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000145 oldmode = os.stat(file)[ST_MODE] & 0o7777
146 newmode = (oldmode | 0o555) & 0o7777
Guido van Rossumd3590f92003-01-29 16:58:31 +0000147 if newmode != oldmode:
148 log.info("changing mode of %s from %o to %o",
149 file, oldmode, newmode)
150 os.chmod(file, newmode)
Martin v. Löwis6178db62008-12-01 04:38:52 +0000151 # XXX should we modify self.outfiles?
152 return outfiles, updated_files
153
154class build_scripts_2to3(build_scripts, Mixin2to3):
155
156 def copy_scripts(self):
157 outfiles, updated_files = build_scripts.copy_scripts(self)
158 if not self.dry_run:
159 self.run_2to3(updated_files)
160 return outfiles, updated_files