annotate Lib/distutils/util.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 2ec4ab2a6f65
children 1d061f83a6bf
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
1 """distutils.util
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
2
12226
ffd242fed0e2 Reorganization: ripped util.py to shreds, creating in the process:
Greg Ward <gward@python.net>
parents: 12170
diff changeset
3 Miscellaneous utility functions -- anything that doesn't fit into
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
4 one of the other *util.py modules.
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
5 """
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
6
11897
017e592c0fd2 Changed '__rcsid__' to '__revision__'.
Greg Ward <gward@python.net>
parents: 11887
diff changeset
7 __revision__ = "$Id$"
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
8
74482
2ec4ab2a6f65 Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74479
diff changeset
9 import sys, os, string, re
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
10 from distutils.errors import DistutilsPlatformError
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
11 from distutils.dep_util import newer
12102
e81b139ab8a4 Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents: 12044
diff changeset
12 from distutils.spawn import spawn
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
13 from distutils import log
58205
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57909
diff changeset
14 from distutils.errors import DistutilsByteCompileError
12453
d0f290b3c0b2 Merged in Python 1.5.1 compatibility changes from the 0.1.3 branch:
Greg Ward <gward@python.net>
parents: 12407
diff changeset
15
11887
81f1069e1558 Added 'get_platform()' to construct a string that identifies the current
Greg Ward <gward@python.net>
parents: 11684
diff changeset
16 def get_platform ():
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
17 """Return a string that identifies the current platform. This is used
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
18 mainly to distinguish platform-specific build directories and
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
19 platform-specific built distributions. Typically includes the OS name
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
20 and version and the architecture (as supplied by 'os.uname()'),
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
21 although the exact information included depends on the OS; eg. for IRIX
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
22 the architecture isn't particularly important (IRIX only runs on SGI
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
23 hardware), but for Linux the kernel version isn't particularly
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
24 important.
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
25
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
26 Examples of returned values:
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
27 linux-i586
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
28 linux-alpha (?)
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
29 solaris-2.6-sun4u
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
30 irix-5.3
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
31 irix64-6.2
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
32
42683
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
33 Windows will return one of:
46331
a93d2919ea59 Issue #2513: enable 64bit cross compilation on windows.
Mark Hammond <mhammond@skippinet.com.au>
parents: 45413
diff changeset
34 win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
42683
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
35 win-ia64 (64bit Windows on Itanium)
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
36 win32 (all others - specifically, sys.platform is returned)
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
37
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
38 For other non-POSIX platforms, currently just returns 'sys.platform'.
12951
7f022e260a4d 'get_platform()' now just returns 'sys.platform' on all platforms.
Greg Ward <gward@python.net>
parents: 12935
diff changeset
39 """
42683
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
40 if os.name == 'nt':
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
41 # sniff sys.version for architecture.
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
42 prefix = " bit ("
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
43 i = string.find(sys.version, prefix)
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
44 if i == -1:
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
45 return sys.platform
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
46 j = string.find(sys.version, ")", i)
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
47 look = sys.version[i+len(prefix):j].lower()
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
48 if look=='amd64':
46331
a93d2919ea59 Issue #2513: enable 64bit cross compilation on windows.
Mark Hammond <mhammond@skippinet.com.au>
parents: 45413
diff changeset
49 return 'win-amd64'
42683
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
50 if look=='itanium':
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
51 return 'win-ia64'
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
52 return sys.platform
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
53
14941
f9cf611096bf In 'get_platform()', handle so-called POSIX systems that don't have
Greg Ward <gward@python.net>
parents: 14860
diff changeset
54 if os.name != "posix" or not hasattr(os, 'uname'):
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
55 # XXX what about the architecture? NT is Intel or Alpha,
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
56 # Mac OS is M68k or PPC, etc.
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
57 return sys.platform
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
58
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
59 # Try to distinguish various flavours of Unix
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
60
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
61 (osname, host, release, version, machine) = os.uname()
16801
2c9186e40b30 Bug #229280: remove '/' characters from the OS name (for BSD/OS :) )
Andrew M. Kuchling <amk@amk.ca>
parents: 16796
diff changeset
62
27106
aee43a4f3570 Translate spaces in the machine name to underscores
Andrew M. Kuchling <amk@amk.ca>
parents: 26509
diff changeset
63 # Convert the OS name to lowercase, remove '/' characters
aee43a4f3570 Translate spaces in the machine name to underscores
Andrew M. Kuchling <amk@amk.ca>
parents: 26509
diff changeset
64 # (to accommodate BSD/OS), and translate spaces (for "Power Macintosh")
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
65 osname = string.lower(osname)
16801
2c9186e40b30 Bug #229280: remove '/' characters from the OS name (for BSD/OS :) )
Andrew M. Kuchling <amk@amk.ca>
parents: 16796
diff changeset
66 osname = string.replace(osname, '/', '')
27106
aee43a4f3570 Translate spaces in the machine name to underscores
Andrew M. Kuchling <amk@amk.ca>
parents: 26509
diff changeset
67 machine = string.replace(machine, ' ', '_')
38033
473f37de13a1 Bug #1478326: don't allow '/' in distutils.util.get_platform machine names
Georg Brandl <georg@python.org>
parents: 32736
diff changeset
68 machine = string.replace(machine, '/', '-')
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
69
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
70 if osname[:5] == "linux":
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
71 # At least on Linux/Intel, 'machine' is the processor --
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
72 # i386, etc.
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
73 # XXX what about Alpha, SPARC, etc?
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
74 return "%s-%s" % (osname, machine)
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
75 elif osname[:5] == "sunos":
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
76 if release[0] >= "5": # SunOS 5 == Solaris 2
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
77 osname = "solaris"
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
78 release = "%d.%s" % (int(release[0]) - 3, release[2:])
74482
2ec4ab2a6f65 Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74479
diff changeset
79 # We can't use "platform.architecture()[0]" because a
2ec4ab2a6f65 Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74479
diff changeset
80 # bootstrap problem. We use a dict to get an error
2ec4ab2a6f65 Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74479
diff changeset
81 # if some suspicious happens.
2ec4ab2a6f65 Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74479
diff changeset
82 bitness = {2147483647:"32bit", 9223372036854775807:"64bit"}
2ec4ab2a6f65 Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74479
diff changeset
83 machine += ".%s" % bitness[sys.maxint]
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
84 # fall through to standard osname-release-machine representation
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
85 elif osname[:4] == "irix": # could be "irix64"!
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
86 return "%s-%s" % (osname, release)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
87 elif osname[:3] == "aix":
16796
ac180c982295 Patch #404275: generate a reasonable platform string for AIX
Andrew M. Kuchling <amk@amk.ca>
parents: 16321
diff changeset
88 return "%s-%s.%s" % (osname, version, release)
16035
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
89 elif osname[:6] == "cygwin":
18782
98484ef225f9 Patch #429442 from Jason Tishler: Corrects sys.platform and
Andrew M. Kuchling <amk@amk.ca>
parents: 17206
diff changeset
90 osname = "cygwin"
16035
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
91 rel_re = re.compile (r'[\d.]+')
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
92 m = rel_re.match(release)
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
93 if m:
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
94 release = m.group()
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
95 elif osname[:6] == "darwin":
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
96 #
38272
de9f1a140611 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38248
diff changeset
97 # For our purposes, we'll assume that the system version from
de9f1a140611 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38248
diff changeset
98 # distutils' perspective is what MACOSX_DEPLOYMENT_TARGET is set
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
99 # to. This makes the compatibility story a bit more sane because the
38272
de9f1a140611 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38248
diff changeset
100 # machine is going to compile and link as if it were
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
101 # MACOSX_DEPLOYMENT_TARGET.
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
102 from distutils.sysconfig import get_config_vars
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
103 cfgvars = get_config_vars()
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
104
70143
9874f213edb2 Issue #9516: avoid errors in sysconfig when MACOSX_DEPLOYMENT_TARGET is set in shell.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 68256
diff changeset
105 macver = cfgvars.get('MACOSX_DEPLOYMENT_TARGET')
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
106
50341
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
107 if 1:
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
108 # Always calculate the release of the running machine,
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
109 # needed to determine if we can build fat binaries or not.
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
110
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
111 macrelease = macver
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
112 # Get the system version. Reading this plist is a documented
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
113 # way to get the system version (see the documentation for
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
114 # the Gestalt Manager)
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
115 try:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
116 f = open('/System/Library/CoreServices/SystemVersion.plist')
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
117 except IOError:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
118 # We're on a plain darwin box, fall back to the default
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
119 # behaviour.
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
120 pass
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
121 else:
66008
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
122 try:
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
123 m = re.search(
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
124 r'<key>ProductUserVisibleVersion</key>\s*' +
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
125 r'<string>(.*?)</string>', f.read())
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
126 if m is not None:
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
127 macrelease = '.'.join(m.group(1).split('.')[:2])
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
128 # else: fall back to the default behaviour
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
129 finally:
a17b21c27a33 Merged revisions 86223-86224,86226,86234 via svnmerge from
Éric Araujo <merwok@netwok.org>
parents: 62945
diff changeset
130 f.close()
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
131
50341
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
132 if not macver:
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
133 macver = macrelease
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
134
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
135 if macver:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
136 from distutils.sysconfig import get_config_vars
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
137 release = macver
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
138 osname = "macosx"
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
139
50341
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
140 if (macrelease + '.') >= '10.4.' and \
47473
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
141 '-arch' in get_config_vars().get('CFLAGS', '').strip():
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
142 # The universal build will build fat binaries, but not on
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
143 # systems before 10.4
47473
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
144 #
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
145 # Try to detect 4-way universal builds, those have machine-type
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
146 # 'universal' instead of 'fat'.
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
147
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
148 machine = 'fat'
50424
df3923c36ec5 Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents: 50341
diff changeset
149 cflags = get_config_vars().get('CFLAGS')
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
150
55973
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
151 archs = re.findall('-arch\s+(\S+)', cflags)
62951
4707ede4d14f Merged revisions 82791 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 58205
diff changeset
152 archs = tuple(sorted(set(archs)))
55973
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
153
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
154 if len(archs) == 1:
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
155 machine = archs[0]
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
156 elif archs == ('i386', 'ppc'):
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
157 machine = 'fat'
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
158 elif archs == ('i386', 'x86_64'):
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
159 machine = 'intel'
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
160 elif archs == ('i386', 'ppc', 'x86_64'):
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
161 machine = 'fat3'
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
162 elif archs == ('ppc64', 'x86_64'):
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
163 machine = 'fat64'
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
164 elif archs == ('i386', 'ppc', 'ppc64', 'x86_64'):
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
165 machine = 'universal'
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
166 else:
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
167 raise ValueError(
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
168 "Don't know machine value for archs=%r"%(archs,))
a6df074d2e09 Merged revisions 74806 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 50424
diff changeset
169
57909
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
170 elif machine == 'i386':
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
171 # On OSX the machine type returned by uname is always the
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
172 # 32-bit variant, even if the executable architecture is
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
173 # the 64-bit variant
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
174 if sys.maxint >= 2**32:
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
175 machine = 'x86_64'
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
176
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
177 elif machine in ('PowerPC', 'Power_Macintosh'):
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
178 # Pick a sane name for the PPC architecture.
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
179 machine = 'ppc'
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
180
57909
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
181 # See 'i386' case
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
182 if sys.maxint >= 2**32:
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
183 machine = 'ppc64'
1df73bf38c55 Merged revisions 77026 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57586
diff changeset
184
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
185 return "%s-%s-%s" % (osname, release, machine)
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
186
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
187 # get_platform ()
11934
7bd97c3dd432 Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents: 11916
diff changeset
188
7bd97c3dd432 Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents: 11916
diff changeset
189
12804
3be92cef1917 Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents: 12802
diff changeset
190 def convert_path (pathname):
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
191 """Return 'pathname' as a name that will work on the native filesystem,
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
192 i.e. split it on '/' and put it back together again using the current
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
193 directory separator. Needed because filenames in the setup script are
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
194 always supplied in Unix style, and have to be converted to the local
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
195 convention before we can actually use them in the filesystem. Raises
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
196 ValueError on non-Unix-ish systems if 'pathname' either starts or
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
197 ends with a slash.
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
198 """
14743
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
199 if os.sep == '/':
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
200 return pathname
24786
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
201 if not pathname:
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
202 return pathname
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
203 if pathname[0] == '/':
12407
01cff9fe2d45 Cleaned up/simplified error-handling:
Greg Ward <gward@python.net>
parents: 12226
diff changeset
204 raise ValueError, "path '%s' cannot be absolute" % pathname
24786
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
205 if pathname[-1] == '/':
12407
01cff9fe2d45 Cleaned up/simplified error-handling:
Greg Ward <gward@python.net>
parents: 12226
diff changeset
206 raise ValueError, "path '%s' cannot end with '/'" % pathname
14743
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
207
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
208 paths = string.split(pathname, '/')
16321
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
209 while '.' in paths:
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
210 paths.remove('.')
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
211 if not paths:
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
212 return os.curdir
63579
ac29f70c61db Merged revisions 79558 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 62951
diff changeset
213 return os.path.join(*paths)
11934
7bd97c3dd432 Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents: 11916
diff changeset
214
12804
3be92cef1917 Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents: 12802
diff changeset
215 # convert_path ()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
216
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
217
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
218 def change_root (new_root, pathname):
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
219 """Return 'pathname' with 'new_root' prepended. If 'pathname' is
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
220 relative, this is equivalent to "os.path.join(new_root,pathname)".
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
221 Otherwise, it requires making 'pathname' relative and then joining the
12802
33ddc12df267 Fixed 'change_root() to work at all on Windows, and to work correctly on Unix.
Greg Ward <gward@python.net>
parents: 12672
diff changeset
222 two, which is tricky on DOS/Windows and Mac OS.
33ddc12df267 Fixed 'change_root() to work at all on Windows, and to work correctly on Unix.
Greg Ward <gward@python.net>
parents: 12672
diff changeset
223 """
33ddc12df267 Fixed 'change_root() to work at all on Windows, and to work correctly on Unix.
Greg Ward <gward@python.net>
parents: 12672
diff changeset
224 if os.name == 'posix':
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
225 if not os.path.isabs(pathname):
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
226 return os.path.join(new_root, pathname)
12802
33ddc12df267 Fixed 'change_root() to work at all on Windows, and to work correctly on Unix.
Greg Ward <gward@python.net>
parents: 12672
diff changeset
227 else:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
228 return os.path.join(new_root, pathname[1:])
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
229
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
230 elif os.name == 'nt':
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
231 (drive, path) = os.path.splitdrive(pathname)
12802
33ddc12df267 Fixed 'change_root() to work at all on Windows, and to work correctly on Unix.
Greg Ward <gward@python.net>
parents: 12672
diff changeset
232 if path[0] == '\\':
33ddc12df267 Fixed 'change_root() to work at all on Windows, and to work correctly on Unix.
Greg Ward <gward@python.net>
parents: 12672
diff changeset
233 path = path[1:]
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
234 return os.path.join(new_root, path)
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
235
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
236 elif os.name == 'os2':
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
237 (drive, path) = os.path.splitdrive(pathname)
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
238 if path[0] == os.sep:
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
239 path = path[1:]
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
240 return os.path.join(new_root, path)
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
241
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
242 else:
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
243 raise DistutilsPlatformError, \
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
244 "nothing known about platform '%s'" % os.name
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
245
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
246
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
247 _environ_checked = 0
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
248 def check_environ ():
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
249 """Ensure that 'os.environ' has all the environment variables we
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
250 guarantee that users can use in config files, command-line options,
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
251 etc. Currently this includes:
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
252 HOME - user's home directory (Unix only)
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
253 PLAT - description of the current platform, including hardware
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
254 and OS (see 'get_platform()')
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
255 """
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
256 global _environ_checked
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
257 if _environ_checked:
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
258 return
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
259
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 42683
diff changeset
260 if os.name == 'posix' and 'HOME' not in os.environ:
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
261 import pwd
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
262 os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
263
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 42683
diff changeset
264 if 'PLAT' not in os.environ:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
265 os.environ['PLAT'] = get_platform()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
266
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
267 _environ_checked = 1
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
268
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
269
16261
bdfc6f1fa475 In subst_vars(), change the name of the argument from str to s to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16035
diff changeset
270 def subst_vars (s, local_vars):
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
271 """Perform shell/Perl-style variable substitution on 'string'. Every
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
272 occurrence of '$' followed by a name is considered a variable, and
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
273 variable is substituted by the value found in the 'local_vars'
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
274 dictionary, or in 'os.environ' if it's not in 'local_vars'.
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
275 'os.environ' is first checked/augmented to guarantee that it contains
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
276 certain values: see 'check_environ()'. Raise ValueError for any
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
277 variables not found in either 'local_vars' or 'os.environ'.
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
278 """
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
279 check_environ()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
280 def _subst (match, local_vars=local_vars):
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
281 var_name = match.group(1)
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 42683
diff changeset
282 if var_name in local_vars:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
283 return str(local_vars[var_name])
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
284 else:
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
285 return os.environ[var_name]
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
286
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
287 try:
16261
bdfc6f1fa475 In subst_vars(), change the name of the argument from str to s to
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 16035
diff changeset
288 return re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s)
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
289 except KeyError, var:
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
290 raise ValueError, "invalid variable '$%s'" % var
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
291
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
292 # subst_vars ()
12102
e81b139ab8a4 Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents: 12044
diff changeset
293
e81b139ab8a4 Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents: 12044
diff changeset
294
12935
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
295 def grok_environment_error (exc, prefix="error: "):
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
296 """Generate a useful error message from an EnvironmentError (IOError or
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
297 OSError) exception object. Handles Python 1.5.1 and 1.5.2 styles, and
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
298 does what it can to deal with exception objects that don't have a
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
299 filename (which happens when the error is due to a two-file operation,
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
300 such as 'rename()' or 'link()'. Returns the error message as a string
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
301 prefixed with 'prefix'.
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
302 """
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
303 # check for Python 1.5.2-style {IO,OS}Error exception objects
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
304 if hasattr(exc, 'filename') and hasattr(exc, 'strerror'):
12935
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
305 if exc.filename:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
306 error = prefix + "%s: %s" % (exc.filename, exc.strerror)
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
307 else:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
308 # two-argument functions in posix module don't
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
309 # include the filename in the exception object!
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
310 error = prefix + "%s" % exc.strerror
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
311 else:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
312 error = prefix + str(exc[-1])
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
313
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
314 return error
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
315
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
316
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
317 # Needed by 'split_quoted()'
31806
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
318 _wordchars_re = _squote_re = _dquote_re = None
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
319 def _init_regex():
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
320 global _wordchars_re, _squote_re, _dquote_re
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
321 _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
322 _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
323 _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
324
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
325 def split_quoted (s):
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
326 """Split a string up according to Unix shell-like rules for quotes and
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
327 backslashes. In short: words are delimited by spaces, as long as those
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
328 spaces are not escaped by a backslash, or inside a quoted string.
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
329 Single and double quotes are equivalent, and the quote characters can
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
330 be backslash-escaped. The backslash is stripped from any two-character
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
331 escape sequence, leaving only the escaped character. The quote
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
332 characters are stripped from any quoted string. Returns a list of
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
333 words.
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
334 """
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
335
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
336 # This is a nice algorithm for splitting up a single string, since it
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
337 # doesn't require character-by-character examination. It was a little
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
338 # bit of a brain-bender to get it working right, though...
31806
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
339 if _wordchars_re is None: _init_regex()
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
340
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
341 s = string.strip(s)
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
342 words = []
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
343 pos = 0
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
344
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
345 while s:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
346 m = _wordchars_re.match(s, pos)
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
347 end = m.end()
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
348 if end == len(s):
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
349 words.append(s[:end])
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
350 break
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
351
13891
08feb538c837 Fix so 'split_quoted()' handles any whitespace delimiter (not just space).
Greg Ward <gward@python.net>
parents: 13870
diff changeset
352 if s[end] in string.whitespace: # unescaped, unquoted whitespace: now
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
353 words.append(s[:end]) # we definitely have a word delimiter
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
354 s = string.lstrip(s[end:])
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
355 pos = 0
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
356
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
357 elif s[end] == '\\': # preserve whatever is being escaped;
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
358 # will become part of the current word
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
359 s = s[:end] + s[end+1:]
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
360 pos = end+1
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
361
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
362 else:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
363 if s[end] == "'": # slurp singly-quoted string
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
364 m = _squote_re.match(s, end)
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
365 elif s[end] == '"': # slurp doubly-quoted string
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
366 m = _dquote_re.match(s, end)
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
367 else:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
368 raise RuntimeError, \
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
369 "this can't happen (bad char '%c')" % s[end]
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
370
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
371 if m is None:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
372 raise ValueError, \
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
373 "bad string (mismatched %s quotes?)" % s[end]
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
374
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
375 (beg, end) = m.span()
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
376 s = s[:beg] + s[beg+1:end-1] + s[end:]
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
377 pos = m.end() - 2
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
378
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
379 if pos >= len(s):
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
380 words.append(s)
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
381 break
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
382
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
383 return words
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
384
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
385 # split_quoted ()
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
386
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
387
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
388 def execute (func, args, msg=None, verbose=0, dry_run=0):
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
389 """Perform some action that affects the outside world (eg. by
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
390 writing to the filesystem). Such actions are special because they
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
391 are disabled by the 'dry_run' flag. This method takes care of all
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
392 that bureaucracy for you; all you have to do is supply the
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
393 function to call and an argument tuple for it (to embody the
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
394 "external action" being performed), and an optional message to
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
395 print.
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
396 """
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
397 if msg is None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
398 msg = "%s%r" % (func.__name__, args)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
399 if msg[-2:] == ',)': # correct for singleton tuple
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
400 msg = msg[0:-2] + ')'
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
401
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
402 log.info(msg)
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
403 if not dry_run:
63603
fd249750cbae Merged revisions 78757 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 63579
diff changeset
404 func(*args)
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
405
14828
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
406
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
407 def strtobool (val):
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
408 """Convert a string representation of truth to true (1) or false (0).
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 31806
diff changeset
409
14828
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
410 True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
411 are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
412 'val' is anything else.
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
413 """
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
414 val = string.lower(val)
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
415 if val in ('y', 'yes', 't', 'true', 'on', '1'):
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
416 return 1
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
417 elif val in ('n', 'no', 'f', 'false', 'off', '0'):
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
418 return 0
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
419 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
420 raise ValueError, "invalid truth value %r" % (val,)
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
421
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
422
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
423 def byte_compile (py_files,
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
424 optimize=0, force=0,
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
425 prefix=None, base_dir=None,
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
426 verbose=1, dry_run=0,
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
427 direct=None):
14958
50386c73ba22 Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents: 14951
diff changeset
428 """Byte-compile a collection of Python source files to either .pyc
50386c73ba22 Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents: 14951
diff changeset
429 or .pyo files in the same directory. 'py_files' is a list of files
50386c73ba22 Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents: 14951
diff changeset
430 to compile; any files that don't end in ".py" are silently skipped.
50386c73ba22 Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents: 14951
diff changeset
431 'optimize' must be one of the following:
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
432 0 - don't optimize (generate .pyc)
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
433 1 - normal optimization (like "python -O")
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
434 2 - extra optimization (like "python -OO")
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
435 If 'force' is true, all files are recompiled regardless of
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
436 timestamps.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
437
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
438 The source filename encoded in each bytecode file defaults to the
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
439 filenames listed in 'py_files'; you can modify these with 'prefix' and
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
440 'basedir'. 'prefix' is a string that will be stripped off of each
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
441 source filename, and 'base_dir' is a directory name that will be
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
442 prepended (after 'prefix' is stripped). You can supply either or both
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
443 (or neither) of 'prefix' and 'base_dir', as you wish.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
444
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
445 If 'dry_run' is true, doesn't actually do anything that would
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
446 affect the filesystem.
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
447
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
448 Byte-compilation is either done directly in this interpreter process
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
449 with the standard py_compile module, or indirectly by writing a
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
450 temporary script and executing it. Normally, you should let
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
451 'byte_compile()' figure out to use direct compilation or not (see
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
452 the source for details). The 'direct' flag is used by the script
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
453 generated in indirect mode; unless you know what you're doing, leave
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
454 it set to None.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
455 """
58205
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57909
diff changeset
456 # nothing is done if sys.dont_write_bytecode is True
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57909
diff changeset
457 if sys.dont_write_bytecode:
ac4931ab0369 Merged revisions 75669-75671 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 57909
diff changeset
458 raise DistutilsByteCompileError('byte-compiling is disabled.')
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
459
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
460 # First, if the caller didn't force us into direct or indirect mode,
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
461 # figure out which mode we should be in. We take a conservative
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
462 # approach: choose direct mode *only* if the current interpreter is
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
463 # in debug mode and optimize is 0. If we're not in debug mode (-O
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
464 # or -OO), we don't know which level of optimization this
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
465 # interpreter is running with, so we can't do direct
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
466 # byte-compilation and be certain that it's the right thing. Thus,
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
467 # always compile indirectly if the current interpreter is in either
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
468 # optimize mode, or if either optimization level was requested by
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
469 # the caller.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
470 if direct is None:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
471 direct = (__debug__ and optimize == 0)
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
472
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
473 # "Indirect" byte-compilation: write a temporary script and then
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
474 # run it with the appropriate flags.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
475 if not direct:
26509
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
476 try:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
477 from tempfile import mkstemp
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
478 (script_fd, script_name) = mkstemp(".py")
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
479 except ImportError:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
480 from tempfile import mktemp
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
481 (script_fd, script_name) = None, mktemp(".py")
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
482 log.info("writing byte-compilation script '%s'", script_name)
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
483 if not dry_run:
26509
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
484 if script_fd is not None:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
485 script = os.fdopen(script_fd, "w")
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
486 else:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
487 script = open(script_name, "w")
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
488
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
489 script.write("""\
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
490 from distutils.util import byte_compile
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
491 files = [
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
492 """)
14982
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
493
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
494 # XXX would be nice to write absolute filenames, just for
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
495 # safety's sake (script should be more robust in the face of
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
496 # chdir'ing before running it). But this requires abspath'ing
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
497 # 'prefix' as well, and that breaks the hack in build_lib's
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
498 # 'byte_compile()' method that carefully tacks on a trailing
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
499 # slash (os.sep really) to make sure the prefix here is "just
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
500 # right". This whole prefix business is rather delicate -- the
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
501 # problem is that it's really a directory, but I'm treating it
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
502 # as a dumb string, so trailing slashes and so forth matter.
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
503
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
504 #py_files = map(os.path.abspath, py_files)
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
505 #if prefix:
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
506 # prefix = os.path.abspath(prefix)
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
507
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
508 script.write(string.join(map(repr, py_files), ",\n") + "]\n")
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
509 script.write("""
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
510 byte_compile(files, optimize=%r, force=%r,
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
511 prefix=%r, base_dir=%r,
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
512 verbose=%r, dry_run=0,
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
513 direct=1)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
514 """ % (optimize, force, prefix, base_dir, verbose))
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
515
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
516 script.close()
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
517
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
518 cmd = [sys.executable, script_name]
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
519 if optimize == 1:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
520 cmd.insert(1, "-O")
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
521 elif optimize == 2:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
522 cmd.insert(1, "-OO")
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
523 spawn(cmd, dry_run=dry_run)
14982
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
524 execute(os.remove, (script_name,), "removing %s" % script_name,
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
525 dry_run=dry_run)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
526
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
527 # "Direct" byte-compilation: use the py_compile module to compile
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
528 # right here, right now. Note that the script generated in indirect
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
529 # mode simply calls 'byte_compile()' in direct mode, a weird sort of
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
530 # cross-process recursion. Hey, it works!
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
531 else:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
532 from py_compile import compile
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
533
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
534 for file in py_files:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
535 if file[-3:] != ".py":
14958
50386c73ba22 Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents: 14951
diff changeset
536 # This lets us be lazy and not filter filenames in
50386c73ba22 Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents: 14951
diff changeset
537 # the "install_lib" command.
50386c73ba22 Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents: 14951
diff changeset
538 continue
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
539
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
540 # Terminology from the py_compile module:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
541 # cfile - byte-compiled file
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
542 # dfile - purported source filename (same as 'file' by default)
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
543 cfile = file + (__debug__ and "c" or "o")
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
544 dfile = file
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
545 if prefix:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
546 if file[:len(prefix)] != prefix:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
547 raise ValueError, \
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
548 ("invalid prefix: filename %r doesn't start with %r"
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
549 % (file, prefix))
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
550 dfile = dfile[len(prefix):]
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
551 if base_dir:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
552 dfile = os.path.join(base_dir, dfile)
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
553
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
554 cfile_base = os.path.basename(cfile)
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
555 if direct:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
556 if force or newer(file, cfile):
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
557 log.info("byte-compiling %s to %s", file, cfile_base)
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
558 if not dry_run:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
559 compile(file, cfile, dfile)
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
560 else:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
561 log.debug("skipping byte-compilation of %s to %s",
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
562 file, cfile_base)
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
563
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
564 # byte_compile ()
17126
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
565
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
566 def rfc822_escape (header):
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
567 """Return a version of the string escaped for inclusion in an
17206
782239488058 Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents: 17126
diff changeset
568 RFC-822 header, by ensuring there are 8 spaces space after each newline.
17126
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
569 """
17206
782239488058 Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents: 17126
diff changeset
570 lines = string.split(header, '\n')
782239488058 Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents: 17126
diff changeset
571 header = string.join(lines, '\n' + 8*' ')
17126
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
572 return header