Browse Source

lint fixes

changes/49/376949/1
Corey Goldberg 9 years ago
parent
commit
8662a5f1fa
  1. 2
      xvfbwrapper/__init__.py
  2. 4
      xvfbwrapper/test_xvfb_selenium.py
  3. 29
      xvfbwrapper/xvfbwrapper.py

2
xvfbwrapper/__init__.py

@ -1,3 +1,3 @@
from xvfbwrapper import *
__version__ = '0.1.1'
__version__ = '0.1.2'

4
xvfbwrapper/test_xvfb_selenium.py

@ -7,9 +7,9 @@ import unittest
class TestHomepages(unittest.TestCase):
def setUp(self):
self.vdisplay = Xvfb(width=1280, height=720)
self.vdisplay = Xvfb(width=800, height=600)
self.vdisplay.start()
self.browser = webdriver.Firefox()

29
xvfbwrapper/xvfbwrapper.py

@ -1,14 +1,14 @@
#!/usr/bin/env python
#
# * Corey Goldberg, 2012
#
# - inspired by PyVirtualDisplay: http://pypi.python.org/pypi/PyVirtualDisplay
# - Corey Goldberg, 2012
# * inspired by:
# PyVirtualDisplay: http://pypi.python.org/pypi/PyVirtualDisplay
"""wrapper for running display inside X virtual framebuffer (Xvfb)"""
import os
import fnmatch
import random
@ -16,23 +16,22 @@ import subprocess
import time
class Xvfb(object):
def __init__(self, width=800, height=680, colordepth=24):
self.width = width
self.height = height
self.colordepth = colordepth
self.xvfb_proc = None
self.old_display_num = \
os.environ['DISPLAY'].split(':')[1] if 'DISPLAY' in os.environ else 0
os.environ['DISPLAY'].split(':')[1] \
if 'DISPLAY' in os.environ else 0
def start(self):
self.vdisplay_num = self.search_for_free_display()
self.xvfb_cmd = 'Xvfb :{0} -screen 0 {1}x{2}x{3} 2>&1 >{4}'.format(
self.vdisplay_num,
self.vdisplay_num,
self.width,
self.height,
self.colordepth,
@ -45,8 +44,7 @@ class Xvfb(object):
)
time.sleep(0.1) # give Xvfb time to start
self._redirect_display(self.vdisplay_num)
def stop(self):
self._redirect_display(self.old_display_num)
if self.xvfb_proc is not None:
@ -54,19 +52,17 @@ class Xvfb(object):
self.xvfb_proc.wait()
self.xvfb_proc = None
def search_for_free_display(self):
ls = map(lambda x:int(x.split('X')[1].split('-')[0]), self._lock_files())
ls = [int(x.split('X')[1].split('-')[0]) for x in self._lock_files()]
min_display_num = 1000
if len(ls):
display_num = max(min_display_num, max(ls) + 1)
else:
display_num = min_display_num
random.seed()
display_num += random.randint(0, 100)
display_num += random.randint(0, 100)
return display_num
def _lock_files(self):
tmpdir = '/tmp'
pattern = '.X*-lock'
@ -74,7 +70,6 @@ class Xvfb(object):
ls = [os.path.join(tmpdir, child) for child in names]
ls = [p for p in ls if os.path.isfile(p)]
return ls
def _redirect_display(self, display_num):
os.environ['DISPLAY'] = ':%s' % display_num

Loading…
Cancel
Save