Commit 794393f3 authored by Jason Madden's avatar Jason Madden

workaround psutil failures on travis.

parent f7dce8bf
...@@ -8,8 +8,8 @@ coveralls>=1.0 ...@@ -8,8 +8,8 @@ coveralls>=1.0
cffi cffi
futures futures
# Makes tests faster, but causes issues on the old # Makes tests faster, but causes issues on the old
# linux version Travis CI uses. # linux version Travis CI uses. We have a workaround.
# psutil psutil
# For viewing README.rst (restview --long-description), # For viewing README.rst (restview --long-description),
# CONTRIBUTING.rst, etc. # CONTRIBUTING.rst, etc.
# https://github.com/mgedmin/restview # https://github.com/mgedmin/restview
......
...@@ -633,69 +633,68 @@ def disabled_gc(): ...@@ -633,69 +633,68 @@ def disabled_gc():
gc.enable() gc.enable()
try: import re
import psutil # Linux/OS X/BSD platforms can implement this by calling out to lsof
except ImportError:
psutil = None def _run_lsof():
import re import tempfile
# Linux/OS X/BSD platforms can implement this by calling out to lsof pid = os.getpid()
fd, tmpname = tempfile.mkstemp('get_open_files')
def _run_lsof(): os.close(fd)
import tempfile lsof_command = 'lsof -p %s > %s' % (pid, tmpname)
pid = os.getpid() if os.system(lsof_command):
fd, tmpname = tempfile.mkstemp('get_open_files') raise OSError("lsof failed")
os.close(fd) with open(tmpname) as fobj:
lsof_command = 'lsof -p %s > %s' % (pid, tmpname) data = fobj.read().strip()
if os.system(lsof_command): os.remove(tmpname)
raise OSError("lsof failed") return data
with open(tmpname) as fobj:
data = fobj.read().strip() def get_open_files(pipes=False):
os.remove(tmpname) data = _run_lsof()
return data results = {}
for line in data.split('\n'):
def get_open_files(pipes=False): line = line.strip()
data = _run_lsof() if not line or line.startswith("COMMAND"):
results = {} # Skip header and blank lines
for line in data.split('\n'): continue
line = line.strip() split = re.split(r'\s+', line)
if not line or line.startswith("COMMAND"): command, pid, user, fd = split[:4]
# Skip header and blank lines # Pipes get an fd like "3" while normal files get an fd like "1u"
if fd[:-1].isdigit() or fd.isdigit():
if not pipes and not fd[-1].isdigit():
continue continue
split = re.split(r'\s+', line) print(fd)
command, pid, user, fd = split[:4] fd = int(fd[:-1]) if not fd[-1].isdigit() else int(fd)
# Pipes get an fd like "3" while normal files get an fd like "1u" if fd in results:
if fd[:-1].isdigit() or fd.isdigit(): params = (fd, line, split, results.get(fd), data)
if not pipes and not fd[-1].isdigit(): raise AssertionError('error when parsing lsof output: duplicate fd=%r\nline=%r\nsplit=%r\nprevious=%r\ndata:\n%s' % params)
continue results[fd] = line
print(fd) if not results:
fd = int(fd[:-1]) if not fd[-1].isdigit() else int(fd) raise AssertionError('failed to parse lsof:\n%s' % (data, ))
if fd in results: results['data'] = data
params = (fd, line, split, results.get(fd), data) return results
raise AssertionError('error when parsing lsof output: duplicate fd=%r\nline=%r\nsplit=%r\nprevious=%r\ndata:\n%s' % params)
results[fd] = line def get_number_open_files():
if not results: if os.path.exists('/proc/'):
raise AssertionError('failed to parse lsof:\n%s' % (data, )) # Linux only
results['data'] = data fd_directory = '/proc/%d/fd' % os.getpid()
return results return len(os.listdir(fd_directory))
else:
try:
return len(get_open_files(pipes=True)) - 1
except (OSError, AssertionError):
return 0
def get_number_open_files(): lsof_get_open_files = get_open_files
if os.path.exists('/proc/'):
# Linux only
fd_directory = '/proc/%d/fd' % os.getpid()
return len(os.listdir(fd_directory))
else:
try:
return len(get_open_files(pipes=True)) - 1
except (OSError, AssertionError):
return 0
else: try:
import psutil
except ImportError:
# If psutil is available (it is cross-platform) use that. # If psutil is available (it is cross-platform) use that.
# It is *much* faster than shelling out to lsof each time # It is *much* faster than shelling out to lsof each time
# (Running 14 tests takes 3.964s with lsof and 0.046 with psutil) # (Running 14 tests takes 3.964s with lsof and 0.046 with psutil)
# However, it still doesn't completely solve the issue on Windows: fds are reported # However, it still doesn't completely solve the issue on Windows: fds are reported
# as -1 there, so we can't fully check those. # as -1 there, so we can't fully check those.
# XXX: Note: installing psutil on the travis linux vm caused failures in test__makefile_refs.
def get_open_files(): def get_open_files():
results = dict() results = dict()
...@@ -714,6 +713,10 @@ else: ...@@ -714,6 +713,10 @@ else:
# num_fds is unix only. Is num_handles close enough on Windows? # num_fds is unix only. Is num_handles close enough on Windows?
return 0 return 0
if RUNNING_ON_TRAVIS:
# XXX: Note: installing psutil on the travis linux vm caused failures in test__makefile_refs.
get_open_files = lsof_get_open_files
if PYPY: if PYPY:
def getrefcount(*args): def getrefcount(*args):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment