blob: 211ade40fad16612c49edd6942466cd95b19bd98 [file] [log] [blame]
Greg Warda4efe652000-05-25 01:20:15 +00001"""distutils.command.build_scripts
2
3Implements the Distutils 'build_scripts' command."""
4
5# created 2000/05/23, Bastian Kleineidam
6
7__revision__ = "$Id$"
8
Greg Ward80fa55e2000-05-25 02:03:56 +00009import sys, os, re
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +000010from distutils import sysconfig
Greg Warda4efe652000-05-25 01:20:15 +000011from distutils.core import Command
Greg Ward80fa55e2000-05-25 02:03:56 +000012from distutils.dep_util import newer
Just van Rossum8d8e7a32001-07-29 21:39:18 +000013from distutils.util import convert_path
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000014from distutils import log
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
Marc-André Lemburgbbea4712002-02-28 09:16:21 +000017first_line_re = re.compile(r'^#!.*python[0-9.]*(\s+.*)?$')
Greg Warda4efe652000-05-25 01:20:15 +000018
19class build_scripts (Command):
20
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"),
26 ]
27
Greg Ward99b032e2000-09-25 01:41:15 +000028 boolean_options = ['force']
29
Greg Warda4efe652000-05-25 01:20:15 +000030
31 def initialize_options (self):
32 self.build_dir = None
33 self.scripts = None
34 self.force = None
35 self.outfiles = None
36
37 def finalize_options (self):
Greg Wardcb1f4c42000-09-30 18:27:54 +000038 self.set_undefined_options('build',
39 ('build_scripts', 'build_dir'),
40 ('force', 'force'))
Greg Warda4efe652000-05-25 01:20:15 +000041 self.scripts = self.distribution.scripts
42
43
44 def run (self):
45 if not self.scripts:
46 return
Greg Ward80fa55e2000-05-25 02:03:56 +000047 self.copy_scripts()
48
Fred Drake53a79062001-03-02 07:28:03 +000049
Greg Ward80fa55e2000-05-25 02:03:56 +000050 def copy_scripts (self):
51 """Copy each script listed in 'self.scripts'; if it's marked as a
52 Python script in the Unix way (first line matches 'first_line_re',
53 ie. starts with "\#!" and contains "python"), then adjust the first
Greg Ward612eb9f2000-07-27 02:13:20 +000054 line to refer to the current Python interpreter as we copy.
Greg Ward80fa55e2000-05-25 02:03:56 +000055 """
Greg Warda4efe652000-05-25 01:20:15 +000056 self.mkpath(self.build_dir)
Greg Ward80fa55e2000-05-25 02:03:56 +000057 for script in self.scripts:
58 adjust = 0
Just van Rossum8d8e7a32001-07-29 21:39:18 +000059 script = convert_path(script)
Greg Wardfe55e862000-05-25 20:05:52 +000060 outfile = os.path.join(self.build_dir, os.path.basename(script))
Greg Ward80fa55e2000-05-25 02:03:56 +000061
62 if not self.force and not newer(script, outfile):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000063 log.debug("not copying %s (up-to-date)", script)
Greg Ward80fa55e2000-05-25 02:03:56 +000064 continue
65
66 # Always open the file, but ignore failures in dry-run mode --
67 # that way, we'll get accurate feedback if we can read the
68 # script.
69 try:
70 f = open(script, "r")
71 except IOError:
72 if not self.dry_run:
73 raise
74 f = None
75 else:
76 first_line = f.readline()
77 if not first_line:
78 self.warn("%s is an empty file (skipping)" % script)
Greg Warda4efe652000-05-25 01:20:15 +000079 continue
Greg Ward80fa55e2000-05-25 02:03:56 +000080
81 match = first_line_re.match(first_line)
82 if match:
83 adjust = 1
Marc-André Lemburg90294d02001-12-11 20:44:42 +000084 post_interp = match.group(1) or ''
Greg Ward80fa55e2000-05-25 02:03:56 +000085
86 if adjust:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +000087 log.info("copying and adjusting %s -> %s", script,
88 self.build_dir)
Greg Ward80fa55e2000-05-25 02:03:56 +000089 if not self.dry_run:
90 outf = open(outfile, "w")
Michael W. Hudsonfb173cd2001-12-10 16:15:44 +000091 if not sysconfig.python_build:
92 outf.write("#!%s%s\n" %
93 (os.path.normpath(sys.executable),
94 post_interp))
95 else:
96 outf.write("#!%s%s" %
97 (os.path.join(
98 sysconfig.get_config_var("BINDIR"),
99 "python" + sysconfig.get_config_var("EXE")),
100 post_interp))
Greg Ward80fa55e2000-05-25 02:03:56 +0000101 outf.writelines(f.readlines())
102 outf.close()
103 if f:
104 f.close()
105 else:
106 f.close()
107 self.copy_file(script, outfile)
108
109 # copy_scripts ()
110
111# class build_scripts