annotate Lib/distutils/command/build_scripts.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
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
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
5 __revision__ = "$Id$"
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
6
45468
1d84bf5f9478 Patch #2167 from calvin: Remove unused imports
Christian Heimes <christian@cheimes.de>
parents: 44430
diff changeset
7 import 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
8 from stat import ST_MODE
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
9 from distutils.core import Command
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
10 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
11 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
12 from distutils import log
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
13
18831
132c4bfdba74 Undo revision 1.7: always mangle a #! line containing "python" to point
Greg Ward <gward@python.net>
parents: 16956
diff changeset
14 # check if Python is called on the first line with this expression
29621
ded9abcb9174 Do not add extra "\n" after bang line.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 27520
diff changeset
15 first_line_re = re.compile('^#!.*python[0-9.]*([ \t].*)?$')
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
16
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
17 class build_scripts (Command):
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
18
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
19 description = "\"build\" scripts (copy and fixup #! line)"
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
20
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
21 user_options = [
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
22 ('build-dir=', 'd', "directory to \"build\" (copy) to"),
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
23 ('force', 'f', "forcibly build everything (ignore file timestamps"),
33266
4e9ecbf47b6b Patch #736857, #736859: Add -e option to build_scripts.
Martin v. Löwis <martin@v.loewis.de>
parents: 32736
diff changeset
24 ('executable=', 'e', "specify final destination interpreter path"),
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
25 ]
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
26
14829
b1f220f636ed Added 'boolean_options' list to support config file parsing.
Greg Ward <gward@python.net>
parents: 13725
diff changeset
27 boolean_options = ['force']
b1f220f636ed Added 'boolean_options' list to support config file parsing.
Greg Ward <gward@python.net>
parents: 13725
diff changeset
28
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
29
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
30 def initialize_options (self):
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
31 self.build_dir = None
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
32 self.scripts = None
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
33 self.force = None
33266
4e9ecbf47b6b Patch #736857, #736859: Add -e option to build_scripts.
Martin v. Löwis <martin@v.loewis.de>
parents: 32736
diff changeset
34 self.executable = None
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
35 self.outfiles = None
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
36
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
37 def finalize_options (self):
14948
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14829
diff changeset
38 self.set_undefined_options('build',
7acb8bbc215b Standardized whitespace around function calls.
Greg Ward <gward@python.net>
parents: 14829
diff changeset
39 ('build_scripts', 'build_dir'),
33266
4e9ecbf47b6b Patch #736857, #736859: Add -e option to build_scripts.
Martin v. Löwis <martin@v.loewis.de>
parents: 32736
diff changeset
40 ('force', 'force'),
4e9ecbf47b6b Patch #736857, #736859: Add -e option to build_scripts.
Martin v. Löwis <martin@v.loewis.de>
parents: 32736
diff changeset
41 ('executable', 'executable'))
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
31852
2cfe14275a04 backport fix for SF bug #796042: sdist ignores scripts argument in setup
Fred Drake <fdrake@acm.org>
parents: 29621
diff changeset
44 def get_source_files(self):
2cfe14275a04 backport fix for SF bug #796042: sdist ignores scripts argument in setup
Fred Drake <fdrake@acm.org>
parents: 29621
diff changeset
45 return self.scripts
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
46
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
47 def run (self):
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
48 if not self.scripts:
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
49 return
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
50 self.copy_scripts()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
51
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
52
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
53 def copy_scripts (self):
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
54 """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
55 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
56 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
57 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
58 """
58450
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51616
diff changeset
59 _sysconfig = __import__('sysconfig')
12745
dad205d13bb5 Bastian Kleineidam: the "build_scripts" command.
Greg Ward <gward@python.net>
parents:
diff changeset
60 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
61 outfiles = []
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
62 for script in self.scripts:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
63 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
64 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
65 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
66 outfiles.append(outfile)
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
67
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
68 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
69 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
70 continue
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
71
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
72 # 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
73 # 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
74 # script.
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
75 try:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
76 f = open(script, "r")
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
77 except IOError:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
78 if not self.dry_run:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
79 raise
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
80 f = None
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
81 else:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
82 first_line = f.readline()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
83 if not first_line:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
84 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
85 continue
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
86
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
87 match = first_line_re.match(first_line)
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
88 if match:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
89 adjust = 1
21355
003dc573b295 Joe VanAndel wrote:
Marc-André Lemburg <mal@egenix.com>
parents: 21330
diff changeset
90 post_interp = match.group(1) or ''
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
91
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
92 if adjust:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 22082
diff changeset
93 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
94 self.build_dir)
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
95 if not self.dry_run:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
96 outf = open(outfile, "w")
58450
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51616
diff changeset
97 if not _sysconfig.is_python_build():
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 31852
diff changeset
98 outf.write("#!%s%s\n" %
33266
4e9ecbf47b6b Patch #736857, #736859: Add -e option to build_scripts.
Martin v. Löwis <martin@v.loewis.de>
parents: 32736
diff changeset
99 (self.executable,
21330
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
100 post_interp))
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
101 else:
29621
ded9abcb9174 Do not add extra "\n" after bang line.
Gustavo Niemeyer <gustavo@niemeyer.net>
parents: 27520
diff changeset
102 outf.write("#!%s%s\n" %
21330
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
103 (os.path.join(
58450
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51616
diff changeset
104 _sysconfig.get_config_var("BINDIR"),
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51616
diff changeset
105 "python%s%s" % (_sysconfig.get_config_var("VERSION"),
fa69e891edf4 taking sysconfig out of distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51616
diff changeset
106 _sysconfig.get_config_var("EXE"))),
21330
4420a4bd96a4 Fix for
Michael W. Hudson <mwh@python.net>
parents: 21271
diff changeset
107 post_interp))
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
108 outf.writelines(f.readlines())
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
109 outf.close()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
110 if f:
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
111 f.close()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
112 else:
44429
e9f480b4c133 Patch #1696. Don't attempt to call None.close() in dry-run mode.
Guido van Rossum <guido@python.org>
parents: 33965
diff changeset
113 if f:
e9f480b4c133 Patch #1696. Don't attempt to call None.close() in dry-run mode.
Guido van Rossum <guido@python.org>
parents: 33965
diff changeset
114 f.close()
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
115 self.copy_file(script, outfile)
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
116
27355
dc6116278259 Change the mode of scripts in the build/scripts* directory to
Guido van Rossum <guido@python.org>
parents: 26285
diff changeset
117 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
118 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
119 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
120 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
121 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
122 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
123 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
124 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
125 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
126 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
127 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
128
12748
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
129 # copy_scripts ()
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
130
eb562df0cebe Improvements to Bastian's build_scripts command:
Greg Ward <gward@python.net>
parents: 12745
diff changeset
131 # class build_scripts