Package buildslave :: Package monkeypatches :: Module bug4881
[frames] | no frames]

Source Code for Module buildslave.monkeypatches.bug4881

  1  # coding=utf-8 
  2  # This file is part of Buildbot.  Buildbot is free software: you can 
  3  # redistribute it and/or modify it under the terms of the GNU General Public 
  4  # License as published by the Free Software Foundation, version 2. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, but WITHOUT 
  7  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  8  # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
  9  # details. 
 10  # 
 11  # You should have received a copy of the GNU General Public License along with 
 12  # this program; if not, write to the Free Software Foundation, Inc., 51 
 13  # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 14  # 
 15  # Copyright Buildbot Team Members 
 16   
 17  import os 
 18  from twisted.internet import process 
 19  from twisted.python import log 
 20   
21 -def patch():
22 log.msg("Applying patch for http://twistedmatrix.com/trac/ticket/4881") 23 process._listOpenFDs = _listOpenFDs
24 25 ############################################################################# 26 # Everything below this line was taken verbatim from Twisted, except as 27 # annotated. 28 29 ######## 30 # r31474:trunk/LICENSE 31 32 # Copyright (c) 2001-2010 33 # Allen Short 34 # Andy Gayton 35 # Andrew Bennetts 36 # Antoine Pitrou 37 # Apple Computer, Inc. 38 # Benjamin Bruheim 39 # Bob Ippolito 40 # Canonical Limited 41 # Christopher Armstrong 42 # David Reid 43 # Donovan Preston 44 # Eric Mangold 45 # Eyal Lotem 46 # Itamar Shtull-Trauring 47 # James Knight 48 # Jason A. Mobarak 49 # Jean-Paul Calderone 50 # Jessica McKellar 51 # Jonathan Jacobs 52 # Jonathan Lange 53 # Jonathan D. Simms 54 # Jürgen Hermann 55 # Kevin Horn 56 # Kevin Turner 57 # Mary Gardiner 58 # Matthew Lefkowitz 59 # Massachusetts Institute of Technology 60 # Moshe Zadka 61 # Paul Swartz 62 # Pavel Pergamenshchik 63 # Ralph Meijer 64 # Sean Riley 65 # Software Freedom Conservancy 66 # Travis B. Hartwell 67 # Thijs Triemstra 68 # Thomas Herve 69 # Timothy Allen 70 # 71 # Permission is hereby granted, free of charge, to any person obtaining 72 # a copy of this software and associated documentation files (the 73 # "Software"), to deal in the Software without restriction, including 74 # without limitation the rights to use, copy, modify, merge, publish, 75 # distribute, sublicense, and/or sell copies of the Software, and to 76 # permit persons to whom the Software is furnished to do so, subject to 77 # the following conditions: 78 # 79 # The above copyright notice and this permission notice shall be 80 # included in all copies or substantial portions of the Software. 81 # 82 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 83 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 84 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 85 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 86 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 87 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 88 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 89 90 ######## 91 # r31474:trunk/twisted/internet/process.py 92 93 # Copyright (c) Twisted Matrix Laboratories. 94 # See LICENSE for details. 95
96 -class _FDDetector(object):
97 """ 98 This class contains the logic necessary to decide which of the available 99 system techniques should be used to detect the open file descriptors for 100 the current process. The chosen technique gets monkey-patched into the 101 _listOpenFDs method of this class so that the detection only needs to occur 102 once. 103 104 @ivars listdir: The implementation of listdir to use. This gets overwritten 105 by the test cases. 106 @ivars getpid: The implementation of getpid to use, returns the PID of the 107 running process. 108 @ivars openfile: The implementation of open() to use, by default the Python 109 builtin. 110 """ 111 # So that we can unit test this 112 listdir = os.listdir 113 getpid = os.getpid 114 openfile = open 115 116
117 - def _listOpenFDs(self):
118 """ 119 Figure out which implementation to use, then run it. 120 """ 121 self._listOpenFDs = self._getImplementation() 122 return self._listOpenFDs()
123 124
125 - def _getImplementation(self):
126 """ 127 Check if /dev/fd works, if so, use that. Otherwise, check if 128 /proc/%d/fd exists, if so use that. 129 130 Otherwise, ask resource.getrlimit, if that throws an exception, then 131 fallback to _fallbackFDImplementation. 132 """ 133 try: 134 self.listdir("/dev/fd") 135 if self._checkDevFDSanity(): # FreeBSD support :-) 136 return self._devFDImplementation 137 else: 138 return self._fallbackFDImplementation 139 except: 140 try: 141 self.listdir("/proc/%d/fd" % (self.getpid(),)) 142 return self._procFDImplementation 143 except: 144 try: 145 self._resourceFDImplementation() # Imports resource 146 return self._resourceFDImplementation 147 except: 148 return self._fallbackFDImplementation
149 150
151 - def _checkDevFDSanity(self):
152 """ 153 Returns true iff opening a file modifies the fds visible 154 in /dev/fd, as it should on a sane platform. 155 """ 156 start = self.listdir("/dev/fd") 157 self.openfile("/dev/null", "r") # changed in Buildbot to hush pyflakes 158 end = self.listdir("/dev/fd") 159 return start != end
160 161
162 - def _devFDImplementation(self):
163 """ 164 Simple implementation for systems where /dev/fd actually works. 165 See: http://www.freebsd.org/cgi/man.cgi?fdescfs 166 """ 167 dname = "/dev/fd" 168 result = [int(fd) for fd in os.listdir(dname)] 169 return result
170 171
172 - def _procFDImplementation(self):
173 """ 174 Simple implementation for systems where /proc/pid/fd exists (we assume 175 it works). 176 """ 177 dname = "/proc/%d/fd" % (os.getpid(),) 178 return [int(fd) for fd in os.listdir(dname)]
179 180
182 """ 183 Fallback implementation where the resource module can inform us about 184 how many FDs we can expect. 185 186 Note that on OS-X we expect to be using the /dev/fd implementation. 187 """ 188 import resource 189 maxfds = resource.getrlimit(resource.RLIMIT_NOFILE)[1] + 1 190 # OS-X reports 9223372036854775808. That's a lot of fds 191 # to close 192 if maxfds > 1024: 193 maxfds = 1024 194 return xrange(maxfds)
195 196
198 """ 199 Fallback-fallback implementation where we just assume that we need to 200 close 256 FDs. 201 """ 202 maxfds = 256 203 return xrange(maxfds)
204 205 206 detector = _FDDetector() 207
208 -def _listOpenFDs():
209 """ 210 Use the global detector object to figure out which FD implementation to 211 use. 212 """ 213 return detector._listOpenFDs()
214