Mercurial > cpython-lru_cache-weakref
annotate Lib/distutils/util.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 0f61e074147f |
| children | a5874453bf17 |
| 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 | 4 one of the other *util.py modules. |
| 5 """ | |
|
10435
324450ea5450
First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
6 |
|
324450ea5450
First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
7 # created 1999/03/08, Greg Ward |
|
324450ea5450
First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
8 |
|
11897
017e592c0fd2
Changed '__rcsid__' to '__revision__'.
Greg Ward <gward@python.net>
parents:
11887
diff
changeset
|
9 __revision__ = "$Id$" |
|
10435
324450ea5450
First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
10 |
|
14951
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
11 import sys, os, string, re |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
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 |
|
10435
324450ea5450
First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff
changeset
|
15 |
|
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
|
16 |
|
11887
81f1069e1558
Added 'get_platform()' to construct a string that identifies the current
Greg Ward <gward@python.net>
parents:
11684
diff
changeset
|
17 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
|
18 """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
|
19 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
|
20 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
|
21 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
|
22 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
|
23 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
|
24 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
|
25 important. |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
26 |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
27 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
|
28 linux-i586 |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
29 linux-alpha (?) |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
30 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
|
31 irix-5.3 |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
32 irix64-6.2 |
| 21267 | 33 |
|
14573
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
34 For 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
|
35 """ |
|
14941
f9cf611096bf
In 'get_platform()', handle so-called POSIX systems that don't have
Greg Ward <gward@python.net>
parents:
14860
diff
changeset
|
36 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
|
37 # 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
|
38 # 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
|
39 return sys.platform |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
40 |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
41 # 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
|
42 |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
43 (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
|
44 |
|
2c9186e40b30
Bug #229280: remove '/' characters from the OS name (for BSD/OS :) )
Andrew M. Kuchling <amk@amk.ca>
parents:
16796
diff
changeset
|
45 # Convert the OS name to lowercase and remove '/' characters |
|
2c9186e40b30
Bug #229280: remove '/' characters from the OS name (for BSD/OS :) )
Andrew M. Kuchling <amk@amk.ca>
parents:
16796
diff
changeset
|
46 # (to accommodate BSD/OS) |
| 21267 | 47 osname = string.lower(osname) |
|
16801
2c9186e40b30
Bug #229280: remove '/' characters from the OS name (for BSD/OS :) )
Andrew M. Kuchling <amk@amk.ca>
parents:
16796
diff
changeset
|
48 osname = string.replace(osname, '/', '') |
| 21267 | 49 |
|
14573
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
50 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
|
51 # 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
|
52 # i386, etc. |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
53 # 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
|
54 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
|
55 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
|
56 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
|
57 osname = "solaris" |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
58 release = "%d.%s" % (int(release[0]) - 3, release[2:]) |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
59 # 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
|
60 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
|
61 return "%s-%s" % (osname, release) |
| 21267 | 62 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
|
63 return "%s-%s.%s" % (osname, version, release) |
|
16035
1144afb94019
Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents:
15217
diff
changeset
|
64 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
|
65 osname = "cygwin" |
|
16035
1144afb94019
Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents:
15217
diff
changeset
|
66 rel_re = re.compile (r'[\d.]+') |
|
1144afb94019
Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents:
15217
diff
changeset
|
67 m = rel_re.match(release) |
|
1144afb94019
Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents:
15217
diff
changeset
|
68 if m: |
|
1144afb94019
Patch #103220 from Jason Tishler:
Andrew M. Kuchling <amk@amk.ca>
parents:
15217
diff
changeset
|
69 release = m.group() |
| 21267 | 70 |
|
14573
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
71 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
|
72 |
|
857376d699bb
Revamped 'get_platform()' to try and do something reasonably smart on
Greg Ward <gward@python.net>
parents:
13891
diff
changeset
|
73 # 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
|
74 |
|
7bd97c3dd432
Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents:
11916
diff
changeset
|
75 |
|
12804
3be92cef1917
Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents:
12802
diff
changeset
|
76 def convert_path (pathname): |
| 14949 | 77 """Return 'pathname' as a name that will work on the native filesystem, |
| 78 i.e. split it on '/' and put it back together again using the current | |
| 79 directory separator. Needed because filenames in the setup script are | |
| 80 always supplied in Unix style, and have to be converted to the local | |
| 81 convention before we can actually use them in the filesystem. Raises | |
| 14950 | 82 ValueError on non-Unix-ish systems if 'pathname' either starts or |
| 83 ends with a slash. | |
| 14949 | 84 """ |
|
14743
8db096b782f9
Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents:
14709
diff
changeset
|
85 if os.sep == '/': |
|
8db096b782f9
Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents:
14709
diff
changeset
|
86 return pathname |
|
24787
0f61e074147f
SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents:
21267
diff
changeset
|
87 if not pathname: |
|
0f61e074147f
SF bug #574235, convert_path fails with empty pathname
Neal Norwitz <nnorwitz@gmail.com>
parents:
21267
diff
changeset
|
88 return pathname |
|
11934
7bd97c3dd432
Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents:
11916
diff
changeset
|
89 if pathname[0] == '/': |
|
12407
01cff9fe2d45
Cleaned up/simplified error-handling:
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
90 raise ValueError, "path '%s' cannot be absolute" % pathname |
|
11934
7bd97c3dd432
Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents:
11916
diff
changeset
|
91 if pathname[-1] == '/': |
|
12407
01cff9fe2d45
Cleaned up/simplified error-handling:
Greg Ward <gward@python.net>
parents:
12226
diff
changeset
|
92 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
|
93 |
|
8db096b782f9
Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents:
14709
diff
changeset
|
94 paths = string.split(pathname, '/') |
|
16321
a58c993d9234
Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents:
16261
diff
changeset
|
95 while '.' in paths: |
|
a58c993d9234
Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents:
16261
diff
changeset
|
96 paths.remove('.') |
|
a58c993d9234
Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents:
16261
diff
changeset
|
97 if not paths: |
|
a58c993d9234
Remove single "." components from pathnames, and return os.curdir if
Jack Jansen <jack.jansen@cwi.nl>
parents:
16261
diff
changeset
|
98 return os.curdir |
|
14743
8db096b782f9
Fix 'convert_path()' so it returns immediately under Unix -- prevents blowing
Greg Ward <gward@python.net>
parents:
14709
diff
changeset
|
99 return apply(os.path.join, paths) |
|
11934
7bd97c3dd432
Added 'native_path()' for use on pathnames from the setup script: split on
Greg Ward <gward@python.net>
parents:
11916
diff
changeset
|
100 |
|
12804
3be92cef1917
Renamed 'native_path()' to 'convert_path()'.
Greg Ward <gward@python.net>
parents:
12802
diff
changeset
|
101 # convert_path () |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
102 |
|
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
103 |
|
12507
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
104 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
|
105 """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
|
106 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
|
107 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
|
108 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
|
109 """ |
|
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
|
110 if os.name == 'posix': |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
111 if not os.path.isabs(pathname): |
|
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
112 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
|
113 else: |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
114 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
|
115 |
|
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
116 elif os.name == 'nt': |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
117 (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
|
118 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
|
119 path = path[1:] |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
120 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
|
121 |
|
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
122 elif os.name == 'mac': |
|
14709
e024438522dd
Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents:
14573
diff
changeset
|
123 if not os.path.isabs(pathname): |
|
e024438522dd
Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents:
14573
diff
changeset
|
124 return os.path.join(new_root, pathname) |
|
e024438522dd
Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents:
14573
diff
changeset
|
125 else: |
|
e024438522dd
Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents:
14573
diff
changeset
|
126 # Chop off volume name from start of path |
|
e024438522dd
Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents:
14573
diff
changeset
|
127 elements = string.split(pathname, ":", 1) |
|
e024438522dd
Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents:
14573
diff
changeset
|
128 pathname = ":" + elements[1] |
|
e024438522dd
Corran Webster: fix 'change_root()' to handle Mac OS paths.
Greg Ward <gward@python.net>
parents:
14573
diff
changeset
|
129 return os.path.join(new_root, pathname) |
|
12507
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
130 |
|
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
131 else: |
|
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
132 raise DistutilsPlatformError, \ |
|
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
133 "nothing known about platform '%s'" % os.name |
|
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
134 |
|
5661f4f9a47f
Added 'change_root()' to forcibly slap a new root directory onto a pathname,
Greg Ward <gward@python.net>
parents:
12482
diff
changeset
|
135 |
|
12672
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
136 _environ_checked = 0 |
|
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
137 def check_environ (): |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
138 """Ensure that 'os.environ' has all the environment variables we |
| 14949 | 139 guarantee that users can use in config files, command-line options, |
| 140 etc. Currently this includes: | |
| 141 HOME - user's home directory (Unix only) | |
| 142 PLAT - description of the current platform, including hardware | |
| 143 and OS (see 'get_platform()') | |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
144 """ |
|
12672
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
145 global _environ_checked |
|
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
146 if _environ_checked: |
|
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
147 return |
|
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
148 |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
149 if os.name == 'posix' and not os.environ.has_key('HOME'): |
|
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
150 import pwd |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
151 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
|
152 |
|
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
153 if not os.environ.has_key('PLAT'): |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
154 os.environ['PLAT'] = get_platform() |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
155 |
|
12672
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
156 _environ_checked = 1 |
|
47bdb636159c
Made 'check_environ()' "public" by stripping the leading underscore;
Gregory P. Smith <greg@mad-scientist.com>
parents:
12507
diff
changeset
|
157 |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
158 |
|
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
|
159 def subst_vars (s, local_vars): |
| 14949 | 160 """Perform shell/Perl-style variable substitution on 'string'. Every |
| 14950 | 161 occurrence of '$' followed by a name is considered a variable, and |
| 162 variable is substituted by the value found in the 'local_vars' | |
| 163 dictionary, or in 'os.environ' if it's not in 'local_vars'. | |
| 164 'os.environ' is first checked/augmented to guarantee that it contains | |
| 165 certain values: see 'check_environ()'. Raise ValueError for any | |
| 166 variables not found in either 'local_vars' or 'os.environ'. | |
| 14949 | 167 """ |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
168 check_environ() |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
169 def _subst (match, local_vars=local_vars): |
|
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
170 var_name = match.group(1) |
|
14860
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
171 if local_vars.has_key(var_name): |
|
18b3def6c4b8
Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents:
14828
diff
changeset
|
172 return str(local_vars[var_name]) |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
173 else: |
|
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
174 return os.environ[var_name] |
|
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
175 |
| 14950 | 176 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
|
177 return re.sub(r'\$([a-zA-Z_][a-zA-Z_0-9]*)', _subst, s) |
| 14950 | 178 except KeyError, var: |
| 179 raise ValueError, "invalid variable '$%s'" % var | |
|
12033
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
180 |
|
856dadf56cd9
Improved an error message in 'mkpath()'.
Greg Ward <gward@python.net>
parents:
12019
diff
changeset
|
181 # subst_vars () |
|
12102
e81b139ab8a4
Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents:
12044
diff
changeset
|
182 |
|
e81b139ab8a4
Added 'make_tarball()' and 'make_zipfile()' functions in preparation
Greg Ward <gward@python.net>
parents:
12044
diff
changeset
|
183 |
|
12935
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
184 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
|
185 """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
|
186 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
|
187 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
|
188 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
|
189 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
|
190 prefixed with 'prefix'. |
|
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
191 """ |
|
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
192 # 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
|
193 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
|
194 if exc.filename: |
|
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
195 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
|
196 else: |
|
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
197 # 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
|
198 # 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
|
199 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
|
200 else: |
|
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
201 error = prefix + str(exc[-1]) |
|
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
202 |
|
a98009a4391e
Added 'grok_environment_error()' function to deal with the various
Greg Ward <gward@python.net>
parents:
12804
diff
changeset
|
203 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
|
204 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
205 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
206 # Needed by 'split_quoted()' |
|
13891
08feb538c837
Fix so 'split_quoted()' handles any whitespace delimiter (not just space).
Greg Ward <gward@python.net>
parents:
13870
diff
changeset
|
207 _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace) |
|
13006
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
208 _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'") |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
209 _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"') |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
210 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
211 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
|
212 """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
|
213 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
|
214 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
|
215 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
|
216 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
|
217 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
|
218 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
|
219 words. |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
220 """ |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
221 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
222 # 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
|
223 # 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
|
224 # bit of a brain-bender to get it working right, though... |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
225 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
226 s = string.strip(s) |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
227 words = [] |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
228 pos = 0 |
|
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 while s: |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
231 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
|
232 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
|
233 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
|
234 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
|
235 break |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
236 |
|
13891
08feb538c837
Fix so 'split_quoted()' handles any whitespace delimiter (not just space).
Greg Ward <gward@python.net>
parents:
13870
diff
changeset
|
237 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
|
238 words.append(s[:end]) # we definitely have a word delimiter |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
239 s = string.lstrip(s[end:]) |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
240 pos = 0 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
241 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
242 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
|
243 # 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
|
244 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
|
245 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
|
246 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
247 else: |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
248 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
|
249 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
|
250 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
|
251 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
|
252 else: |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
253 raise RuntimeError, \ |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
254 "this can't happen (bad char '%c')" % s[end] |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
255 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
256 if m is None: |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
257 raise ValueError, \ |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
258 "bad string (mismatched %s quotes?)" % s[end] |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
259 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
260 (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
|
261 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
|
262 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
|
263 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
264 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
|
265 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
|
266 break |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
267 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
268 return words |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
269 |
|
87003842faec
Added 'split_quoted()' function to deal with strings that are quoted in
Greg Ward <gward@python.net>
parents:
12951
diff
changeset
|
270 # 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
|
271 |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
272 |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
273 def execute (func, args, msg=None, verbose=0, dry_run=0): |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
274 """Perform some action that affects the outside world (eg. by writing |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
275 to the filesystem). Such actions are special because they are disabled |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
276 by the 'dry_run' flag, and announce themselves if 'verbose' is true. |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
277 This method takes care of all that bureaucracy for you; all you have to |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
278 do is supply the function to call and an argument tuple for it (to |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
279 embody the "external action" being performed), and an optional message |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
280 to print. |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
281 """ |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
282 # Generate a message if we weren't passed one |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
283 if msg is None: |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
284 msg = "%s%s" % (func.__name__, `args`) |
| 21267 | 285 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
|
286 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
|
287 |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
288 # Print it if verbosity level is high enough |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
289 if verbose: |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
290 print msg |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
291 |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
292 # And do it, as long as we're not in dry-run mode |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
293 if not dry_run: |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
294 apply(func, args) |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
295 |
|
ba8db96ea4d9
Added the 'execute()' function (moved here from cmd.py with minor tweakage).
Greg Ward <gward@python.net>
parents:
13801
diff
changeset
|
296 # execute() |
|
14828
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
297 |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
298 |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
299 def strtobool (val): |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
300 """Convert a string representation of truth to true (1) or false (0). |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
301 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
|
302 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
|
303 'val' is anything else. |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
304 """ |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
305 val = string.lower(val) |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
306 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
|
307 return 1 |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
308 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
|
309 return 0 |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
310 else: |
|
9f96c92f03e3
Added 'strtobool()' function: convert strings like "yes", "1",
Greg Ward <gward@python.net>
parents:
14743
diff
changeset
|
311 raise ValueError, "invalid truth value %s" % `val` |
|
14951
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
312 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
313 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
314 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
|
315 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
|
316 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
|
317 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
|
318 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
|
319 """Byte-compile a collection of Python source files to either .pyc |
|
50386c73ba22
Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents:
14951
diff
changeset
|
320 or .pyo files in the same directory. 'py_files' is a list of files |
|
50386c73ba22
Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents:
14951
diff
changeset
|
321 to compile; any files that don't end in ".py" are silently skipped. |
|
50386c73ba22
Tweaked 'byte_compile()' so it silently skips non-Python files, rather than
Greg Ward <gward@python.net>
parents:
14951
diff
changeset
|
322 '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
|
323 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
|
324 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
|
325 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
|
326 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
|
327 timestamps. |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
328 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
329 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
|
330 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
|
331 '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
|
332 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
|
333 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
|
334 (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
|
335 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
336 If 'verbose' is true, prints out a report of each file. If 'dry_run' |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
337 is true, doesn't actually do anything that would affect the filesystem. |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
338 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
339 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
|
340 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
|
341 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
|
342 '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
|
343 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
|
344 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
|
345 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
|
346 """ |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
347 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
348 # 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
|
349 # 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
|
350 # 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
|
351 # 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
|
352 # 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
|
353 # 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
|
354 # 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
|
355 # 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
|
356 # 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
|
357 # the caller. |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
358 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
|
359 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
|
360 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
361 # "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
|
362 # 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
|
363 if not direct: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
364 from tempfile import mktemp |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
365 script_name = mktemp(".py") |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
366 if verbose: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
367 print "writing byte-compilation script '%s'" % script_name |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
368 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
|
369 script = open(script_name, "w") |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
370 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
371 script.write("""\ |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
372 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
|
373 files = [ |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
374 """) |
|
14982
0fa58fea30cf
Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents:
14960
diff
changeset
|
375 |
|
0fa58fea30cf
Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents:
14960
diff
changeset
|
376 # 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
|
377 # 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
|
378 # 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
|
379 # '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
|
380 # '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
|
381 # 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
|
382 # 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
|
383 # 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
|
384 # 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
|
385 |
|
0fa58fea30cf
Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents:
14960
diff
changeset
|
386 #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
|
387 #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
|
388 # 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
|
389 |
|
14951
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
390 script.write(string.join(map(repr, py_files), ",\n") + "]\n") |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
391 script.write(""" |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
392 byte_compile(files, optimize=%s, force=%s, |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
393 prefix=%s, base_dir=%s, |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
394 verbose=%s, dry_run=0, |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
395 direct=1) |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
396 """ % (`optimize`, `force`, `prefix`, `base_dir`, `verbose`)) |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
397 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
398 script.close() |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
399 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
400 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
|
401 if optimize == 1: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
402 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
|
403 elif optimize == 2: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
404 cmd.insert(1, "-OO") |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
405 spawn(cmd, verbose=verbose, 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
|
406 execute(os.remove, (script_name,), "removing %s" % script_name, |
|
0fa58fea30cf
Added a long-winded comment (and commented-out comment to go with out)
Greg Ward <gward@python.net>
parents:
14960
diff
changeset
|
407 verbose=verbose, dry_run=dry_run) |
| 21267 | 408 |
|
14951
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
409 # "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
|
410 # 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
|
411 # 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
|
412 # 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
|
413 else: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
414 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
|
415 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
416 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
|
417 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
|
418 # 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
|
419 # 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
|
420 continue |
|
14951
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
421 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
422 # 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
|
423 # 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
|
424 # dfile - purported source filename (same as 'file' by default) |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
425 cfile = file + (__debug__ and "c" or "o") |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
426 dfile = file |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
427 if prefix: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
428 if file[:len(prefix)] != prefix: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
429 raise ValueError, \ |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
430 ("invalid prefix: filename %s doesn't start with %s" |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
431 % (`file`, `prefix`)) |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
432 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
|
433 if base_dir: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
434 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
|
435 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
436 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
|
437 if direct: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
438 if force or newer(file, cfile): |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
439 if verbose: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
440 print "byte-compiling %s to %s" % (file, cfile_base) |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
441 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
|
442 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
|
443 else: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
444 if verbose: |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
445 print "skipping byte-compilation of %s to %s" % \ |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
446 (file, cfile_base) |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
447 |
|
005a0b4c382d
Added 'byte_compile(): an all-singing, all-dancing wrapper around the
Greg Ward <gward@python.net>
parents:
14950
diff
changeset
|
448 # byte_compile () |
|
17126
1b8745faa2ed
Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents:
16801
diff
changeset
|
449 |
|
1b8745faa2ed
Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents:
16801
diff
changeset
|
450 def rfc822_escape (header): |
|
1b8745faa2ed
Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents:
16801
diff
changeset
|
451 """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
|
452 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
|
453 """ |
|
17206
782239488058
Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents:
17126
diff
changeset
|
454 lines = string.split(header, '\n') |
|
782239488058
Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents:
17126
diff
changeset
|
455 lines = map(string.strip, lines) |
|
782239488058
Change rfc822_escape() to ensure there's a consistent amount of whitespace
Andrew M. Kuchling <amk@amk.ca>
parents:
17126
diff
changeset
|
456 header = string.join(lines, '\n' + 8*' ') |
|
17126
1b8745faa2ed
Patch #407434: add rfc822_escape utility function
Andrew M. Kuchling <amk@amk.ca>
parents:
16801
diff
changeset
|
457 return header |
