2007-11-14 08:30:19 +00:00
|
|
|
# -*- coding: utf-8; mode: python; -*-
|
|
|
|
#
|
|
|
|
# GStreamer Development Utilities
|
|
|
|
#
|
|
|
|
# Copyright (C) 2007 René Stadler <mail@renestadler.de>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU General Public License as published by the Free
|
|
|
|
# Software Foundation; either version 3 of the License, or (at your option)
|
|
|
|
# any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
# more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
2007-11-22 08:47:06 +00:00
|
|
|
# this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2007-11-22 08:47:06 +00:00
|
|
|
"""GStreamer Development Utilities Common Main module."""
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
from operator import attrgetter
|
|
|
|
import logging
|
|
|
|
import locale
|
|
|
|
import gettext
|
|
|
|
from gettext import gettext as _, ngettext
|
|
|
|
|
2015-10-20 13:21:01 +00:00
|
|
|
import gi
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2018-04-14 13:22:11 +00:00
|
|
|
from gi.repository import GLib
|
2015-10-20 13:21:01 +00:00
|
|
|
from gi.repository import GObject
|
2016-09-28 18:38:55 +00:00
|
|
|
from gi.repository import Gtk
|
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
class ExceptionHandler (object):
|
|
|
|
|
|
|
|
exc_types = (Exception,)
|
|
|
|
priority = 50
|
|
|
|
inherit_fork = True
|
|
|
|
|
|
|
|
_handling_exception = False
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __call__(self, exc_type, exc_value, exc_traceback):
|
|
|
|
|
|
|
|
raise NotImplementedError(
|
|
|
|
"derived classes need to override this method")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DefaultExceptionHandler (ExceptionHandler):
|
2018-04-14 13:22:11 +00:00
|
|
|
exc_types = (BaseException,)
|
2007-11-14 08:30:19 +00:00
|
|
|
priority = 0
|
|
|
|
inherit_fork = True
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self, excepthook):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
ExceptionHandler.__init__(self)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
self.excepthook = excepthook
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __call__(self, *exc_info):
|
|
|
|
|
|
|
|
return self.excepthook(*exc_info)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ExitOnInterruptExceptionHandler (ExceptionHandler):
|
|
|
|
|
|
|
|
exc_types = (KeyboardInterrupt,)
|
|
|
|
priority = 100
|
|
|
|
inherit_fork = False
|
|
|
|
|
|
|
|
exit_status = 2
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __call__(self, *args):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2018-04-14 13:22:11 +00:00
|
|
|
print("Interrupt caught, exiting.", file=sys.stderr)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
sys.exit(self.exit_status)
|
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
class MainLoopWrapper (ExceptionHandler):
|
|
|
|
|
|
|
|
priority = 95
|
|
|
|
inherit_fork = False
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self, enter, exit):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
ExceptionHandler.__init__(self)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
self.exc_info = (None,) * 3
|
|
|
|
self.enter = enter
|
|
|
|
self.exit = exit
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __call__(self, *exc_info):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
self.exc_info = exc_info
|
2016-09-28 18:38:55 +00:00
|
|
|
self.exit()
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def run(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
ExceptHookManager.register_handler(self)
|
2007-11-14 08:30:19 +00:00
|
|
|
try:
|
2016-09-28 18:38:55 +00:00
|
|
|
self.enter()
|
2007-11-14 08:30:19 +00:00
|
|
|
finally:
|
2016-09-28 18:38:55 +00:00
|
|
|
ExceptHookManager.unregister_handler(self)
|
2009-03-14 21:02:45 +00:00
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
if self.exc_info != (None,) * 3:
|
|
|
|
# Re-raise unhandled exception that occured while running the loop.
|
|
|
|
exc_type, exc_value, exc_tb = self.exc_info
|
2018-04-19 17:26:23 +00:00
|
|
|
raise exc_value
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
class ExceptHookManagerClass (object):
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __init__(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
self._in_forked_child = False
|
|
|
|
|
|
|
|
self.handlers = []
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def setup(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
if sys.excepthook == self.__excepthook:
|
2016-09-28 18:38:55 +00:00
|
|
|
raise ValueError("already set up")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
hook = sys.excepthook
|
2016-09-28 18:38:55 +00:00
|
|
|
self.__instrument_excepthook()
|
|
|
|
self.__instrument_fork()
|
|
|
|
self.register_handler(DefaultExceptionHandler(hook))
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def shutdown(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
if sys.excepthook != self.__excepthook:
|
2016-09-28 18:38:55 +00:00
|
|
|
raise ValueError("not set up")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.__restore_excepthook()
|
|
|
|
self.__restore_fork()
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __instrument_excepthook(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
hook = sys.excepthook
|
|
|
|
self._original_excepthook = hook
|
|
|
|
sys.excepthook = self.__excepthook
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __restore_excepthook(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
sys.excepthook = self._original_excepthook
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __instrument_fork(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
fork = os.fork
|
|
|
|
except AttributeError:
|
|
|
|
# System has no fork() system call.
|
|
|
|
self._original_fork = None
|
|
|
|
else:
|
|
|
|
self._original_fork = fork
|
|
|
|
os.fork = self.__fork
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __restore_fork(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
if not hasattr(os, "fork"):
|
2007-11-14 08:30:19 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
os.fork = self._original_fork
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def entered_forked_child(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
self._in_forked_child = True
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
for handler in tuple(self.handlers):
|
2007-11-14 08:30:19 +00:00
|
|
|
if not handler.inherit_fork:
|
2016-09-28 18:38:55 +00:00
|
|
|
self.handlers.remove(handler)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def register_handler(self, handler):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
if self._in_forked_child and not handler.inherit_fork:
|
|
|
|
return
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.handlers.append(handler)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def unregister_handler(self, handler):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
self.handlers.remove(handler)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __fork(self):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
pid = self._original_fork()
|
2007-11-14 08:30:19 +00:00
|
|
|
if pid == 0:
|
|
|
|
# Child process.
|
2016-09-28 18:38:55 +00:00
|
|
|
self.entered_forked_child()
|
2007-11-14 08:30:19 +00:00
|
|
|
return pid
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def __excepthook(self, exc_type, exc_value, exc_traceback):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
for handler in sorted(self.handlers,
|
|
|
|
key=attrgetter("priority"),
|
|
|
|
reverse=True):
|
2009-03-14 21:02:45 +00:00
|
|
|
|
2007-11-14 08:30:19 +00:00
|
|
|
if handler._handling_exception:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for type_ in handler.exc_types:
|
2016-09-28 18:38:55 +00:00
|
|
|
if issubclass(exc_type, type_):
|
2007-11-14 08:30:19 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
handler._handling_exception = True
|
2016-09-28 18:38:55 +00:00
|
|
|
handler(exc_type, exc_value, exc_traceback)
|
2007-11-14 08:30:19 +00:00
|
|
|
# Not using try...finally on purpose here. If the handler itself
|
|
|
|
# fails with an exception, this prevents recursing into it again.
|
|
|
|
handler._handling_exception = False
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
from warnings import warn
|
2016-09-28 18:38:55 +00:00
|
|
|
warn("ExceptHookManager: unhandled %r" % (exc_value,),
|
|
|
|
RuntimeWarning,
|
|
|
|
stacklevel=2)
|
|
|
|
|
2018-04-14 15:04:22 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
ExceptHookManager = ExceptHookManagerClass()
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PathsBase (object):
|
|
|
|
|
|
|
|
data_dir = None
|
|
|
|
icon_dir = None
|
|
|
|
locale_dir = None
|
|
|
|
|
|
|
|
@classmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def setup_installed(cls, data_prefix):
|
2007-11-14 08:30:19 +00:00
|
|
|
"""Set up paths for running from a regular installation."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def setup_uninstalled(cls, source_dir):
|
2007-11-14 08:30:19 +00:00
|
|
|
"""Set up paths for running 'uninstalled' (i.e. directly from the
|
|
|
|
source dist)."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def ensure_setup(cls):
|
2007-11-14 08:30:19 +00:00
|
|
|
"""If paths are still not set up, try to set from a fallback."""
|
|
|
|
|
|
|
|
if cls.data_dir is None:
|
2016-09-28 18:38:55 +00:00
|
|
|
source_dir = os.path.dirname(
|
|
|
|
os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
cls.setup_uninstalled(source_dir)
|
|
|
|
|
|
|
|
def __new__(cls):
|
2009-03-14 21:02:45 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
raise RuntimeError("do not create instances of this class -- "
|
|
|
|
"use the class object directly")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PathsProgramBase (PathsBase):
|
|
|
|
|
|
|
|
program_name = None
|
|
|
|
|
|
|
|
@classmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def setup_installed(cls, data_prefix):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
if cls.program_name is None:
|
2016-09-28 18:38:55 +00:00
|
|
|
raise NotImplementedError(
|
|
|
|
"derived classes need to set program_name attribute")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2018-04-15 19:31:36 +00:00
|
|
|
cls.data_dir = os.path.join(data_prefix, cls.program_name)
|
|
|
|
cls.icon_dir = os.path.join(data_prefix, "icons")
|
|
|
|
cls.locale_dir = os.path.join(data_prefix, "locale")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
@classmethod
|
2016-09-28 18:38:55 +00:00
|
|
|
def setup_uninstalled(cls, source_dir):
|
2007-11-14 08:30:19 +00:00
|
|
|
"""Set up paths for running 'uninstalled' (i.e. directly from the
|
|
|
|
source dist)."""
|
|
|
|
|
|
|
|
# This is essential: The GUI module needs to find the .glade file.
|
2016-09-28 18:38:55 +00:00
|
|
|
cls.data_dir = os.path.join(source_dir, "data")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
# The locale data might be missing if "setup.py build" wasn't run.
|
2016-09-28 18:38:55 +00:00
|
|
|
cls.locale_dir = os.path.join(source_dir, "build", "mo")
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
# Not setting icon_dir. It is not useful since we don't employ the
|
|
|
|
# needed directory structure in the source dist.
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
|
|
|
|
def _init_excepthooks():
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
ExceptHookManager.setup()
|
|
|
|
ExceptHookManager.register_handler(ExitOnInterruptExceptionHandler())
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
def _init_paths(paths):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
paths.ensure_setup()
|
|
|
|
|
|
|
|
|
|
|
|
def _init_locale(gettext_domain=None):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
if Paths.locale_dir and gettext_domain is not None:
|
|
|
|
try:
|
2016-09-28 18:38:55 +00:00
|
|
|
locale.setlocale(locale.LC_ALL, "")
|
2011-11-16 18:45:16 +00:00
|
|
|
except locale.Error as exc:
|
2007-11-14 08:30:19 +00:00
|
|
|
from warnings import warn
|
2016-09-28 18:38:55 +00:00
|
|
|
warn("locale error: %s" % (exc,),
|
|
|
|
RuntimeWarning,
|
|
|
|
stacklevel=2)
|
2007-11-14 08:30:19 +00:00
|
|
|
Paths.locale_dir = None
|
|
|
|
else:
|
2016-09-28 18:38:55 +00:00
|
|
|
gettext.bindtextdomain(gettext_domain, Paths.locale_dir)
|
|
|
|
gettext.textdomain(gettext_domain)
|
|
|
|
gettext.bind_textdomain_codeset(gettext_domain, "UTF-8")
|
|
|
|
|
2018-04-14 15:04:22 +00:00
|
|
|
|
2018-04-14 13:22:11 +00:00
|
|
|
def _init_logging(level):
|
|
|
|
if level == "none":
|
|
|
|
return
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2018-04-14 15:04:22 +00:00
|
|
|
mapping = {"debug": logging.DEBUG,
|
|
|
|
"info": logging.INFO,
|
|
|
|
"warning": logging.WARNING,
|
|
|
|
"error": logging.ERROR,
|
|
|
|
"critical": logging.CRITICAL}
|
2018-04-14 13:22:11 +00:00
|
|
|
logging.basicConfig(level=mapping[level],
|
2016-09-28 18:38:55 +00:00
|
|
|
format='%(asctime)s.%(msecs)03d %(levelname)8s %(name)20s: %(message)s',
|
|
|
|
datefmt='%H:%M:%S')
|
|
|
|
|
|
|
|
logger = logging.getLogger("main")
|
|
|
|
logger.debug("logging at level %s", logging.getLevelName(level))
|
|
|
|
logger.info("using Python %i.%i.%i %s %i", *sys.version_info)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2018-04-14 15:04:22 +00:00
|
|
|
|
2018-04-14 13:22:11 +00:00
|
|
|
def _init_log_option(parser):
|
|
|
|
choices = ["none", "debug", "info", "warning", "error", "critical"]
|
|
|
|
parser.add_option("--log-level", "-l",
|
|
|
|
type="choice",
|
|
|
|
choices=choices,
|
|
|
|
action="store",
|
|
|
|
dest="log_level",
|
|
|
|
default="none",
|
|
|
|
help=_("Enable logging, possible values: ") + ", ".join(choices))
|
|
|
|
return parser
|
2007-11-14 08:30:19 +00:00
|
|
|
|
2018-04-14 15:04:22 +00:00
|
|
|
|
2018-04-14 13:22:11 +00:00
|
|
|
def main(main_function, option_parser, gettext_domain=None, paths=None):
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
# FIXME:
|
|
|
|
global Paths
|
|
|
|
Paths = paths
|
|
|
|
|
2016-09-28 18:38:55 +00:00
|
|
|
_init_excepthooks()
|
|
|
|
_init_paths(paths)
|
|
|
|
_init_locale(gettext_domain)
|
2018-04-14 13:22:11 +00:00
|
|
|
parser = _init_log_option(option_parser)
|
|
|
|
options, args = option_parser.parse_args()
|
|
|
|
_init_logging(options.log_level)
|
2007-11-14 08:30:19 +00:00
|
|
|
|
|
|
|
try:
|
2018-04-14 13:22:11 +00:00
|
|
|
main_function(args)
|
2007-11-14 08:30:19 +00:00
|
|
|
finally:
|
2016-09-28 18:38:55 +00:00
|
|
|
logging.shutdown()
|