Rearrange code to not import PyQt on every qvm-* call

Move notification functions to separate file (out of guihelpers).
This commit is contained in:
Marek Marczykowski-Górecki 2014-06-04 02:47:04 +02:00
parent eaac0024ca
commit 1ed9c74d83
8 changed files with 90 additions and 81 deletions

View File

@ -25,8 +25,6 @@ import sys
from optparse import OptionParser from optparse import OptionParser
from PyQt4.QtCore import * from PyQt4.QtCore import *
from PyQt4.QtGui import * from PyQt4.QtGui import *
import dbus
from dbus import DBusException
app = None app = None
system_bus = None system_bus = None
@ -58,28 +56,3 @@ def ask(text, title="Question", yestoall=False):
#?! #?!
return 127 return 127
def notify_error_qubes_manager(name, message):
global system_bus
if system_bus is None:
system_bus = dbus.SystemBus()
try:
qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
'/org/qubesos/QubesManager')
qubes_manager.notify_error(name, message, dbus_interface='org.qubesos.QubesManager')
except DBusException:
# ignore the case when no qubes-manager is running
pass
def clear_error_qubes_manager(name, message):
global system_bus
if system_bus is None:
system_bus = dbus.SystemBus()
try:
qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
'/org/qubesos/QubesManager')
qubes_manager.clear_error_exact(name, message, dbus_interface='org.qubesos.QubesManager')
except DBusException:
# ignore the case when no qubes-manager is running
pass

73
core/notify.py Normal file
View File

@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2014 Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
#
# 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 2
# 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
#
import dbus
system_bus = None
session_bus = None
notify_object = None
def tray_notify_init():
global notify_object
try:
notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
except dbus.DBusException as ex:
print >>sys.stderr, "WARNING: failed connect to tray notification service: %s" % str(ex)
def tray_notify(str, label, timeout = 3000):
if notify_object:
if label:
if not isinstance(label, str):
label = label.icon
notify_object.Notify("Qubes", 0, label, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def tray_notify_error(str, timeout = 3000):
if notify_object:
notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def notify_error_qubes_manager(name, message):
global system_bus
if system_bus is None:
system_bus = dbus.SystemBus()
try:
qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
'/org/qubesos/QubesManager')
qubes_manager.notify_error(name, message, dbus_interface='org.qubesos.QubesManager')
except dbus.DBusException:
# ignore the case when no qubes-manager is running
pass
def clear_error_qubes_manager(name, message):
global system_bus
if system_bus is None:
system_bus = dbus.SystemBus()
try:
qubes_manager = system_bus.get_object('org.qubesos.QubesManager',
'/org/qubesos/QubesManager')
qubes_manager.clear_error_exact(name, message, dbus_interface='org.qubesos.QubesManager')
except dbus.DBusException:
# ignore the case when no qubes-manager is running
pass

View File

@ -29,12 +29,8 @@ import sys
import os import os
import subprocess import subprocess
import re import re
import shutil
import time import time
import grp,pwd
import stat import stat
from datetime import datetime
from qmemman_client import QMemmanClient
import xen.lowlevel.xc import xen.lowlevel.xc
import xen.lowlevel.xl import xen.lowlevel.xl

View File

