# (c) Copyright 2010-2015. CodeWeavers, Inc.
import os
import subprocess
import sys
import traceback
OK = 0
MISSING = 1
BROKEN = 2
NO_DISPLAY = 3
def status():
import warnings
import cxlog
warnings.filterwarnings("ignore", "", Warning)
try:
try:
# pylint: disable=F0401,W0612
import gtk
except ImportError:
cxlog.log("Error importing gtk\npython executable: %s\nsite file: %s\nsys.path: %s\n%s" % (
sys.executable,
getattr(sys.modules.get('site'), '__file__', 'none'),
sys.path,
traceback.format_exc()))
return MISSING
except Exception: # pylint: disable=W0703
cxlog.log("Error importing gtk: %s\n" % traceback.format_exc())
traceback.print_exc()
return BROKEN
finally:
warnings.resetwarnings()
try:
# In theory init_check() return True if successful but in practice
# it returns None and raises a RuntimeException exception if there
# is a problem.
gtk.init_check()
except AttributeError:
cxlog.log("we cannot check that $DISPLAY is correct because gtk is too old")
except RuntimeError:
run_error = sys.exc_info()[1]
if str(run_error) == "could not open display":
cxlog.log("could not open display")
return NO_DISPLAY
traceback.print_exc()
cxlog.log("Error opening display: %s\n" % traceback.format_exc())
return BROKEN
except Exception: # pylint: disable=W0703
traceback.print_exc()
cxlog.log("Error opening display: %s\n" % traceback.format_exc())
return BROKEN
return OK
def ensure_present_gtk():
gtk_status = status()
if gtk_status == MISSING or gtk_status == BROKEN:
# GTK+ is missing or broken so try with another version of Python,
# hoping to have better luck.
for candidate in ('python', 'python2'):
try:
# Run this module with other Python versions to see if they
# pass the test.
popen = subprocess.Popen([candidate, __file__])
if popen.wait() in (OK, NO_DISPLAY):
# Note that it's better to issue a $DISPLAY error than
# send the user on a wild goose chase for a gtk module.
os.execvp(candidate, [candidate] + sys.argv)
except Exception: # pylint: disable=W0703
# Let the caller deal with the fallout
pass
return gtk_status
def check_gtk(warn_gtk=True, warn_display=True):
gtk_status = ensure_present_gtk()
if warn_gtk and (gtk_status == MISSING or gtk_status == BROKEN):
import cxfixes
cxfixes.add_error('gtk2')
cxfixes.fix_errors()
gtk_status = status()
if gtk_status == MISSING or gtk_status == BROKEN:
cxfixes.add_error('gtk2')
cxfixes.report_errors()
if warn_display and gtk_status == NO_DISPLAY:
import cxlog
cxlog.err("Verify $DISPLAY: it is not set, invalid or you are not allowed to access it")
return gtk_status
if __name__ == '__main__':
sys.exit(status())