annotate Lib/distutils/util.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents df3923c36ec5
children a6df074d2e09 5d749bdda979
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
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
9 import sys, os, string, re
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
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
14
11887
81f1069e1558 Added 'get_platform()' to construct a string that identifies the current
Greg Ward <gward@python.net>
parents: 11684
diff changeset
15 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
16 """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
17 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
18 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
19 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
20 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
21 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
22 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
23 important.
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
24
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
25 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
26 linux-i586
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
27 linux-alpha (?)
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
28 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
29 irix-5.3
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
30 irix64-6.2
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
31
42683
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
32 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
33 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
34 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
35 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
36
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
37 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
38 """
42683
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
39 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
40 # 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
41 prefix = " bit ("
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
42 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
43 if i == -1:
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
44 return sys.platform
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
45 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
46 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
47 if look=='amd64':
46331
a93d2919ea59 Issue #2513: enable 64bit cross compilation on windows.
Mark Hammond <mhammond@skippinet.com.au>
parents: 45413
diff changeset
48 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
49 if look=='itanium':
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
50 return 'win-ia64'
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
51 return sys.platform
ddff521ac7f1 [ 1761786 ] distutils.util.get_platform() return value on 64bit Windows
Mark Hammond <mhammond@skippinet.com.au>
parents: 38272
diff changeset
52
14941
f9cf611096bf In 'get_platform()', handle so-called POSIX systems that don't have
Greg Ward <gward@python.net>
parents: 14860
diff changeset
53 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
54 # 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
55 # 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
56 return sys.platform
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
57
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
58 # 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
59
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
60 (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
61
27106
aee43a4f3570 Translate spaces in the machine name to underscores
Andrew M. Kuchling <amk@amk.ca>
parents: 26509
diff changeset
62 # 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
63 # (to accommodate BSD/OS), and translate spaces (for "Power Macintosh")
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
64 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
65 osname = string.replace(osname, '/', '')
27106
aee43a4f3570 Translate spaces in the machine name to underscores
Andrew M. Kuchling <amk@amk.ca>
parents: 26509
diff changeset
66 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
67 machine = string.replace(machine, '/', '-')
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
68
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
69 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
70 # 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
71 # i386, etc.
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
72 # 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
73 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
74 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
75 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
76 osname = "solaris"
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
77 release = "%d.%s" % (int(release[0]) - 3, release[2:])
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
78 # 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
79 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
80 return "%s-%s" % (osname, release)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
81 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
82 return "%s-%s.%s" % (osname, version, release)
16035
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
83 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
84 osname = "cygwin"
16035
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
85 rel_re = re.compile (r'[\d.]+')
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
86 m = rel_re.match(release)
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
87 if m:
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
88 release = m.group()
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
89 elif osname[:6] == "darwin":
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
90 #
38272
de9f1a140611 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38248
diff changeset
91 # For our purposes, we'll assume that the system version from
de9f1a140611 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 38248
diff changeset
92 # distutils' perspective is what MACOSX_DEPLOYMENT_TARGET is set
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
93 # 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
94 # machine is going to compile and link as if it were
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
95 # MACOSX_DEPLOYMENT_TARGET.
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
96 from distutils.sysconfig import get_config_vars
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
97 cfgvars = get_config_vars()
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
98
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
99 macver = os.environ.get('MACOSX_DEPLOYMENT_TARGET')
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
100 if not macver:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
101 macver = cfgvars.get('MACOSX_DEPLOYMENT_TARGET')
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
102
50341
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
103 if 1:
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
104 # Always calculate the release of the running machine,
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
105 # needed to determine if we can build fat binaries or not.
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
106
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
107 macrelease = macver
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
108 # Get the system version. Reading this plist is a documented
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
109 # way to get the system version (see the documentation for
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
110 # the Gestalt Manager)
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
111 try:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
112 f = open('/System/Library/CoreServices/SystemVersion.plist')
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
113 except IOError:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
114 # We're on a plain darwin box, fall back to the default
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
115 # behaviour.
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
116 pass
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
117 else:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
118 m = re.search(
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
119 r'<key>ProductUserVisibleVersion</key>\s*' +
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
120 r'<string>(.*?)</string>', f.read())
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
121 f.close()
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
122 if m is not None:
50341
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
123 macrelease = '.'.join(m.group(1).split('.')[:2])
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
124 # else: fall back to the default behaviour
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
125
50341
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
126 if not macver:
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
127 macver = macrelease
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
128
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
129 if macver:
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
130 from distutils.sysconfig import get_config_vars
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
131 release = macver
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
132 osname = "macosx"
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
133
50341
457ec884af04 Backport of r67988
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 47473
diff changeset
134 if (macrelease + '.') >= '10.4.' and \
47473
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
135 '-arch' in get_config_vars().get('CFLAGS', '').strip():
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
136 # The universal build will build fat binaries, but not on
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
137 # systems before 10.4
47473
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
138 #
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
139 # 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
140 # 'universal' instead of 'fat'.
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
141
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
142 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
143 cflags = get_config_vars().get('CFLAGS')
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
144
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
145 if '-arch x86_64' in cflags:
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
146 if '-arch i386' in cflags:
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
147 machine = 'universal'
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
148 else:
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 machine = 'fat64'
47473
c34e34057714 MacOS X: Enable 4-way universal builds
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 46331
diff changeset
150
38248
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
151 elif machine in ('PowerPC', 'Power_Macintosh'):
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
152 # Pick a sane name for the PPC architecture.
3078fdb7cf3d Patch #1488098.
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 38033
diff changeset
153 machine = 'ppc'
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
154
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
155 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
156
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
157 # 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
158
7bd97c3dd432 Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents: 11916
diff changeset
159
12804
3be92cef1917 Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents: 12802
diff changeset
160 def convert_path (pathname):
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
161 """Return 'pathname' as a name that will work on the native filesystem,
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
162 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
163 directory separator. Needed because filenames in the setup script are
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
164 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
165 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
166 ValueError on non-Unix-ish systems if 'pathname' either starts or
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
167 ends with a slash.
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
168 """
14743
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
169 if os.sep == '/':
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
170 return pathname
24786
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
171 if not pathname:
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
172 return pathname
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
173 if pathname[0] == '/':
12407
01cff9fe2d45 Cleaned up/simplified error-handling:
Greg Ward <gward@python.net>
parents: 12226
diff changeset
174 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
175 if pathname[-1] == '/':
12407
01cff9fe2d45 Cleaned up/simplified error-handling:
Greg Ward <gward@python.net>
parents: 12226
diff changeset
176 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
177
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
178 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
179 while '.' in paths:
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
180 paths.remove('.')
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
181 if not paths:
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
182 return os.curdir
14743
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
183 return apply(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
184
12804
3be92cef1917 Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents: 12802
diff changeset
185 # convert_path ()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
186
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
187
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
188 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
189 """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
190 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
191 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
192 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
193 """
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
194 if os.name == 'posix':
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
195 if not os.path.isabs(pathname):
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
196 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
197 else:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
198 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
199
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
200 elif os.name == 'nt':
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
201 (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
202 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
203 path = path[1:]
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
204 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
205
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
206 elif os.name == 'os2':
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
207 (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
208 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
209 path = path[1:]
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
210 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
211
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
212 elif os.name == 'mac':
14709
e024438522dd Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents: 14573
diff changeset
213 if not os.path.isabs(pathname):
e024438522dd Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents: 14573
diff changeset
214 return os.path.join(new_root, pathname)
e024438522dd Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents: 14573
diff changeset
215 else:
e024438522dd Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents: 14573
diff changeset
216 # Chop off volume name from start of path
e024438522dd Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents: 14573
diff changeset
217 elements = string.split(pathname, ":", 1)
e024438522dd Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents: 14573
diff changeset
218 pathname = ":" + elements[1]
e024438522dd Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents: 14573
diff changeset
219 return os.path.join(new_root, pathname)
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
220
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
221 else:
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
222 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
223 "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
224
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
225
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
226 _environ_checked = 0
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
227 def check_environ ():
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
228 """Ensure that 'os.environ' has all the environment variables we
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
229 guarantee that users can use in config files, command-line options,
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
230 etc. Currently this includes:
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
231 HOME - user's home directory (Unix only)
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
232 PLAT - description of the current platform, including hardware
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
233 and OS (see 'get_platform()')
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
234 """
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
235 global _environ_checked
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
236 if _environ_checked:
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
237 return
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
238
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 42683
diff changeset
239 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
240 import pwd
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
241 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
242
45413
8a187f562959 Removed uses of dict.has_key() from distutils, and uses of
Guido van Rossum <guido@python.org>
parents: 42683
diff changeset
243 if 'PLAT' not in os.environ:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
244 os.environ['PLAT'] = get_platform()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
245
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
246 _environ_checked = 1
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
247
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
248
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
249 def subst_vars (s, local_vars):
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
250 """Perform shell/Perl-style variable substitution on 'string'. Every
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
251 occurrence of '$' followed by a name is considered a variable, and
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
252 variable is substituted by the value found in the 'local_vars'
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
253 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
254 'os.environ' is first checked/augmented to guarantee that it contains
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
255 certain values: see 'check_environ()'. Raise ValueError for any
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
256 variables not found in either 'local_vars' or 'os.environ'.
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
257 """
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
258 check_environ()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
259 def _subst (match, local_vars=local_vars):
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
260 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
261 if var_name in local_vars:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
262 return str(local_vars[var_name])
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
263 else:
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
264 return os.environ[var_name]
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
265
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
266 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
267 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
268 except KeyError, var:
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
269 raise ValueError, "invalid variable '$%s'" % var
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
270
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
271 # subst_vars ()
12102
e81b139ab8a4 Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents: 12044
diff changeset
272
e81b139ab8a4 Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents: 12044
diff changeset
273
12935
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
274 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
275 """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
276 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
277 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
278 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
279 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
280 prefixed with 'prefix'.
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
281 """
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
282 # 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
283 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
284 if exc.filename:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
285 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
286 else:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
287 # 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
288 # 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
289 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
290 else:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
291 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
292
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
293 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
294
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
295
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
296 # 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
297 _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
298 def _init_regex():
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
299 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
300 _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
301 _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
302 _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
303
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
304 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
305 """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
306 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
307 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
308 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
309 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
310 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
311 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
312 words.
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
313 """
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
314
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
315 # 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
316 # 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
317 # 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
318 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
319
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
320 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
321 words = []
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
322 pos = 0
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
323
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
324 while s:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
325 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
326 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
327 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
328 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
329 break
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
330
13891
08feb538c837 Fix so 'split_quoted()' handles any whitespace delimiter (not just space).
Greg Ward <gward@python.net>
parents: 13870
diff changeset
331 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
332 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
333 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
334 pos = 0
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 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
337 # 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
338 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
339 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
340
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
341 else:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
342 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
343 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
344 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
345 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
346 else:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
347 raise RuntimeError, \
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
348 "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
349
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
350 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
351 raise ValueError, \
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
352 "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
353
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
354 (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
355 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
356 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
357
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
358 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
359 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
360 break
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 return words
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
363
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
364 # 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
365
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
366
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
367 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
368 """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
369 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
370 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
371 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
372 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
373 "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
374 print.
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
375 """
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
376 if msg is None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
377 msg = "%s%r" % (func.__name__, args)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
378 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
379 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
380
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
381 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
382 if not dry_run:
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
383 apply(func, args)
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
384
14828
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
385
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
386 def strtobool (val):
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
387 """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
388
14828
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
389 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
390 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
391 'val' is anything else.
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
392 """
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
393 val = string.lower(val)
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
394 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
395 return 1
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
396 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
397 return 0
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
398 else:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
399 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
400
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
401
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
402 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
403 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
404 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
405 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
406 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
407 """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
408 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
409 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
410 '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
411 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
412 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
413 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
414 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
415 timestamps.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
416
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
417 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
418 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
419 '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
420 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
421 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
422 (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
423
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
424 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
425 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
426
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
427 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
428 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
429 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
430 '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
431 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
432 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
433 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
434 """
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
435
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
436 # 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
437 # 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
438 # 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
439 # 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
440 # 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
441 # 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
442 # 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
443 # 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
444 # 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
445 # the caller.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
446 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
447 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
448
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
449 # "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
450 # 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
451 if not direct:
26509
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
452 try:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
453 from tempfile import mkstemp
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
454 (script_fd, script_name) = mkstemp(".py")
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
455 except ImportError:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
456 from tempfile import mktemp
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
457 (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
458 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
459 if not dry_run:
26509
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
460 if script_fd is not None:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
461 script = os.fdopen(script_fd, "w")
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
462 else:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
463 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
464
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
465 script.write("""\
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
466 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
467 files = [
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
468 """)
14982
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
469
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
470 # 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
471 # 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
472 # 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
473 # '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
474 # '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
475 # 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
476 # 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
477 # 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
478 # 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
479
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
480 #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
481 #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
482 # 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
483
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
484 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
485 script.write("""
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
486 byte_compile(files, optimize=%r, force=%r,
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
487 prefix=%r, base_dir=%r,
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
488 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
489 direct=1)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
490 """ % (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
491
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
492 script.close()
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
493
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
494 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
495 if optimize == 1:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
496 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
497 elif optimize == 2:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
498 cmd.insert(1, "-OO")
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
499 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
500 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
501 dry_run=dry_run)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
502
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
503 # "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
504 # 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
505 # 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
506 # 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
507 else:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
508 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
509
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
510 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
511 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
512 # 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
513 # 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
514 continue
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 # 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
517 # 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
518 # 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
519 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
520 dfile = file
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
521 if prefix:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
522 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
523 raise ValueError, \
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
524 ("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
525 % (file, prefix))
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
526 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
527 if base_dir:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
528 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
529
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
530 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
531 if direct:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
532 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
533 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
534 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
535 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
536 else:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
537 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
538 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
539
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
540 # byte_compile ()
17126
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
541
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
542 def rfc822_escape (header):
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
543 """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
544 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
545 """
17206
782239488058 Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents: 17126
diff changeset
546 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
547 lines = map(string.strip, lines)
782239488058 Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents: 17126
diff changeset
548 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
549 return header