def testFeeds(self): self.cache_iface(foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="0" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <feed src='http://bar'/> </interface>""" % foo_iface_uri) self.cache_iface('http://bar', """<?xml version="1.0" ?> <interface last-modified="0" uri="http://bar" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <feed-for interface='%s'/> <name>Bar</name> <summary>Bar</summary> <description>Bar</description> <implementation version='1.0' id='sha1=123' main='dummy'> <archive href='foo' size='10'/> </implementation> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config = self.config) policy.freshness = 0 policy.network_use = model.network_full recalculate(policy) assert policy.ready foo_iface = self.config.iface_cache.get_interface(foo_iface_uri) self.assertEquals('sha1=123', policy.implementation[foo_iface].id)
def testFeeds(self): self.cache_iface( foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="0" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <feed src='http://bar'/> </interface>""" % foo_iface_uri) self.cache_iface( 'http://bar', """<?xml version="1.0" ?> <interface last-modified="0" uri="http://bar" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <feed-for interface='%s'/> <name>Bar</name> <summary>Bar</summary> <description>Bar</description> <implementation version='1.0' id='sha1=123' main='dummy'> <archive href='foo' size='10'/> </implementation> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config=self.config) policy.freshness = 0 policy.network_use = model.network_full recalculate(policy) assert policy.ready foo_iface = self.config.iface_cache.get_interface(foo_iface_uri) self.assertEquals('sha1=123', policy.implementation[foo_iface].id)
def build(): # Get the chosen versions src_policy = Policy(interface.uri, src=True) src_policy.freshness = 0 src_policy.recalculate() if not src_policy.ready: raise Exception( _('Internal error: required source components not found!')) root_iface = iface_cache.iface_cache.get_interface(src_policy.root) impl = src_policy.implementation[root_iface] min_version = impl.metadata.get(XMLNS_0COMPILE + ' min-version', our_min_version) # Check the syntax is valid and the version is high enough if model.parse_version(min_version) < model.parse_version( our_min_version): min_version = our_min_version # Do the whole build-and-register-feed c = Command() c.run(("0launch", '--message', _('Download the 0compile tool, to compile the source code'), '--not-before=' + min_version, "http://0install.net/2006/interfaces/0compile.xml", 'gui', '--no-prompt', interface.uri), add_feed)
def setUp(self): www.HTTP_ROOT = '<HTTP_ROOT>' www.REPO_ROOT = '<REPO_ROOT>' for name, path in _ENVS.items(): os.environ[name] = os.path.join(_ROOT, path) if os.path.exists(os.environ['XDG_CACHE_HOME']): support.ro_rmtree(os.environ['XDG_CACHE_HOME']) shutil.rmtree(_ROOT, ignore_errors=True) for name, path in _ENVS.items(): os.makedirs(os.environ[name], mode=0700) reload(basedir) assert basedir.xdg_config_home == os.environ['XDG_CONFIG_HOME'] iface_cache.iface_cache.__init__() download._downloads = {} logger = logging.getLogger() for i in logger.handlers: logger.removeHandler(i) logging.basicConfig(filename=os.path.join(_ROOT, 'debug.log'), level=logging.DEBUG) self.logger = logging.getLogger('test') policy = Policy('') policy.network_use = model.network_full policy.freshness = 60 policy.save_config() self._child = None
def testNoNeedDl(self): policy = Policy(foo_iface_uri, config = self.config) policy.freshness = 0 assert policy.need_download() policy = Policy(os.path.abspath('Foo.xml'), config = self.config) assert not policy.need_download() assert policy.ready
def testNoNeedDl(self): policy = Policy(foo_iface_uri, config=self.config) policy.freshness = 0 assert policy.need_download() policy = Policy(os.path.abspath('Foo.xml'), config=self.config) assert not policy.need_download() assert policy.ready
def testBackground(self, verbose=False): p = Policy('http://example.com:8000/Hello.xml', config=self.config) self.import_feed(p.root, 'Hello.xml') p.freshness = 0 p.network_use = model.network_minimal p.solver.solve(p.root, arch.get_host_architecture()) assert p.ready, p.solver.get_failure_reason() @tasks. async def choose_download(registed_cb, nid, actions): try: assert actions == ['download', 'Download'], actions registed_cb(nid, 'download') except: import traceback traceback.print_exc() yield None global ran_gui ran_gui = False old_out = sys.stdout try: sys.stdout = StringIO() self.child = server.handle_requests('Hello.xml', '6FCF121BE2390E0B.gpg') my_dbus.system_services = { "org.freedesktop.NetworkManager": { "/org/freedesktop/NetworkManager": NetworkManager() } } my_dbus.user_callback = choose_download pid = os.getpid() old_exit = os._exit def my_exit(code): # The background handler runs in the same process # as the tests, so don't let it abort. if os.getpid() == pid: raise SystemExit(code) # But, child download processes are OK old_exit(code) from zeroinstall.injector import config key_info = config.DEFAULT_KEY_LOOKUP_SERVER config.DEFAULT_KEY_LOOKUP_SERVER = None try: try: os._exit = my_exit background.spawn_background_update(p, verbose) assert False except SystemExit as ex: self.assertEquals(1, ex.code) finally: os._exit = old_exit config.DEFAULT_KEY_LOOKUP_SERVER = key_info finally: sys.stdout = old_out assert ran_gui
def testConstraints(self): self.cache_iface('http://bar', """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="http://bar" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Bar</name> <summary>Bar</summary> <description>Bar</description> <implementation id='sha1=100' version='1.0'> <archive href='foo' size='10'/> </implementation> <implementation id='sha1=150' stability='developer' version='1.5'> <archive href='foo' size='10'/> </implementation> <implementation id='sha1=200' version='2.0'> <archive href='foo' size='10'/> </implementation> </interface>""") self.cache_iface(foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <group main='dummy'> <requires interface='http://bar'> <version/> </requires> <implementation id='sha1=123' version='1.0'> <archive href='foo' size='10'/> </implementation> </group> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config = self.config) policy.network_use = model.network_full policy.freshness = 0 #logger.setLevel(logging.DEBUG) recalculate(policy) #logger.setLevel(logging.WARN) foo_iface = self.config.iface_cache.get_interface(foo_iface_uri) bar_iface = self.config.iface_cache.get_interface('http://bar') assert policy.implementation[bar_iface].id == 'sha1=200' dep = policy.implementation[foo_iface].dependencies['http://bar'] assert len(dep.restrictions) == 1 restriction = dep.restrictions[0] restriction.before = model.parse_version('2.0') recalculate(policy) assert policy.implementation[bar_iface].id == 'sha1=100' restriction.not_before = model.parse_version('1.5') recalculate(policy) assert policy.implementation[bar_iface].id == 'sha1=150'
def testConstraints(self): self.cache_iface( 'http://bar', """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="http://bar" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Bar</name> <summary>Bar</summary> <description>Bar</description> <implementation id='sha1=100' version='1.0'> <archive href='foo' size='10'/> </implementation> <implementation id='sha1=150' stability='developer' version='1.5'> <archive href='foo' size='10'/> </implementation> <implementation id='sha1=200' version='2.0'> <archive href='foo' size='10'/> </implementation> </interface>""") self.cache_iface( foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <group main='dummy'> <requires interface='http://bar'> <version/> </requires> <implementation id='sha1=123' version='1.0'> <archive href='foo' size='10'/> </implementation> </group> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config=self.config) policy.network_use = model.network_full policy.freshness = 0 #logger.setLevel(logging.DEBUG) recalculate(policy) #logger.setLevel(logging.WARN) foo_iface = self.config.iface_cache.get_interface(foo_iface_uri) bar_iface = self.config.iface_cache.get_interface('http://bar') assert policy.implementation[bar_iface].id == 'sha1=200' dep = policy.implementation[foo_iface].dependencies['http://bar'] assert len(dep.restrictions) == 1 restriction = dep.restrictions[0] restriction.before = model.parse_version('2.0') recalculate(policy) assert policy.implementation[bar_iface].id == 'sha1=100' restriction.not_before = model.parse_version('1.5') recalculate(policy) assert policy.implementation[bar_iface].id == 'sha1=150'
def testBackground(self, verbose=False): p = Policy("http://example.com:8000/Hello.xml", config=self.config) self.import_feed(p.root, "Hello.xml") p.freshness = 0 p.network_use = model.network_minimal p.solver.solve(p.root, arch.get_host_architecture()) assert p.ready, p.solver.get_failure_reason() @tasks.async def choose_download(registed_cb, nid, actions): try: assert actions == ["download", "Download"], actions registed_cb(nid, "download") except: import traceback traceback.print_exc() yield None global ran_gui ran_gui = False old_out = sys.stdout try: sys.stdout = StringIO() self.child = server.handle_requests("Hello.xml", "6FCF121BE2390E0B.gpg") my_dbus.system_services = { "org.freedesktop.NetworkManager": {"/org/freedesktop/NetworkManager": NetworkManager()} } my_dbus.user_callback = choose_download pid = os.getpid() old_exit = os._exit def my_exit(code): # The background handler runs in the same process # as the tests, so don't let it abort. if os.getpid() == pid: raise SystemExit(code) # But, child download processes are OK old_exit(code) key_info = fetch.DEFAULT_KEY_LOOKUP_SERVER fetch.DEFAULT_KEY_LOOKUP_SERVER = None try: try: os._exit = my_exit background.spawn_background_update(p, verbose) assert False except SystemExit, ex: self.assertEquals(1, ex.code) finally: os._exit = old_exit fetch.DEFAULT_KEY_LOOKUP_SERVER = key_info finally: sys.stdout = old_out assert ran_gui
def _check_for_updates(old_policy, verbose): from zeroinstall.injector.policy import Policy from zeroinstall.injector.config import load_config iface_cache = old_policy.config.iface_cache root_iface = iface_cache.get_interface(old_policy.root).get_name() background_config = load_config( BackgroundHandler(root_iface, old_policy.root)) policy = Policy(config=background_config, requirements=old_policy.requirements) info(_("Checking for updates to '%s' in a background process"), root_iface) if verbose: policy.handler.notify("Zero Install", _("Checking for updates to '%s'...") % root_iface, timeout=1) network_state = policy.handler.get_network_state() if network_state != _NetworkState.NM_STATE_CONNECTED: info( _("Not yet connected to network (status = %d). Sleeping for a bit..." ), network_state) import time time.sleep(120) if network_state in (_NetworkState.NM_STATE_DISCONNECTED, _NetworkState.NM_STATE_ASLEEP): info(_("Still not connected to network. Giving up.")) sys.exit(1) else: info(_("NetworkManager says we're on-line. Good!")) policy.freshness = 0 # Don't bother trying to refresh when getting the interface refresh = policy.refresh_all() # (causes confusing log messages) tasks.wait_for_blocker(refresh) # We could even download the archives here, but for now just # update the interfaces. if not policy.need_download(): if verbose: policy.handler.notify("Zero Install", _("No updates to download."), timeout=1) sys.exit(0) policy.handler.notify("Zero Install", _("Updates ready to download for '%s'.") % root_iface, timeout=1) _exec_gui(policy.root, '--refresh', '--systray') sys.exit(1)
def testNoArchives(self): self.cache_iface( foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <implementation id='sha1=123' version='1.0' main='dummy'/> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config=self.config) policy.freshness = 0 recalculate(policy) assert not policy.ready
def testNoArchives(self): self.cache_iface(foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <implementation id='sha1=123' version='1.0' main='dummy'/> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config = self.config) policy.freshness = 0 recalculate(policy) assert not policy.ready
def testCycle(self): self.cache_iface(foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <group> <requires interface='%s'/> <implementation id='sha1=123' version='1.0'> <archive href='foo' size='10'/> </implementation> </group> </interface>""" % (foo_iface_uri, foo_iface_uri)) policy = Policy(foo_iface_uri, config = self.config) policy.freshness = 0 recalculate(policy)
def testCycle(self): self.cache_iface( foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <group> <requires interface='%s'/> <implementation id='sha1=123' version='1.0'> <archive href='foo' size='10'/> </implementation> </group> </interface>""" % (foo_iface_uri, foo_iface_uri)) policy = Policy(foo_iface_uri, config=self.config) policy.freshness = 0 recalculate(policy)
def _check_for_updates(old_policy, verbose): from zeroinstall.injector.policy import Policy from zeroinstall.injector.config import load_config iface_cache = old_policy.config.iface_cache root_iface = iface_cache.get_interface(old_policy.root).get_name() background_config = load_config(BackgroundHandler(root_iface, old_policy.root)) policy = Policy(config = background_config, requirements = old_policy.requirements) info(_("Checking for updates to '%s' in a background process"), root_iface) if verbose: policy.handler.notify("Zero Install", _("Checking for updates to '%s'...") % root_iface, timeout = 1) network_state = policy.handler.get_network_state() if network_state != _NetworkState.NM_STATE_CONNECTED: info(_("Not yet connected to network (status = %d). Sleeping for a bit..."), network_state) import time time.sleep(120) if network_state in (_NetworkState.NM_STATE_DISCONNECTED, _NetworkState.NM_STATE_ASLEEP): info(_("Still not connected to network. Giving up.")) sys.exit(1) else: info(_("NetworkManager says we're on-line. Good!")) policy.freshness = 0 # Don't bother trying to refresh when getting the interface refresh = policy.refresh_all() # (causes confusing log messages) tasks.wait_for_blocker(refresh) # We could even download the archives here, but for now just # update the interfaces. if not policy.need_download(): if verbose: policy.handler.notify("Zero Install", _("No updates to download."), timeout = 1) sys.exit(0) policy.handler.notify("Zero Install", _("Updates ready to download for '%s'.") % root_iface, timeout = 1) _exec_gui(policy.root, '--refresh', '--systray') sys.exit(1)
def testNeedDL(self): self.cache_iface(foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="0" uri="%s" main='ThisBetterNotExist' xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <implementation version='1.0' id='sha1=123'> <archive href='http://foo/foo.tgz' size='100'/> </implementation> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config = self.config) policy.freshness = 0 policy.network_use = model.network_full recalculate(policy) assert policy.need_download() assert policy.ready
def testNeedDL(self): self.cache_iface( foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="0" uri="%s" main='ThisBetterNotExist' xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <implementation version='1.0' id='sha1=123'> <archive href='http://foo/foo.tgz' size='100'/> </implementation> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config=self.config) policy.freshness = 0 policy.network_use = model.network_full recalculate(policy) assert policy.need_download() assert policy.ready
def testUnknownAlg(self): self.cache_iface(foo_iface_uri, """<?xml version="1.0" ?> <interface uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <implementation main='.' id='unknown=123' version='1.0'> <archive href='http://foo/foo.tgz' size='100'/> </implementation> </interface>""" % foo_iface_uri) self.config.fetcher = fetch.Fetcher(self.config) policy = Policy(foo_iface_uri, config = self.config) policy.freshness = 0 try: assert policy.need_download() download_and_execute(policy, []) except model.SafeException as ex: assert 'Unknown digest algorithm' in str(ex)
def testUnknownAlg(self): self.cache_iface( foo_iface_uri, """<?xml version="1.0" ?> <interface uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <implementation main='.' id='unknown=123' version='1.0'> <archive href='http://foo/foo.tgz' size='100'/> </implementation> </interface>""" % foo_iface_uri) self.config.fetcher = fetch.Fetcher(self.config) policy = Policy(foo_iface_uri, config=self.config) policy.freshness = 0 try: assert policy.need_download() download_and_execute(policy, []) except model.SafeException as ex: assert 'Unknown digest algorithm' in str(ex)
def testDLfeed(self): self.cache_iface( foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <feed src='http://example.com'/> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config=self.config) policy.network_use = model.network_full policy.freshness = 0 assert policy.need_download() feed = self.config.iface_cache.get_feed(foo_iface_uri) feed.feeds = [model.Feed('/BadFeed', None, False)] logger.setLevel(logging.ERROR) assert policy.need_download() # Triggers warning logger.setLevel(logging.WARN)
def testDLfeed(self): self.cache_iface(foo_iface_uri, """<?xml version="1.0" ?> <interface last-modified="1110752708" uri="%s" xmlns="http://zero-install.sourceforge.net/2004/injector/interface"> <name>Foo</name> <summary>Foo</summary> <description>Foo</description> <feed src='http://example.com'/> </interface>""" % foo_iface_uri) policy = Policy(foo_iface_uri, config = self.config) policy.network_use = model.network_full policy.freshness = 0 assert policy.need_download() feed = self.config.iface_cache.get_feed(foo_iface_uri) feed.feeds = [model.Feed('/BadFeed', None, False)] logger.setLevel(logging.ERROR) assert policy.need_download() # Triggers warning logger.setLevel(logging.WARN)
def testSource(self): iface_cache = self.config.iface_cache warnings.filterwarnings("ignore", category=DeprecationWarning) foo = iface_cache.get_interface("http://foo/Binary.xml") self.import_feed(foo.uri, "Binary.xml") foo_src = iface_cache.get_interface("http://foo/Source.xml") self.import_feed(foo_src.uri, "Source.xml") compiler = iface_cache.get_interface("http://foo/Compiler.xml") self.import_feed(compiler.uri, "Compiler.xml") p = Policy("http://foo/Binary.xml", config=self.config) p.freshness = 0 p.network_use = model.network_full p.recalculate() # Deprecated assert p.implementation[foo].id == "sha1=123" # Now ask for source instead p.requirements.source = True p.requirements.command = "compile" p.recalculate() assert p.solver.ready, p.solver.get_failure_reason() assert p.implementation[foo].id == "sha1=234" # The source assert p.implementation[compiler].id == "sha1=345" # A binary needed to compile it
def _check_for_updates(old_policy, verbose): from zeroinstall.injector.policy import load_config, Policy iface_cache = old_policy.config.iface_cache root_iface = iface_cache.get_interface(old_policy.root).get_name() background_config = load_config(BackgroundHandler(root_iface, old_policy.root)) policy = Policy(config = background_config, requirements = old_policy.requirements) info(_("Checking for updates to '%s' in a background process"), root_iface) if verbose: policy.handler.notify("Zero Install", _("Checking for updates to '%s'...") % root_iface, timeout = 1) network_state = policy.handler.get_network_state() if network_state != _NetworkState.NM_STATE_CONNECTED: info(_("Not yet connected to network (status = %d). Sleeping for a bit..."), network_state) import time time.sleep(120) if network_state in (_NetworkState.NM_STATE_DISCONNECTED, _NetworkState.NM_STATE_ASLEEP): info(_("Still not connected to network. Giving up.")) sys.exit(1) else: info(_("NetworkManager says we're on-line. Good!")) policy.freshness = 0 # Don't bother trying to refresh when getting the interface refresh = policy.refresh_all() # (causes confusing log messages) policy.handler.wait_for_blocker(refresh) # We could even download the archives here, but for now just # update the interfaces. if not policy.need_download(): if verbose: policy.handler.notify("Zero Install", _("No updates to download."), timeout = 1) sys.exit(0) if not policy.handler.have_actions_support(): # Can't ask the user to choose, so just notify them # In particular, Ubuntu/Jaunty doesn't support actions policy.handler.notify("Zero Install", _("Updates ready to download for '%s'.") % root_iface, timeout = 1) _exec_gui(policy.root, '--refresh', '--systray') sys.exit(1) notification_closed = tasks.Blocker("wait for notification response") def _NotificationClosed(nid, *unused): if nid != our_question: return notification_closed.trigger() def _ActionInvoked(nid, action): if nid != our_question: return if action == 'download': _exec_gui(policy.root) notification_closed.trigger() policy.handler.notification_service.connect_to_signal('NotificationClosed', _NotificationClosed) policy.handler.notification_service.connect_to_signal('ActionInvoked', _ActionInvoked) our_question = policy.handler.notify("Zero Install", _("Updates ready to download for '%s'.") % root_iface, actions = ['download', 'Download']) policy.handler.wait_for_blocker(notification_closed)