aboutsummaryrefslogtreecommitdiffstats
path: root/lib/python2.7/site-packages/buildbot_slave-0.8.8-py2.7.egg/buildslave/monkeypatches/bug4881.py
blob: aaa4bba376d95533dc36f5293860d642b673adba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# coding=utf-8
# This file is part of Buildbot.  Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import os
from twisted.internet import process
from twisted.python import log

def patch():
    log.msg("Applying patch for http://twistedmatrix.com/trac/ticket/4881")
    process._listOpenFDs = _listOpenFDs

#############################################################################
# Everything below this line was taken verbatim from Twisted, except as
# annotated.

########
# r31474:trunk/LICENSE

# Copyright (c) 2001-2010
# Allen Short
# Andy Gayton
# Andrew Bennetts
# Antoine Pitrou
# Apple Computer, Inc.
# Benjamin Bruheim
# Bob Ippolito
# Canonical Limited
# Christopher Armstrong
# David Reid
# Donovan Preston
# Eric Mangold
# Eyal Lotem
# Itamar Shtull-Trauring
# James Knight
# Jason A. Mobarak
# Jean-Paul Calderone
# Jessica McKellar
# Jonathan Jacobs
# Jonathan Lange
# Jonathan D. Simms
# Jürgen Hermann
# Kevin Horn
# Kevin Turner
# Mary Gardiner
# Matthew Lefkowitz
# Massachusetts Institute of Technology
# Moshe Zadka
# Paul Swartz
# Pavel Pergamenshchik
# Ralph Meijer
# Sean Riley
# Software Freedom Conservancy
# Travis B. Hartwell
# Thijs Triemstra
# Thomas Herve
# Timothy Allen
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
#     The above copyright notice and this permission notice shall be
#     included in all copies or substantial portions of the Software.
# 
#     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
#     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
#     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
#     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
#     LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#     OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
#     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

########
# r31474:trunk/twisted/internet/process.py

# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

class _FDDetector(object):
    """
    This class contains the logic necessary to decide which of the available
    system techniques should be used to detect the open file descriptors for
    the current process. The chosen technique gets monkey-patched into the
    _listOpenFDs method of this class so that the detection only needs to occur
    once.

    @ivars listdir: The implementation of listdir to use. This gets overwritten
        by the test cases.
    @ivars getpid: The implementation of getpid to use, returns the PID of the
        running process.
    @ivars openfile: The implementation of open() to use, by default the Python
        builtin.
    """
    # So that we can unit test this
    listdir = os.listdir
    getpid = os.getpid
    openfile = open


    def _listOpenFDs(self):
        """
        Figure out which implementation to use, then run it.
        """
        self._listOpenFDs = self._getImplementation()
        return self._listOpenFDs()


    def _getImplementation(self):
        """
        Check if /dev/fd works, if so, use that.  Otherwise, check if
        /proc/%d/fd exists, if so use that.
        
        Otherwise, ask resource.getrlimit, if that throws an exception, then
        fallback to _fallbackFDImplementation.
        """
        try:
            self.listdir("/dev/fd")
            if self._checkDevFDSanity(): # FreeBSD support :-)
                return self._devFDImplementation
            else:
                return self._fallbackFDImplementation
        except:
            try:
                self.listdir("/proc/%d/fd" % (self.getpid(),))
                return self._procFDImplementation
            except:
                try:
                    self._resourceFDImplementation() # Imports resource
                    return self._resourceFDImplementation
                except:
                    return self._fallbackFDImplementation


    def _checkDevFDSanity(self):
        """
        Returns true iff opening a file modifies the fds visible
        in /dev/fd, as it should on a sane platform.
        """
        start = self.listdir("/dev/fd")
        self.openfile("/dev/null", "r") # changed in Buildbot to hush pyflakes
        end = self.listdir("/dev/fd")
        return start != end


    def _devFDImplementation(self):
        """
        Simple implementation for systems where /dev/fd actually works.
        See: http://www.freebsd.org/cgi/man.cgi?fdescfs
        """
        dname = "/dev/fd"
        result = [int(fd) for fd in os.listdir(dname)]
        return result


    def _procFDImplementation(self):
        """
        Simple implementation for systems where /proc/pid/fd exists (we assume
        it works).
        """
        dname = "/proc/%d/fd" % (os.getpid(),)
        return [int(fd) for fd in os.listdir(dname)]


    def _resourceFDImplementation(self):
        """
        Fallback implementation where the resource module can inform us about
        how many FDs we can expect.

        Note that on OS-X we expect to be using the /dev/fd implementation.
        """
        import resource
        maxfds = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + 1
        # OS-X reports 9223372036854775808. That's a lot of fds
        # to close
        if maxfds > 1024:
            maxfds = 1024
        return xrange(maxfds)


    def _fallbackFDImplementation(self):
        """
        Fallback-fallback implementation where we just assume that we need to
        close 256 FDs.
        """
        maxfds = 256
        return xrange(maxfds)


detector = _FDDetector()

def _listOpenFDs():
    """
    Use the global detector object to figure out which FD implementation to
    use.
    """
    return detector._listOpenFDs()