blob: e0fcc23e13f52efa506177d9af7262833b171807 [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"),
27 ]
28
Greg Ward99b032e2000-09-25 01:41:15 +000029 boolean_options = ['force']
30
Greg Warda4efe652000-05-25 01:20:15 +000031
32 def initialize_options (self):
33 self.build_dir = None
34 self.scripts = None
35 self.force = None
36 self.outfiles = None
37
38 def finalize_options (self):
Greg Wardcb1f4c42000-09-30 18:27:54 +000039 self.set_undefined_options('build',
40 ('build_scripts', 'build_dir'),
41 ('force', 'force'))
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
47 def run (self):
48 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
Greg Ward80fa55e2000-05-25 02:03:56 +000053 def copy_scripts (self):
54 """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 = []
Greg Ward80fa55e2000-05-25 02:03:56 +000061 for script in self.scripts:
62 adjust = 0
Just van Rossum8d8e7a32001-07-29 21:39:18 +000063 script = convert_path(script)
Greg Wardfe55e862000-05-25 20:05:52 +000064 outfile = os.path.join(self.build_dir, os.path.basename(script))
Guido van Rossumbcb0e202003-01-24 14:56:52 +000065 outfiles.append(outfile)
Greg Ward80fa55e2000-05-25 02:03:56 +000066
67 if not self.force and not newer(script, outfile):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000068 log.debug("not copying %s (up-to-date)", script)
Greg Ward80fa55e2000-05-25 02:03:56 +000069 continue
70
71 # Always open the file, but ignore failures in dry-run mode --
72 # that way, we'll get accurate feedback if we can read the
73 # script.
74 try:
75 f = open(script, "r")
76 except IOError:
77 if not self.dry_run:
78 raise
79 f = None
80 else:
81 first_line = f.readline()
82 if not first_line:
83 self.warn("%s is an empty file (skipping)" % script)
Greg Warda4efe652000-05-25 01:20:15 +000084 continue
Greg Ward80fa55e2000-05-25 02:03:56 +000085
86 match = first_line_re.match(first_line)
87 if match:
88 adjust = 1
Marc-André Lemburg90294d02001-12-11 20:44:42 +000089 post_interp = match.group(1) or ''
Greg Ward80fa55e2000-05-25 02:03:56 +000090
91 if adjust:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000092 log.info("copying and adjusting %s -> %s", script,
93 self.build_dir)
Greg Ward80fa55e2000-05-25 02:03:56 +000094 if not self.dry_run:
95 outf = open(outfile, "w")
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +000096 if not sysconfig.python_build:
Tim Peters182b5ac2004-07-18 06:16:08 +000097 outf.write("#!%s%s\n" %
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +000098 (os.path.normpath(sys.executable),
99 post_interp))
100 else:
Gustavo Niemeyer6cf26192003-06-27 19:33:38 +0000101 outf.write("#!%s%s\n" %
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +0000102 (os.path.join(
103 sysconfig.get_config_var("BINDIR"),
104 "python" + sysconfig.get_config_var("EXE")),
105 post_interp))
Greg Ward80fa55e2000-05-25 02:03:56 +0000106 outf.writelines(f.readlines())
107 outf.close()
108 if f:
109 f.close()
110 else:
111 f.close()
112 self.copy_file(script, outfile)
113
Guido van Rossumbcb0e202003-01-24 14:56:52 +0000114 if os.name == 'posix':
115 for file in outfiles:
116 if self.dry_run:
117 log.info("changing mode of %s", file)
118 else:
Guido van Rossumd3590f92003-01-29 16:58:31 +0000119 oldmode = os.stat(file)[ST_MODE] & 07777
120 newmode = (oldmode | 0555) & 07777
121 if newmode != oldmode:
122 log.info("changing mode of %s from %o to %o",
123 file, oldmode, newmode)
124 os.chmod(file, newmode)
Guido van Rossumbcb0e202003-01-24 14:56:52 +0000125
Greg Ward80fa55e2000-05-25 02:03:56 +0000126 # copy_scripts ()
127
128# class build_scripts