~ubuntu-branches/ubuntu/vivid/sahara/vivid-proposed

« back to all changes in this revision

Viewing changes to sahara/utils/patches.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2014-09-24 16:34:46 UTC
  • Revision ID: package-import@ubuntu.com-20140924163446-8gu3zscu5e3n9lr2
Tags: upstream-2014.2~b3
ImportĀ upstreamĀ versionĀ 2014.2~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2013 Mirantis Inc.
 
2
#
 
3
# Licensed under the Apache License, Version 2.0 (the "License");
 
4
# you may not use this file except in compliance with the License.
 
5
# You may obtain a copy of the License at
 
6
#
 
7
#    http://www.apache.org/licenses/LICENSE-2.0
 
8
#
 
9
# Unless required by applicable law or agreed to in writing, software
 
10
# distributed under the License is distributed on an "AS IS" BASIS,
 
11
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
12
# implied.
 
13
# See the License for the specific language governing permissions and
 
14
# limitations under the License.
 
15
 
 
16
import eventlet
 
17
 
 
18
 
 
19
EVENTLET_MONKEY_PATCH_MODULES = dict(os=True,
 
20
                                     select=True,
 
21
                                     socket=True,
 
22
                                     thread=True,
 
23
                                     time=True)
 
24
 
 
25
 
 
26
def patch_all():
 
27
    """Apply all patches.
 
28
 
 
29
    List of patches:
 
30
 
 
31
    * eventlet's monkey patch for all cases;
 
32
    * minidom's writexml patch for py < 2.7.3 only.
 
33
    """
 
34
    eventlet_monkey_patch()
 
35
    patch_minidom_writexml()
 
36
 
 
37
 
 
38
def eventlet_monkey_patch():
 
39
    """Apply eventlet's monkey patch.
 
40
 
 
41
    This call should be the first call in application. It's safe to call
 
42
    monkey_patch multiple times.
 
43
    """
 
44
    eventlet.monkey_patch(**EVENTLET_MONKEY_PATCH_MODULES)
 
45
 
 
46
 
 
47
def eventlet_import_monkey_patched(module):
 
48
    """Returns module monkey patched by eventlet.
 
49
 
 
50
    It's needed for some tests, for example, context test.
 
51
    """
 
52
    return eventlet.import_patched(module, **EVENTLET_MONKEY_PATCH_MODULES)
 
53
 
 
54
 
 
55
def patch_minidom_writexml():
 
56
    """Patch for xml.dom.minidom toprettyxml bug with whitespaces around text
 
57
 
 
58
    We apply the patch to avoid excess whitespaces in generated xml
 
59
    configuration files that brakes Hadoop.
 
60
 
 
61
    (This patch will be applied for all Python versions < 2.7.3)
 
62
 
 
63
    Issue: http://bugs.python.org/issue4147
 
64
    Patch: http://hg.python.org/cpython/rev/cb6614e3438b/
 
65
    Description: http://ronrothman.com/public/leftbraned/xml-dom-minidom-\
 
66
                        toprettyxml-and-silly-whitespace/#best-solution
 
67
    """
 
68
 
 
69
    import sys
 
70
    if sys.version_info >= (2, 7, 3):
 
71
        return
 
72
 
 
73
    import xml.dom.minidom as md
 
74
 
 
75
    def element_writexml(self, writer, indent="", addindent="", newl=""):
 
76
        # indent = current indentation
 
77
        # addindent = indentation to add to higher levels
 
78
        # newl = newline string
 
79
        writer.write(indent + "<" + self.tagName)
 
80
 
 
81
        attrs = self._get_attributes()
 
82
        a_names = attrs.keys()
 
83
        a_names.sort()
 
84
 
 
85
        for a_name in a_names:
 
86
            writer.write(" %s=\"" % a_name)
 
87
            md._write_data(writer, attrs[a_name].value)
 
88
            writer.write("\"")
 
89
        if self.childNodes:
 
90
            writer.write(">")
 
91
            if (len(self.childNodes) == 1
 
92
                    and self.childNodes[0].nodeType == md.Node.TEXT_NODE):
 
93
                self.childNodes[0].writexml(writer, '', '', '')
 
94
            else:
 
95
                writer.write(newl)
 
96
                for node in self.childNodes:
 
97
                    node.writexml(writer, indent + addindent, addindent, newl)
 
98
                writer.write(indent)
 
99
            writer.write("</%s>%s" % (self.tagName, newl))
 
100
        else:
 
101
            writer.write("/>%s" % (newl))
 
102
 
 
103
    md.Element.writexml = element_writexml
 
104
 
 
105
    def text_writexml(self, writer, indent="", addindent="", newl=""):
 
106
        md._write_data(writer, "%s%s%s" % (indent, self.data, newl))
 
107
 
 
108
    md.Text.writexml = text_writexml