blob: 495f4c372ab8c19d634f0d030b2cf89467486dfb [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
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
Greg Warda4efe652000-05-25 01:20:15 +000012
13# check if Python is called on the first line with this expression
Greg Ward80fa55e2000-05-25 02:03:56 +000014first_line_re = re.compile(r'^#!.*python(\s+.*)?')
Greg Warda4efe652000-05-25 01:20:15 +000015
16class build_scripts (Command):
17
Greg Ward80fa55e2000-05-25 02:03:56 +000018 description = "\"build\" scripts (copy and fixup #! line)"
Greg Warda4efe652000-05-25 01:20:15 +000019
20 user_options = [
21 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
22 ('force', 'f', "forcibly build everything (ignore file timestamps"),
23 ]
24
Greg Ward99b032e2000-09-25 01:41:15 +000025 boolean_options = ['force']
26
Greg Warda4efe652000-05-25 01:20:15 +000027
28 def initialize_options (self):
29 self.build_dir = None
30 self.scripts = None
31 self.force = None
32 self.outfiles = None
33
34 def finalize_options (self):
Greg Wardcb1f4c42000-09-30 18:27:54 +000035 self.set_undefined_options('build',
36 ('build_scripts', 'build_dir'),
37 ('force', 'force'))
Greg Warda4efe652000-05-25 01:20:15 +000038 self.scripts = self.distribution.scripts
39
40
41 def run (self):
42 if not self.scripts:
43 return
Greg Ward80fa55e2000-05-25 02:03:56 +000044 self.copy_scripts()
45
Greg Warda4efe652000-05-25 01:20:15 +000046
Greg Ward80fa55e2000-05-25 02:03:56 +000047 def copy_scripts (self):
48 """Copy each script listed in 'self.scripts'; if it's marked as a
49 Python script in the Unix way (first line matches 'first_line_re',
50 ie. starts with "\#!" and contains "python"), then adjust the first
Greg Ward612eb9f2000-07-27 02:13:20 +000051 line to refer to the current Python interpreter as we copy.
Greg Ward80fa55e2000-05-25 02:03:56 +000052 """
53 outfiles = []
Greg Warda4efe652000-05-25 01:20:15 +000054 self.mkpath(self.build_dir)
Greg Ward80fa55e2000-05-25 02:03:56 +000055 for script in self.scripts:
56 adjust = 0
Greg Wardfe55e862000-05-25 20:05:52 +000057 outfile = os.path.join(self.build_dir, os.path.basename(script))
Greg Ward80fa55e2000-05-25 02:03:56 +000058
59 if not self.force and not newer(script, outfile):
60 self.announce("not copying %s (output up-to-date)" % script)
61 continue
62
63 # Always open the file, but ignore failures in dry-run mode --
64 # that way, we'll get accurate feedback if we can read the
65 # script.
66 try:
67 f = open(script, "r")
68 except IOError:
69 if not self.dry_run:
70 raise
71 f = None
72 else:
73 first_line = f.readline()
74 if not first_line:
75 self.warn("%s is an empty file (skipping)" % script)
Greg Warda4efe652000-05-25 01:20:15 +000076 continue
Greg Ward80fa55e2000-05-25 02:03:56 +000077
78 match = first_line_re.match(first_line)
79 if match:
80 adjust = 1
81 post_interp = match.group(1)
82
83 if adjust:
84 self.announce("copying and adjusting %s -> %s" %
85 (script, self.build_dir))
86 if not self.dry_run:
87 outf = open(outfile, "w")
88 outf.write("#!%s%s\n" %
89 (os.path.normpath(sys.executable), post_interp))
90 outf.writelines(f.readlines())
91 outf.close()
92 if f:
93 f.close()
94 else:
95 f.close()
96 self.copy_file(script, outfile)
97
98 # copy_scripts ()
99
100# class build_scripts