@ -20,7 +20,6 @@
# #
# #
import os import os
import dbus
import subprocess import subprocess
import sys import sys
import fcntl import fcntl
@ -32,10 +31,10 @@ from qubes.qubes import QubesException
from qubes.qubes import QubesDaemonPidfile from qubes.qubes import QubesDaemonPidfile
from qubes.qubes import QubesDispVmLabels from qubes.qubes import QubesDispVmLabels
from qubes.qmemman_client import QMemmanClient from qubes.qmemman_client import QMemmanClient
from qubes.notify import tray_notify,tray_notify_error,tray_notify_init
current_savefile = '/var/run/qubes/current-savefile' current_savefile = '/var/run/qubes/current-savefile'
current_dvm_conf = '/var/run/qubes/current-dvm.conf' current_dvm_conf = '/var/run/qubes/current-dvm.conf'
notify_object = None
class QfileDaemonDvm: class QfileDaemonDvm:
def __init__(self, name): def __init__(self, name):
@ -53,7 +52,7 @@ class QfileDaemonDvm:
subprocess.call(['/usr/bin/zenity', '--warning', '--text', errmsg]) subprocess.call(['/usr/bin/zenity', '--warning', '--text', errmsg])
return None return None
self.tray_notify("Starting new DispVM...") tray_notify("red", "Starting new DispVM...")
qvm_collection = QubesVmCollection() qvm_collection = QubesVmCollection()
qvm_collection.lock_db_for_writing() qvm_collection.lock_db_for_writing()
@ -124,22 +123,15 @@ class QfileDaemonDvm:
if dvm_mtime < root_mtime: if dvm_mtime < root_mtime:
template_name = os.path.basename(os.path.dirname(os.readlink(dvmdata_dir+'savefile-root'))) template_name = os.path.basename(os.path.dirname(os.readlink(dvmdata_dir+'savefile-root')))
if subprocess.call(["xl", "domid", template_name]) == 0: if subprocess.call(["xl", "domid", template_name]) == 0:
self.tray_notify("For optimum performance, you should not start DispVM when its template is running.") tray_notify("red", "For optimum performance, you should not "
"start DispVM when its template is running.")
return False return False
return True return True
def tray_notify(self, str, timeout = 3000):
if notify_object:
notify_object.Notify("Qubes", 0, "red", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def tray_notify_error(self, str, timeout = 3000):
if notify_object:
notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def get_dvm(self): def get_dvm(self):
if not self.dvm_setup_ok(): if not self.dvm_setup_ok():
if os.system("/usr/lib/qubes/qubes-update-dispvm-savefile-with-progress.sh >/dev/null </dev/null" ) != 0: if os.system("/usr/lib/qubes/qubes-update-dispvm-savefile-with-progress.sh >/dev/null </dev/null" ) != 0:
self.tray_notify_error("DVM savefile creation failed") tray_notify_error("DVM savefile creation failed")
return None return None
return self.do_get_dvm() return self.do_get_dvm()
@ -165,11 +157,7 @@ def main():
# sys.argv[5] - override firewall # sys.argv[5] - override firewall
print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time())) print >>sys.stderr, "time=%s, qfile-daemon-dvm init" % (str(time.time()))
try: tray_notify_init()
notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
except dbus.DBusException as e:
print >>sys.stderr, "Warning: notification service not available: %s" % str(e)
notify_object = None
print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time())) print >>sys.stderr, "time=%s, creating DispVM" % (str(time.time()))
qfile = QfileDaemonDvm(src_vmname) qfile = QfileDaemonDvm(src_vmname)
lockf = open("/var/run/qubes/qfile-daemon-dvm.lock", 'a') lockf = open("/var/run/qubes/qfile-daemon-dvm.lock", 'a')

View File

@ -27,7 +27,7 @@ import string
import time import time
import qmemman_algo import qmemman_algo
import os import os
from guihelpers import notify_error_qubes_manager, clear_error_qubes_manager from notify import notify_error_qubes_manager, clear_error_qubes_manager
no_progress_msg="VM refused to give back requested memory" no_progress_msg="VM refused to give back requested memory"
slow_memset_react_msg="VM didn't give back all requested memory" slow_memset_react_msg="VM didn't give back all requested memory"

View File

