/usr/lib/python2.7/dist-packages/pyvirtualdisplay/abstractdisplay.py is in python-pyvirtualdisplay 0.2.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | from easyprocess import EasyProcess
import fnmatch
import logging
import os
import time
import tempfile
from threading import Lock
from pyvirtualdisplay import xauth
mutex = Lock()
log = logging.getLogger(__name__)
# TODO: not perfect
# randomize to avoid possible conflicts
RANDOMIZE_DISPLAY_NR = False
if RANDOMIZE_DISPLAY_NR:
import random
random.seed()
MIN_DISPLAY_NR = 1000
USED_DISPLAY_NR_LIST=[]
class AbstractDisplay(EasyProcess):
'''
Common parent for Xvfb and Xephyr
'''
def __init__(self, use_xauth=False):
mutex.acquire()
try:
self.display = self.search_for_display()
while self.display in USED_DISPLAY_NR_LIST:
self.display+=1
USED_DISPLAY_NR_LIST.append(self.display)
finally:
mutex.release()
if xauth and not xauth.is_installed():
raise xauth.NotFoundError()
self.use_xauth = use_xauth
self._old_xauth = None
self._xauth_filename = None
EasyProcess.__init__(self, self._cmd)
@property
def new_display_var(self):
return ':%s' % (self.display)
@property
def _cmd(self):
raise NotImplementedError()
def lock_files(self):
tmpdir = '/tmp'
pattern = '.X*-lock'
# ls = path('/tmp').files('.X*-lock')
# remove path.py dependency
names = fnmatch.filter(os.listdir(tmpdir), pattern)
ls = [os.path.join(tmpdir, child) for child in names]
ls = [p for p in ls if os.path.isfile(p)]
return ls
def search_for_display(self):
# search for free display
ls = map(
lambda x: int(x.split('X')[1].split('-')[0]), self.lock_files())
if len(ls):
display = max(MIN_DISPLAY_NR, max(ls) + 3)
else:
display = MIN_DISPLAY_NR
if RANDOMIZE_DISPLAY_NR:
display += random.randint(0, 100)
return display
def redirect_display(self, on):
'''
on:
* True -> set $DISPLAY to virtual screen
* False -> set $DISPLAY to original screen
:param on: bool
'''
d = self.new_display_var if on else self.old_display_var
if d is None:
log.debug('unset DISPLAY')
del os.environ['DISPLAY']
else:
log.debug('DISPLAY=%s', d)
os.environ['DISPLAY'] = d
def start(self):
'''
start display
:rtype: self
'''
if self.use_xauth:
self._setup_xauth()
EasyProcess.start(self)
# https://github.com/ponty/PyVirtualDisplay/issues/2
# https://github.com/ponty/PyVirtualDisplay/issues/14
self.old_display_var = os.environ.get('DISPLAY', None)
self.redirect_display(True)
# wait until X server is active
# TODO: better method
time.sleep(0.1)
return self
def stop(self):
'''
stop display
:rtype: self
'''
self.redirect_display(False)
EasyProcess.stop(self)
if self.use_xauth:
self._clear_xauth()
return self
def _setup_xauth(self):
'''
Set up the Xauthority file and the XAUTHORITY environment variable.
'''
handle, filename = tempfile.mkstemp(prefix='PyVirtualDisplay.',
suffix='.Xauthority')
self._xauth_filename = filename
os.close(handle)
# Save old environment
self._old_xauth = {}
self._old_xauth['AUTHFILE'] = os.getenv('AUTHFILE')
self._old_xauth['XAUTHORITY'] = os.getenv('XAUTHORITY')
os.environ['AUTHFILE'] = os.environ['XAUTHORITY'] = filename
cookie = xauth.generate_mcookie()
xauth.call('add', self.new_display_var, '.', cookie)
def _clear_xauth(self):
'''
Clear the Xauthority file and restore the environment variables.
'''
os.remove(self._xauth_filename)
for varname in ['AUTHFILE', 'XAUTHORITY']:
if self._old_xauth[varname] is None:
del os.environ[varname]
else:
os.environ[varname] = self._old_xauth[varname]
self._old_xauth = None
|