1""" 2Check for updates in a background process. If we can start a program immediately, but some of our information 3is rather old (longer that the L{config.Config.freshness} threshold) then we run it anyway, and check for updates using a new 4process that runs quietly in the background. 5 6This avoids the need to annoy people with a 'checking for updates' box when they're trying to run things. 7""" 8 9# Copyright (C) 2009, Thomas Leonard 10# See the README file for details, or visit http://0install.net. 11 12fromzeroinstallimport_,logger 13importsys,os 14fromzeroinstall.supportimporttasks 15fromzeroinstall.injectorimporthandler 16
42"""@type title: str 43 @type root: str""" 44handler.Handler.__init__(self) 45self.title=title 46self.notification_service=None 47self.network_manager=None 48self.notification_service_caps=[] 49self.root=root# If we need to confirm any keys, run the GUI on this 50self.need_gui=False 51 52try: 53importdbus 54try: 55fromdbus.mainloop.glibimportDBusGMainLoop 56DBusGMainLoop(set_as_default=True) 57exceptImportError: 58importdbus.glib# Python 2 59exceptExceptionasex: 60logger.info(_("Failed to import D-BUS bindings: %s"),ex) 61return 62 63try: 64session_bus=dbus.SessionBus() 65remote_object=session_bus.get_object('org.freedesktop.Notifications', 66'/org/freedesktop/Notifications') 67 68self.notification_service=dbus.Interface(remote_object, 69'org.freedesktop.Notifications') 70 71# The Python bindings insist on printing a pointless introspection 72# warning to stderr if the service is missing. Force it to be done 73# now so we can skip it 74old_stderr=sys.stderr 75sys.stderr=None 76try: 77self.notification_service_caps=[str(s)forsin 78self.notification_service.GetCapabilities()] 79finally: 80sys.stderr=old_stderr 81exceptExceptionasex: 82logger.info(_("No D-BUS notification service available: %s"),ex) 83 84try: 85system_bus=dbus.SystemBus() 86remote_object=system_bus.get_object('org.freedesktop.NetworkManager', 87'/org/freedesktop/NetworkManager') 88 89self.network_manager=dbus.Interface(remote_object, 90'org.freedesktop.NetworkManager') 91exceptExceptionasex: 92logger.info(_("No D-BUS network manager service available: %s"),ex)
108"""Run the GUI if we need to confirm any keys.109 @type pending: L{zeroinstall.injector.iface_cache.PendingFeed}"""110111ifos.environ.get('DISPLAY',None):112logger.info(_("Can't update feed; signature not yet trusted. Running GUI..."))113114self.need_gui=True115116fordlinself.monitored_downloads:117dl.abort()118119raisehandler.NoTrustedKeys("need to switch to GUI to confirm keys")120else:121raisehandler.NoTrustedKeys(_("Background update for {iface} needed to confirm keys, but no GUI available!").format(122iface=self.root))
141"""Send a D-BUS notification message if possible. If there is no notification142 service available, log the message instead.143 @type title: str144 @type message: str145 @type timeout: int"""146ifnotself.notification_service:147logger.info('%s: %s',title,message)148returnNone149150LOW=0151NORMAL=1152#CRITICAL = 2153154importdbus.types155156hints={}157ifactions:158hints['urgency']=dbus.types.Byte(NORMAL)159else:160hints['urgency']=dbus.types.Byte(LOW)161162returnself.notification_service.Notify('Zero Install',1630,# replaces_id,164'',# icon165_escape_xml(title),166_escape_xml(message),167actions,168hints,169timeout*1000)
172"""Fork a detached grandchild.173 @return: True if we are the original."""174child=os.fork()175ifchild:176pid,status=os.waitpid(child,0)177assertpid==child178returnTrue179180# The calling process might be waiting for EOF from its child.181# Close our stdout so we don't keep it waiting.182# Note: this only fixes the most common case; it could be waiting183# on any other FD as well. We should really use gobject.spawn_async184# to close *all* FDs.185null=os.open(os.devnull,os.O_RDWR)186os.dup2(null,1)187os.close(null)188189grandchild=os.fork()190ifgrandchild:191os._exit(0)# Parent's waitpid returns and grandchild continues192193returnFalse
273"""Spawn a detached child process to check for updates.274 @param driver: driver containing interfaces to update275 @type driver: L{driver.Driver}276 @param verbose: whether to notify the user about minor events277 @type verbose: bool278 @since: 1.5 (used to take a Policy)"""279iface_cache=driver.config.iface_cache280# Mark all feeds as being updated. Do this before forking, so that if someone is281# running lots of 0launch commands in series on the same program we don't start282# huge numbers of processes.283foruriindriver.solver.feeds_used:284iface_cache.mark_as_checking(uri)285286spawn_background_update2(driver.requirements,verbose)
289"""Spawn a detached child process to check for updates.290 @param requirements: requirements for the new selections291 @type requirements: L{requirements.Requirements}292 @param verbose: whether to notify the user about minor events293 @type verbose: bool294 @param app: application to update (if any)295 @type app: L{apps.App} | None296 @since: 1.9"""297if_detach():298return299300try:301try:302_check_for_updates(requirements,verbose,app)303exceptSystemExit:304raise305except:306importtraceback307traceback.print_exc()308sys.stdout.flush()309finally:310os._exit(1)