@ -24,31 +24,21 @@
from qubes.qubes import QubesVmCollection from qubes.qubes import QubesVmCollection
from qubes.qubes import QubesException from qubes.qubes import QubesException
from qubes.guihelpers import notify_error_qubes_manager from qubes.notify import notify_error_qubes_manager
from qubes.notify import tray_notify,tray_notify_error,tray_notify_init
from optparse import OptionParser from optparse import OptionParser
import subprocess import subprocess
import socket import socket
import errno import errno
import dbus
import time import time
import sys import sys
import os import os
import os.path import os.path
notify_object = None
# how long (in sec) to wait for VMs to shutdown # how long (in sec) to wait for VMs to shutdown
# before killing them (when used with --wait option) # before killing them (when used with --wait option)
from qubes.qubes import defaults from qubes.qubes import defaults
def tray_notify(str, label, timeout = 3000):
if notify_object:
notify_object.Notify("Qubes", 0, label.icon, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def tray_notify_error(str, timeout = 3000):
if notify_object:
notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def vm_run_cmd(vm, cmd, options): def vm_run_cmd(vm, cmd, options):
if options.shutdown: if options.shutdown:
if options.verbose: if options.verbose:
@ -206,11 +196,7 @@ def main():
cmdstr = args[1] if takes_cmd_argument else None cmdstr = args[1] if takes_cmd_argument else None
if options.tray: if options.tray:
global notify_object tray_notify_init()
try:
notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
except dbus.DBusException as ex:
print >>sys.stderr, "WARNING: failed connect to tray notification service: %s" % str(ex)
qvm_collection = QubesVmCollection() qvm_collection = QubesVmCollection()
qvm_collection.lock_db_for_reading() qvm_collection.lock_db_for_reading()

View File

@ -24,21 +24,13 @@
from qubes.qubes import QubesVmCollection from qubes.qubes import QubesVmCollection
from qubes.qubes import QubesException from qubes.qubes import QubesException
from optparse import OptionParser from optparse import OptionParser
from qubes.notify import tray_notify,tray_notify_error,tray_notify_init
import subprocess import subprocess
import os import os
import sys import sys
import dbus
notify_object = None notify_object = None
def tray_notify(str, label, timeout = 3000):
if notify_object:
notify_object.Notify("Qubes", 0, label.icon, "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def tray_notify_error(str, timeout = 3000):
if notify_object:
notify_object.Notify("Qubes", 0, "dialog-error", "Qubes", str, [], [], timeout, dbus_interface="org.freedesktop.Notifications")
def tray_notify_generic(level, str): def tray_notify_generic(level, str):
if level == "info": if level == "info":
tray_notify(str, label=vm.label) tray_notify(str, label=vm.label)
@ -74,11 +66,7 @@ def main():
vmname = args[0] vmname = args[0]
if options.tray: if options.tray:
global notify_object tray_notify_init()
try:
notify_object = dbus.SessionBus().get_object("org.freedesktop.Notifications", "/org/freedesktop/Notifications")
except dbus.DBusException as ex:
print >>sys.stderr, "WARNING: failed connect to tray notification service: %s" % str(ex)
qvm_collection = QubesVmCollection() qvm_collection = QubesVmCollection()
qvm_collection.lock_db_for_reading() qvm_collection.lock_db_for_reading()

View File

@ -111,6 +111,8 @@ cp core/qubesutils.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/qubesutils.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/qubesutils.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/guihelpers.py $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/guihelpers.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/guihelpers.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/guihelpers.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/notify.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/notify.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/backup.py $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/backup.py $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp core/backup.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes cp core/backup.py[co] $RPM_BUILD_ROOT%{python_sitearch}/qubes
cp qmemman/qmemman*py $RPM_BUILD_ROOT%{python_sitearch}/qubes cp qmemman/qmemman*py $RPM_BUILD_ROOT%{python_sitearch}/qubes
@ -275,6 +277,9 @@ fi
%{python_sitearch}/qubes/guihelpers.py %{python_sitearch}/qubes/guihelpers.py
%{python_sitearch}/qubes/guihelpers.pyc %{python_sitearch}/qubes/guihelpers.pyc
%{python_sitearch}/qubes/guihelpers.pyo %{python_sitearch}/qubes/guihelpers.pyo
%{python_sitearch}/qubes/notify.py
%{python_sitearch}/qubes/notify.pyc
%{python_sitearch}/qubes/notify.pyo
%{python_sitearch}/qubes/backup.py %{python_sitearch}/qubes/backup.py
%{python_sitearch}/qubes/backup.pyc %{python_sitearch}/qubes/backup.pyc
%{python_sitearch}/qubes/backup.pyo %{python_sitearch}/qubes/backup.pyo