annotate Lib/distutils/util.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 147b9d530dd0
children 7d69d04522e3
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
72818
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
7 import os
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
8 import re
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
9 import imp
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
10 import sys
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
11 import string
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
12 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
13 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
14 from distutils.spawn import spawn
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
15 from distutils import log
56741
a3cb812d80f9 Merged revisions 75704 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 55975
diff changeset
16 from distutils.errors import DistutilsByteCompileError
12453
d0f290b3c0b2 Merged in Python 1.5.1 compatibility changes from the 0.1.3 branch:
Greg Ward <gward@python.net>
parents: 12407
diff changeset
17
11887
81f1069e1558 Added 'get_platform()' to construct a string that identifies the current
Greg Ward <gward@python.net>
parents: 11684
diff changeset
18 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
19 """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
20 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
21 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
22 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
23 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
24 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
25 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
26 important.
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
27
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
28 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
29 linux-i586
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
30 linux-alpha (?)
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
31 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
32 irix-5.3
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
33 irix64-6.2
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
34
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
35 Windows will return one of:
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44544
diff changeset
36 win-amd64 (64bit Windows on AMD64 (aka x86_64, Intel64, EM64T, etc)
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
37 win-ia64 (64bit Windows on Itanium)
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
38 win32 (all others - specifically, sys.platform is returned)
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
39
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
40 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
41 """
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
42 if os.name == 'nt':
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
43 # sniff sys.version for architecture.
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
44 prefix = " bit ("
43029
708033ed9515 Stop using the find function on the string module, use the string method.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43026
diff changeset
45 i = sys.version.find(prefix)
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
46 if i == -1:
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
47 return sys.platform
43029
708033ed9515 Stop using the find function on the string module, use the string method.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43026
diff changeset
48 j = sys.version.find(")", i)
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
49 look = sys.version[i+len(prefix):j].lower()
43029
708033ed9515 Stop using the find function on the string module, use the string method.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43026
diff changeset
50 if look == 'amd64':
46370
2e917b0c8f6e Merged revisions 62194,62197-62198,62204-62205,62214,62219-62221,62227,62229-62231,62233-62235,62237-62239 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44544
diff changeset
51 return 'win-amd64'
43029
708033ed9515 Stop using the find function on the string module, use the string method.
Neal Norwitz <nnorwitz@gmail.com>
parents: 43026
diff changeset
52 if look == 'itanium':
42775
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
53 return 'win-ia64'
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
54 return sys.platform
d7517c306791 Merged revisions 57221-57391 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41480
diff changeset
55
77886
cfbe51e66749 - Issue #14330: For cross builds, don't use host python, use host search paths
doko@ubuntu.com
parents: 74491
diff changeset
56 # Set for cross builds explicitly
cfbe51e66749 - Issue #14330: For cross builds, don't use host python, use host search paths
doko@ubuntu.com
parents: 74491
diff changeset
57 if "_PYTHON_HOST_PLATFORM" in os.environ:
cfbe51e66749 - Issue #14330: For cross builds, don't use host python, use host search paths
doko@ubuntu.com
parents: 74491
diff changeset
58 return os.environ["_PYTHON_HOST_PLATFORM"]
cfbe51e66749 - Issue #14330: For cross builds, don't use host python, use host search paths
doko@ubuntu.com
parents: 74491
diff changeset
59
14941
f9cf611096bf In 'get_platform()', handle so-called POSIX systems that don't have
Greg Ward <gward@python.net>
parents: 14860
diff changeset
60 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
61 # 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
62 # 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
63 return sys.platform
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
64
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
65 # 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
66
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
67 (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
68
27106
aee43a4f3570 Translate spaces in the machine name to underscores
Andrew M. Kuchling <amk@amk.ca>
parents: 26509
diff changeset
69 # 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
70 # (to accommodate BSD/OS), and translate spaces (for "Power Macintosh")
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
71 osname = osname.lower().replace('/', '')
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
72 machine = machine.replace(' ', '_')
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
73 machine = machine.replace('/', '-')
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
74
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
75 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
76 # 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
77 # i386, etc.
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
78 # 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
79 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
80 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
81 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
82 osname = "solaris"
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
83 release = "%d.%s" % (int(release[0]) - 3, release[2:])
74483
9d62f5aa35ff Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74480
diff changeset
84 # We can't use "platform.architecture()[0]" because a
9d62f5aa35ff Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74480
diff changeset
85 # bootstrap problem. We use a dict to get an error
9d62f5aa35ff Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74480
diff changeset
86 # if some suspicious happens.
9d62f5aa35ff Emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74480
diff changeset
87 bitness = {2147483647:"32bit", 9223372036854775807:"64bit"}
74491
147ad02875fa And yet another emergency fix for #13803 bootstrap issue: Under Solaris, distutils doesn't include bitness in the directory name
Jesus Cea <jcea@jcea.es>
parents: 74483
diff changeset
88 machine += ".%s" % bitness[sys.maxsize]
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
89 # 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
90 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
91 return "%s-%s" % (osname, release)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
92 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
93 return "%s-%s.%s" % (osname, version, release)
16035
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
94 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
95 osname = "cygwin"
48769
83b2d07b475b #2834: Change re module semantics, so that str and bytes mixing is forbidden,
Antoine Pitrou <solipsis@pitrou.net>
parents: 48485
diff changeset
96 rel_re = re.compile (r'[\d.]+', re.ASCII)
16035
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
97 m = rel_re.match(release)
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
98 if m:
1144afb94019 Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents: 15217
diff changeset
99 release = m.group()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 37048
diff changeset
100 elif osname[:6] == "darwin":
78216
d76b83803e7e Issue #15184: Ensure consistent results of OS X configuration
Ned Deily <nad@acm.org>
parents: 77886
diff changeset
101 import _osx_support, distutils.sysconfig
d76b83803e7e Issue #15184: Ensure consistent results of OS X configuration
Ned Deily <nad@acm.org>
parents: 77886
diff changeset
102 osname, release, machine = _osx_support.get_platform_osx(
d76b83803e7e Issue #15184: Ensure consistent results of OS X configuration
Ned Deily <nad@acm.org>
parents: 77886
diff changeset
103 distutils.sysconfig.get_config_vars(),
d76b83803e7e Issue #15184: Ensure consistent results of OS X configuration
Ned Deily <nad@acm.org>
parents: 77886
diff changeset
104 osname, release, machine)
57911
71e6a0dfafc8 Merged revisions 77028 via svnmerge from
Ronald Oussoren <ronaldoussoren@mac.com>
parents: 57588
diff changeset
105
14573
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
106 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
107
857376d699bb Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents: 13891
diff changeset
108 # 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
109
7bd97c3dd432 Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents: 11916
diff changeset
110
12804
3be92cef1917 Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents: 12802
diff changeset
111 def convert_path (pathname):
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
112 """Return 'pathname' as a name that will work on the native filesystem,
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
113 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
114 directory separator. Needed because filenames in the setup script are
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
115 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
116 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
117 ValueError on non-Unix-ish systems if 'pathname' either starts or
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
118 ends with a slash.
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
119 """
14743
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
120 if os.sep == '/':
8db096b782f9 Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents: 14709
diff changeset
121 return pathname
24786
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
122 if not pathname:
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
123 return pathname
f0d63aa1dfc7 SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents: 24710
diff changeset
124 if pathname[0] == '/':
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
125 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
126 if pathname[-1] == '/':
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
127 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
128
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
129 paths = pathname.split('/')
16321
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
130 while '.' in paths:
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
131 paths.remove('.')
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
132 if not paths:
a58c993d9234 Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents: 16261
diff changeset
133 return os.curdir
37048
c048a09d06ff Remove apply()
Neal Norwitz <nnorwitz@gmail.com>
parents: 32736
diff changeset
134 return os.path.join(*paths)
11934
7bd97c3dd432 Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents: 11916
diff changeset
135
12804
3be92cef1917 Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents: 12802
diff changeset
136 # convert_path ()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
137
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
138
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
139 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
140 """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
141 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
142 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
143 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
144 """
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
145 if os.name == 'posix':
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
146 if not os.path.isabs(pathname):
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
147 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
148 else:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
149 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
150
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
151 elif os.name == 'nt':
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
152 (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
153 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
154 path = path[1:]
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
155 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
156
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
157 else:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
158 raise DistutilsPlatformError("nothing known about platform '%s'" % os.name)
12507
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
159
5661f4f9a47f Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents: 12482
diff changeset
160
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
161 _environ_checked = 0
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
162 def check_environ ():
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
163 """Ensure that 'os.environ' has all the environment variables we
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
164 guarantee that users can use in config files, command-line options,
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
165 etc. Currently this includes:
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
166 HOME - user's home directory (Unix only)
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
167 PLAT - description of the current platform, including hardware
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
168 and OS (see 'get_platform()')
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
169 """
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
170 global _environ_checked
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
171 if _environ_checked:
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
172 return
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
173
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
174 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
175 import pwd
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
176 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
177
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
178 if 'PLAT' not in os.environ:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
179 os.environ['PLAT'] = get_platform()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
180
12672
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
181 _environ_checked = 1
47bdb636159c Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents: 12507
diff changeset
182
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
183
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
184 def subst_vars (s, local_vars):
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
185 """Perform shell/Perl-style variable substitution on 'string'. Every
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
186 occurrence of '$' followed by a name is considered a variable, and
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
187 variable is substituted by the value found in the 'local_vars'
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
188 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
189 'os.environ' is first checked/augmented to guarantee that it contains
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
190 certain values: see 'check_environ()'. Raise ValueError for any
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
191 variables not found in either 'local_vars' or 'os.environ'.
14949
14b647d550d3 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 14941
diff changeset
192 """
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
193 check_environ()
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
194 def _subst (match, local_vars=local_vars):
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
195 var_name = match.group(1)
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 38453
diff changeset
196 if var_name in local_vars:
14860
18b3def6c4b8 Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14828
diff changeset
197 return str(local_vars[var_name])
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
198 else:
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
199 return os.environ[var_name]
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
200
14950
f8b526fc39f6 Various docstring tweaks.
Greg Ward <gward@python.net>
parents: 14949
diff changeset
201 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
202 return re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
203 except KeyError as var:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
204 raise ValueError("invalid variable '$%s'" % var)
12033
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
205
856dadf56cd9 Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents: 12019
diff changeset
206 # subst_vars ()
12102
e81b139ab8a4 Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents: 12044
diff changeset
207
e81b139ab8a4 Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents: 12044
diff changeset
208
12935
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
209 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
210 """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
211 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
212 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
213 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
214 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
215 prefixed with 'prefix'.
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
216 """
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
217 # 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
218 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
219 if exc.filename:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
220 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
221 else:
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
222 # 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
223 # 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
224 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
225 else:
44544
b8e34bcdba45 Missed one because of indirection.
Georg Brandl <georg@python.org>
parents: 43029
diff changeset
226 error = prefix + str(exc.args[-1])
12935
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
227
a98009a4391e Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents: 12804
diff changeset
228 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
229
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
230
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
231 # 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
232 _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
233 def _init_regex():
078032444ecc Defer compilation of regular expressions until first use.
Martin v. Löwis <martin@v.loewis.de>
parents: 31515
diff changeset
234 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
235 _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
236 _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
237 _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
238
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
239 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
240 """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
241 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
242 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
243 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
244 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
245 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
246 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
247 words.
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
248 """
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
249
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
250 # 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
251 # 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
252 # 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
253 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
254
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
255 s = s.strip()
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
256 words = []
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
257 pos = 0
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
258
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
259 while s:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
260 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
261 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
262 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
263 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
264 break
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
265
13891
08feb538c837 Fix so 'split_quoted()' handles any whitespace delimiter (not just space).
Greg Ward <gward@python.net>
parents: 13870
diff changeset
266 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
267 words.append(s[:end]) # we definitely have a word delimiter
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
268 s = s[end:].lstrip()
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
269 pos = 0
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
270
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
271 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
272 # 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
273 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
274 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
275
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
276 else:
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
277 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
278 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
279 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
280 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
281 else:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
282 raise RuntimeError("this can't happen (bad char '%c')" % s[end])
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
283
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
284 if m is None:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
285 raise ValueError("bad string (mismatched %s quotes?)" % s[end])
13006
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
286
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
287 (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
288 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
289 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
290
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
291 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
292 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
293 break
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 return words
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
296
87003842faec Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents: 12951
diff changeset
297 # 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
298
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
299
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
300 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
301 """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
302 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
303 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
304 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
305 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
306 "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
307 print.
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
308 """
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
309 if msg is None:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
310 msg = "%s%r" % (func.__name__, args)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
311 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
312 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
313
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
314 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
315 if not dry_run:
37048
c048a09d06ff Remove apply()
Neal Norwitz <nnorwitz@gmail.com>
parents: 32736
diff changeset
316 func(*args)
13807
ba8db96ea4d9 Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents: 13801
diff changeset
317
14828
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
318
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
319 def strtobool (val):
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
320 """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
321
14828
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
322 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
323 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
324 'val' is anything else.
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
325 """
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
326 val = val.lower()
14828
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
327 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
328 return 1
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
329 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
330 return 0
9f96c92f03e3 Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents: 14743
diff changeset
331 else:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
332 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
333
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
334
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
335 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
336 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
337 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
338 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
339 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
340 """Byte-compile a collection of Python source files to either .pyc
72823
651e84363001 Fix docstring of distutils.util.byte_compile (followup for #11254)
Éric Araujo <merwok@netwok.org>
parents: 72818
diff changeset
341 or .pyo files in a __pycache__ subdirectory. 'py_files' is a list
651e84363001 Fix docstring of distutils.util.byte_compile (followup for #11254)
Éric Araujo <merwok@netwok.org>
parents: 72818
diff changeset
342 of files to compile; any files that don't end in ".py" are silently
651e84363001 Fix docstring of distutils.util.byte_compile (followup for #11254)
Éric Araujo <merwok@netwok.org>
parents: 72818
diff changeset
343 skipped. '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
344 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
345 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
346 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
347 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
348 timestamps.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
349
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
350 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
351 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
352 '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
353 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
354 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
355 (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
356
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
357 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
358 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
359
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
360 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
361 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
362 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
363 '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
364 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
365 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
366 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
367 """
56741
a3cb812d80f9 Merged revisions 75704 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 55975
diff changeset
368 # nothing is done if sys.dont_write_bytecode is True
a3cb812d80f9 Merged revisions 75704 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 55975
diff changeset
369 if sys.dont_write_bytecode:
a3cb812d80f9 Merged revisions 75704 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 55975
diff changeset
370 raise DistutilsByteCompileError('byte-compiling is disabled.')
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
371
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
372 # 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
373 # 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
374 # 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
375 # 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
376 # 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
377 # 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
378 # 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
379 # 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
380 # 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
381 # the caller.
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
382 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
383 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
384
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
385 # "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
386 # 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
387 if not direct:
26509
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
388 try:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
389 from tempfile import mkstemp
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
390 (script_fd, script_name) = mkstemp(".py")
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
391 except ImportError:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
392 from tempfile import mktemp
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
393 (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
394 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
395 if not dry_run:
26509
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
396 if script_fd is not None:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
397 script = os.fdopen(script_fd, "w")
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
398 else:
9e4c80340fa1 Adding Python <= 2.2 support back in.
Marc-André Lemburg <mal@egenix.com>
parents: 26210
diff changeset
399 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
400
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
401 script.write("""\
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
402 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
403 files = [
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
404 """)
14982
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
405
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
406 # 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
407 # 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
408 # 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
409 # '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
410 # '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
411 # 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
412 # 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
413 # 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
414 # 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
415
0fa58fea30cf Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents: 14960
diff changeset
416 #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
417 #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
418 # 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
419
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
420 script.write(",\n".join(map(repr, py_files)) + "]\n")
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
421 script.write("""
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
422 byte_compile(files, optimize=%r, force=%r,
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
423 prefix=%r, base_dir=%r,
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
424 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
425 direct=1)
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
426 """ % (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
427
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
428 script.close()
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
429
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
430 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
431 if optimize == 1:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
432 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
433 elif optimize == 2:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
434 cmd.insert(1, "-OO")
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
435 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
436 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
437 dry_run=dry_run)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 18782
diff changeset
438
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
439 # "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
440 # 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
441 # 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
442 # 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
443 else:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
444 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
445
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
446 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
447 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
448 # 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
449 # 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
450 continue
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
451
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
452 # 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
453 # 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
454 # dfile - purported source filename (same as 'file' by default)
72818
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
455 if optimize >= 0:
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
456 cfile = imp.cache_from_source(file, debug_override=not optimize)
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
457 else:
27a36b05caed Fix distutils byte-compilation to comply with PEP 3147 (#11254).
Éric Araujo <merwok@netwok.org>
parents: 71540
diff changeset
458 cfile = imp.cache_from_source(file)
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
459 dfile = file
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
460 if prefix:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
461 if file[:len(prefix)] != prefix:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 42775
diff changeset
462 raise ValueError("invalid prefix: filename %r doesn't start with %r"
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 27106
diff changeset
463 % (file, prefix))
14951
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
464 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
465 if base_dir:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
466 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
467
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
468 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
469 if direct:
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
470 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
471 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
472 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
473 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
474 else:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23382
diff changeset
475 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
476 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
477
005a0b4c382d Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents: 14950
diff changeset
478 # byte_compile ()
17126
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
479
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
480 def rfc822_escape (header):
1b8745faa2ed Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents: 16801
diff changeset
481 """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
482 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
483 """
57588
568618b5f6fd Merged revisions 76686 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56741
diff changeset
484 lines = header.split('\n')
568618b5f6fd Merged revisions 76686 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 56741
diff changeset
485 sep = '\n' + 8 * ' '
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40587
diff changeset
486 return sep.join(lines)
49911
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
487
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
488 # 2to3 support
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
489
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
490 def run_2to3(files, fixer_names=None, options=None, explicit=None):
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
491 """Invoke 2to3 on a list of Python files.
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
492 The files should all come from the build area, as the
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
493 modification is done in-place. To reduce the build time,
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
494 only files modified since the last invocation of this
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
495 function should be passed in the files argument."""
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
496
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
497 if not files:
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
498 return
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
499
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
500 # Make this class local, to delay import of 2to3
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
501 from lib2to3.refactor import RefactoringTool, get_fixers_from_package
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
502 class DistutilsRefactoringTool(RefactoringTool):
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
503 def log_error(self, msg, *args, **kw):
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
504 log.error(msg, *args)
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
505
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
506 def log_message(self, msg, *args):
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
507 log.info(msg, *args)
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
508
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
509 def log_debug(self, msg, *args):
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
510 log.debug(msg, *args)
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
511
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
512 if fixer_names is None:
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
513 fixer_names = get_fixers_from_package('lib2to3.fixes')
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
514 r = DistutilsRefactoringTool(fixer_names, options=options)
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
515 r.refactor(files, write=True)
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
516
52501
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
517 def copydir_run_2to3(src, dest, template=None, fixer_names=None,
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
518 options=None, explicit=None):
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
519 """Recursively copy a directory, only copying new and changed files,
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
520 running run_2to3 over all newly copied Python modules afterward.
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
521
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
522 If you give a template string, it's parsed like a MANIFEST.in.
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
523 """
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
524 from distutils.dir_util import mkpath
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
525 from distutils.file_util import copy_file
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
526 from distutils.filelist import FileList
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
527 filelist = FileList()
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
528 curdir = os.getcwd()
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
529 os.chdir(src)
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
530 try:
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
531 filelist.findall()
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
532 finally:
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
533 os.chdir(curdir)
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
534 filelist.files[:] = filelist.allfiles
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
535 if template:
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
536 for line in template.splitlines():
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
537 line = line.strip()
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
538 if not line: continue
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
539 filelist.process_template_line(line)
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
540 copied = []
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
541 for filename in filelist.files:
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
542 outname = os.path.join(dest, filename)
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
543 mkpath(os.path.dirname(outname))
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
544 res = copy_file(os.path.join(src, filename), outname, update=1)
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
545 if res[1]: copied.append(outname)
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
546 run_2to3([fn for fn in copied if fn.lower().endswith('.py')],
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
547 fixer_names=fixer_names, options=options, explicit=explicit)
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
548 return copied
89dea3df4cb5 Add new copydir_run_2to3() function, for use e.g. in test runners to transparently convert and run tests written for Python 2.
Georg Brandl <georg@python.org>
parents: 50380
diff changeset
549
49911
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
550 class Mixin2to3:
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
551 '''Mixin class for commands that run 2to3.
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
552 To configure 2to3, setup scripts may either change
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
553 the class variables, or inherit from individual commands
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
554 to override how 2to3 is invoked.'''
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
555
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
556 # provide list of fixers to run;
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
557 # defaults to all from lib2to3.fixers
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
558 fixer_names = None
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
559
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
560 # options dictionary
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
561 options = None
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
562
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
563 # list of fixers to invoke even though they are marked as explicit
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
564 explicit = None
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
565
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
566 def run_2to3(self, files):
60e775eaaaee Issue #4073: Add 2to3 support to build_scripts, refactor that support
Martin v. Löwis <martin@v.loewis.de>
parents: 48769
diff changeset
567 return run_2to3(files, self.fixer_names, self.options, self.explicit)