5e095d1cb43989869366e197cbda386858c880c3
[scons.git] / src / engine / SCons / compat / __init__.py
1 #
2 # __COPYRIGHT__
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining
5 # a copy of this software and associated documentation files (the
6 # "Software"), to deal in the Software without restriction, including
7 # without limitation the rights to use, copy, modify, merge, publish,
8 # distribute, sublicense, and/or sell copies of the Software, and to
9 # permit persons to whom the Software is furnished to do so, subject to
10 # the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included
13 # in all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
16 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
17 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #
23
24 __doc__ = """
25 SCons compatibility package for old Python versions
26
27 This subpackage holds modules that provide backwards-compatible
28 implementations of various things that we'd like to use in SCons but which
29 only show up in later versions of Python than the early, old version(s)
30 we still support.
31
32 This package will be imported by other code:
33
34     import SCons.compat
35
36 But other code will not generally reference things in this package through
37 the SCons.compat namespace.  The modules included here add things to
38 the __builtin__ namespace or the global module list so that the rest
39 of our code can use the objects and names imported here regardless of
40 Python version.
41
42 Simply enough, things that go in the __builtin__ name space come from
43 our builtins module.
44
45 The rest of the things here will be in individual compatibility modules
46 that are either: 1) suitably modified copies of the future modules that
47 we want to use; or 2) backwards compatible re-implementations of the
48 specific portions of a future module's API that we want to use.
49
50 GENERAL WARNINGS:  Implementations of functions in the SCons.compat
51 modules are *NOT* guaranteed to be fully compliant with these functions in
52 later versions of Python.  We are only concerned with adding functionality
53 that we actually use in SCons, so be wary if you lift this code for
54 other uses.  (That said, making these more nearly the same as later,
55 official versions is still a desirable goal, we just don't need to be
56 obsessive about it.)
57
58 We name the compatibility modules with an initial '_scons_' (for example,
59 _scons_subprocess.py is our compatibility module for subprocess) so
60 that we can still try to import the real module name and fall back to
61 our compatibility module if we get an ImportError.  The import_as()
62 function defined below loads the module as the "real" name (without the
63 '_scons'), after which all of the "import {module}" statements in the
64 rest of our code will find our pre-loaded compatibility module.
65 """
66
67 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
68
69 def import_as(module, name):
70     """
71     Imports the specified module (from our local directory) as the
72     specified name.
73     """
74     import imp
75     import os.path
76     dir = os.path.split(__file__)[0]
77     file, filename, suffix_mode_type = imp.find_module(module, [dir])
78     imp.load_module(name, file, filename, suffix_mode_type)
79
80 import builtins
81
82 try:
83     import hashlib
84 except ImportError:
85     # Pre-2.5 Python has no hashlib module.
86     try:
87         import_as('_scons_hashlib', 'hashlib')
88     except ImportError:
89         # If we failed importing our compatibility module, it probably
90         # means this version of Python has no md5 module.  Don't do
91         # anything and let the higher layer discover this fact, so it
92         # can fall back to using timestamp.
93         pass
94
95 try:
96     set
97 except NameError:
98     # Pre-2.4 Python has no native set type
99     try:
100         # Python 2.2 and 2.3 can use the copy of the 2.[45] sets module
101         # that we grabbed.
102         import_as('_scons_sets', 'sets')
103     except (ImportError, SyntaxError):
104         # Python 1.5 (ImportError, no __future_ module) and 2.1
105         # (SyntaxError, no generators in __future__) will blow up
106         # trying to import the 2.[45] sets module, so back off to a
107         # custom sets module that can be discarded easily when we
108         # stop supporting those versions.
109         import_as('_scons_sets15', 'sets')
110     import __builtin__
111     import sets
112     __builtin__.set = sets.Set
113
114 # If we need the compatibility version of textwrap, it  must be imported
115 # before optparse, which uses it.
116 try:
117     import textwrap
118 except ImportError:
119     # Pre-2.3 Python has no textwrap module.
120     import_as('_scons_textwrap', 'textwrap')
121
122 try:
123     import optparse
124 except ImportError:
125     # Pre-2.3 Python has no optparse module.
126     import_as('_scons_optparse', 'optparse')
127
128 import shlex
129 try:
130     shlex.split
131 except AttributeError:
132     # Pre-2.3 Python has no shlex.split function.
133     def split(s, comments=False):
134         import StringIO
135         lex = shlex.shlex(StringIO.StringIO(s))
136         lex.wordchars = lex.wordchars + '/\\-+,=:'
137         result = []
138         while True:
139             tt = lex.get_token()
140             if not tt:
141                 break
142             result.append(tt)
143         return result
144     shlex.split = split
145     del split
146
147 try:
148     import subprocess
149 except ImportError:
150     # Pre-2.4 Python has no subprocess module.
151     import_as('_scons_subprocess', 'subprocess')
152
153 try:
154     import UserString
155 except ImportError:
156     # Pre-1.6 Python has no UserString module.
157     import_as('_scons_UserString', 'UserString')