annotate Lib/distutils/command/build_scripts.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents 6786e86b8ff0
children ded9abcb9174
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
1 """distutils.command.build_scripts
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
2
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
3 Implements the Distutils 'build_scripts' command."""
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
4
26285
42bfc98d295b Add comment to Distutil files about requiring 1.5.2 compatibility, as
Andrew M. Kuchling <amk@amk.ca>
parents: 26210
diff changeset
5 # This module should be kept compatible with Python 1.5.2.
42bfc98d295b Add comment to Distutil files about requiring 1.5.2 compatibility, as
Andrew M. Kuchling <amk@amk.ca>
parents: 26210
diff changeset
6
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
7 __revision__ = "$Id$"
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
8
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
9 import sys, os, re
27355
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
10 from stat import ST_MODE
21330
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
11 from distutils import sysconfig
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
12 from distutils.core import Command
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
13 from distutils.dep_util import newer
18871
1b09a9cb06d5 Do convert_path() on script paths (now PyXML builds out of the box
Just van Rossum <just@letterror.com>
parents: 18831
diff changeset
14 from distutils.util import convert_path
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22082
diff changeset
15 from distutils import log
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
16
18831
132c4bfdba74 Undo revision 1.7: always mangle a #! line containing "python" to point
Greg Ward <gward@python.net>
parents: 16956
diff changeset
17 # check if Python is called on the first line with this expression
22082
55c4d083c309 Allow shebang's which use versioned Python binaries. Fixes
Marc-André Lemburg <mal@egenix.com>
parents: 21355
diff changeset
18 first_line_re = re.compile(r'^#!.*python[0-9.]*(\s+.*)?$')
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
19
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
20 class build_scripts (Command):
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
21
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
22 description = "\"build\" scripts (copy and fixup #! line)"
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
23
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
24 user_options = [
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
25 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
26 ('force', 'f', "forcibly build everything (ignore file timestamps"),
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
27 ]
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
28
14829
b1f220f636ed Added 'boolean_options' list to support config file parsing.
Greg Ward <gward@python.net>
parents: 13725
diff changeset
29 boolean_options = ['force']
b1f220f636ed Added 'boolean_options' list to support config file parsing.
Greg Ward <gward@python.net>
parents: 13725
diff changeset
30
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
31
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
32 def initialize_options (self):
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
33 self.build_dir = None
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
34 self.scripts = None
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
35 self.force = None
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
36 self.outfiles = None
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
37
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
38 def finalize_options (self):
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14829
diff changeset
39 self.set_undefined_options('build',
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14829
diff changeset
40 ('build_scripts', 'build_dir'),
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14829
diff changeset
41 ('force', 'force'))
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
42 self.scripts = self.distribution.scripts
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
43
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
44
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
45 def run (self):
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
46 if not self.scripts:
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
47 return
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
48 self.copy_scripts()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
49
16956
a1b3446c720f When not copying a file because the output is up to date, make the message
Fred Drake <fdrake@acm.org>
parents: 16846
diff changeset
50
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
51 def copy_scripts (self):
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
52 """Copy each script listed in 'self.scripts'; if it's marked as a
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
53 Python script in the Unix way (first line matches 'first_line_re',
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
54 ie. starts with "\#!" and contains "python"), then adjust the first
13725
449dd3a00bd7 Fixed a grab-bag of typos spotted by Rob Hooft.
Greg Ward <gward@python.net>
parents: 12752
diff changeset
55 line to refer to the current Python interpreter as we copy.
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
56 """
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
57 self.mkpath(self.build_dir)
27355
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
58 outfiles = []
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
59 for script in self.scripts:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
60 adjust = 0
18871
1b09a9cb06d5 Do convert_path() on script paths (now PyXML builds out of the box
Just van Rossum <just@letterror.com>
parents: 18831
diff changeset
61 script = convert_path(script)
12752
7b49334a6369 Take the basename of the script before concatenating it with the build dir.
Greg Ward <gward@python.net>
parents: 12748
diff changeset
62 outfile = os.path.join(self.build_dir, os.path.basename(script))
27355
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
63 outfiles.append(outfile)
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
64
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
65 if not self.force and not newer(script, outfile):
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22082
diff changeset
66 log.debug("not copying %s (up-to-date)", script)
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
67 continue
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
68
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
69 # Always open the file, but ignore failures in dry-run mode --
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
70 # that way, we'll get accurate feedback if we can read the
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
71 # script.
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
72 try:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
73 f = open(script, "r")
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
74 except IOError:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
75 if not self.dry_run:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
76 raise
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
77 f = None
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
78 else:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
79 first_line = f.readline()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
80 if not first_line:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
81 self.warn("%s is an empty file (skipping)" % script)
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
82 continue
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
83
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
84 match = first_line_re.match(first_line)
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
85 if match:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
86 adjust = 1
21355
003dc573b295 Joe VanAndel wrote:
Marc-André Lemburg <mal@egenix.com>
parents: 21330
diff changeset
87 post_interp = match.group(1) or ''
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
88
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
89 if adjust:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22082
diff changeset
90 log.info("copying and adjusting %s -> %s", script,
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22082
diff changeset
91 self.build_dir)
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
92 if not self.dry_run:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
93 outf = open(outfile, "w")
21330
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
94 if not sysconfig.python_build:
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
95 outf.write("#!%s%s\n" %
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
96 (os.path.normpath(sys.executable),
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
97 post_interp))
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
98 else:
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
99 outf.write("#!%s%s" %
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
100 (os.path.join(
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
101 sysconfig.get_config_var("BINDIR"),
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
102 "python" + sysconfig.get_config_var("EXE")),
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
103 post_interp))
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
104 outf.writelines(f.readlines())
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
105 outf.close()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
106 if f:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
107 f.close()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
108 else:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
109 f.close()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
110 self.copy_file(script, outfile)
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
111
27355
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
112 if os.name == 'posix':
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
113 for file in outfiles:
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
114 if self.dry_run:
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
115 log.info("changing mode of %s", file)
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
116 else:
27520
6786e86b8ff0 Only log a message and chmod() when the mode isn't already what we
Guido van Rossum <guido@python.org>
parents: 27355
diff changeset
117 oldmode = os.stat(file)[ST_MODE] & 07777
6786e86b8ff0 Only log a message and chmod() when the mode isn't already what we
Guido van Rossum <guido@python.org>
parents: 27355
diff changeset
118 newmode = (oldmode | 0555) & 07777
6786e86b8ff0 Only log a message and chmod() when the mode isn't already what we
Guido van Rossum <guido@python.org>
parents: 27355
diff changeset
119 if newmode != oldmode:
6786e86b8ff0 Only log a message and chmod() when the mode isn't already what we
Guido van Rossum <guido@python.org>
parents: 27355
diff changeset
120 log.info("changing mode of %s from %o to %o",
6786e86b8ff0 Only log a message and chmod() when the mode isn't already what we
Guido van Rossum <guido@python.org>
parents: 27355
diff changeset
121 file, oldmode, newmode)
6786e86b8ff0 Only log a message and chmod() when the mode isn't already what we
Guido van Rossum <guido@python.org>
parents: 27355
diff changeset
122 os.chmod(file, newmode)
27355
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
123
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
124 # copy_scripts ()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
125
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
126 # class build_scripts