Example #1
0
    def action_help(self, uri):
        p = policy.Policy(uri)
        policy.network_use = model.network_offline
        if p.need_download():
            child = subprocess.Popen(['0launch', '-d', '--', uri])
            child.wait()
            if child.returncode:
                return
        iface = self.iface_cache.get_interface(uri)
        reader.update_from_cache(iface)
        p.solve_with_downloads()
        impl = p.solver.selections[iface]
        assert impl, "Failed to choose an implementation of %s" % uri
        help_dir = impl.metadata.get('doc-dir')
        path = p.get_implementation_path(impl)
        assert path, "Chosen implementation is not cached!"
        if help_dir:
            path = os.path.join(path, help_dir)
        else:
            main = impl.main
            if main:
                # Hack for ROX applications. They should be updated to
                # set doc-dir.
                help_dir = os.path.join(path, os.path.dirname(main), 'Help')
                if os.path.isdir(help_dir):
                    path = help_dir

        # xdg-open has no "safe" mode, so check we're not "opening" an application.
        if os.path.exists(os.path.join(path, 'AppRun')):
            raise Exception(
                _("Documentation directory '%s' is an AppDir; refusing to open"
                  ) % path)

        subprocess.Popen(['xdg-open', path])
def ensure_cached(uri, command='run', config=None):
    """Ensure that an implementation of uri is cached.
	If not, it downloads one. It uses the GUI if a display is
	available, or the console otherwise.
	@param uri: the required interface
	@type uri: str
	@return: the selected implementations, or None if the user cancelled
	@rtype: L{zeroinstall.injector.selections.Selections}
	"""
    from zeroinstall.injector import policy

    if config is None:
        from zeroinstall.injector.config import load_config
        config = load_config()
    p = policy.Policy(uri, command=command, config=config)
    p.freshness = 0  # Don't check for updates

    if p.need_download() or not p.ready:
        if os.environ.get('DISPLAY', None):
            return get_selections_gui(uri, ['--command', command])
        else:
            done = p.solve_and_download_impls()
            tasks.wait_for_blocker(done)

    return p.solver.selections
    def testCommands(self):
        iface = os.path.join(mydir, "Command.xml")
        p = policy.Policy(iface, config=self.config)
        p.need_download()
        assert p.ready

        impl = p.solver.selections[self.config.iface_cache.get_interface(
            iface)]
        assert impl.id == 'c'
        assert impl.main == 'runnable/missing'

        dep_impl_uri = impl.commands['run'].requires[0].interface
        dep_impl = p.solver.selections[self.config.iface_cache.get_interface(
            dep_impl_uri)]
        assert dep_impl.id == 'sha1=256'

        s1 = selections.Selections(p)
        assert s1.commands[0].path == 'runnable/missing'
        xml = s1.toDOM().toxml("utf-8")
        root = qdom.parse(StringIO(xml))
        s2 = selections.Selections(root)

        assert s2.commands[0].path == 'runnable/missing'
        impl = s2.selections[iface]
        assert impl.id == 'c'

        assert s2.commands[0].qdom.attrs['http://custom attr'] == 'namespaced'
        custom_element = s2.commands[0].qdom.childNodes[0]
        assert custom_element.name == 'child'

        dep_impl = s2.selections[dep_impl_uri]
        assert dep_impl.id == 'sha1=256'
Example #4
0
	def testCommandBindings(self):
		p = policy.Policy(command_feed, config = self.config)
		self.config.handler.wait_for_blocker(p.solve_with_downloads())
		old_stdout = sys.stdout
		try:
			sys.stdout = StringIO()
			run.execute_selections(p.solver.selections, [], main = 'runner', dry_run = True, stores = self.config.stores)
		finally:
			sys.stdout = old_stdout
		assert 'local' in os.environ['LOCAL'], os.environ['LOCAL']
Example #5
0
	def testArgs(self):
		p = policy.Policy(runnable, config = self.config)
		self.config.handler.wait_for_blocker(p.solve_with_downloads())
		old_stdout = sys.stdout
		try:
			sys.stdout = StringIO()
			run.execute_selections(p.solver.selections, [], dry_run = True, stores = self.config.stores)
			out = sys.stdout.getvalue()
		finally:
			sys.stdout = old_stdout
		assert 'runner-arg' in out, out
Example #6
0
	def testAbsMain(self):
		p = policy.Policy(command_feed, config = self.config)
		self.config.handler.wait_for_blocker(p.solve_with_downloads())

		old_stdout = sys.stdout
		try:
			sys.stdout = StringIO()
			run.execute_selections(p.solver.selections, [], main = '/runnable/runner', dry_run = True, stores = self.config.stores)
		finally:
			sys.stdout = old_stdout

		try:
			old_stdout = sys.stdout
			try:
				sys.stdout = StringIO()
				run.execute_selections(p.solver.selections, [], main = '/runnable/not-there', dry_run = True, stores = self.config.stores)
			finally:
				sys.stdout = old_stdout
		except SafeException as ex:
			assert 'not-there' in unicode(ex)
    def testLocalPath(self):
        # 0launch --get-selections Local.xml
        iface = os.path.join(mydir, "Local.xml")
        p = policy.Policy(iface, config=self.config)
        p.need_download()
        assert p.ready
        s1 = selections.Selections(p)
        xml = s1.toDOM().toxml("utf-8")

        # Reload selections and check they're the same
        root = qdom.parse(StringIO(xml))
        s2 = selections.Selections(root)
        local_path = s2.selections[iface].local_path
        assert os.path.isdir(local_path), local_path
        assert not s2.selections[iface].digests, s2.selections[iface].digests

        # Add a newer implementation and try again
        feed = self.config.iface_cache.get_feed(iface)
        impl = model.ZeroInstallImplementation(feed,
                                               "foo bar=123",
                                               local_path=None)
        impl.version = model.parse_version('1.0')
        impl.commands["run"] = model.Command(
            qdom.Element(namespaces.XMLNS_IFACE, 'command', {'path': 'dummy'}),
            None)
        impl.add_download_source('http://localhost/bar.tgz', 1000, None)
        feed.implementations = {impl.id: impl}
        assert p.need_download()
        assert p.ready, p.solver.get_failure_reason()
        s1 = selections.Selections(p)
        xml = s1.toDOM().toxml("utf-8")
        root = qdom.parse(StringIO(xml))
        s2 = selections.Selections(root)
        xml = s2.toDOM().toxml("utf-8")
        qdom.parse(StringIO(xml))
        assert s2.selections[iface].local_path is None
        assert not s2.selections[iface].digests, s2.selections[iface].digests
        assert s2.selections[iface].id == 'foo bar=123'
    def testSelections(self):
        p = policy.Policy('http://foo/Source.xml',
                          src=True,
                          config=self.config)
        source = self.config.iface_cache.get_interface('http://foo/Source.xml')
        compiler = self.config.iface_cache.get_interface(
            'http://foo/Compiler.xml')
        self.import_feed(source.uri, 'Source.xml')
        self.import_feed(compiler.uri, 'Compiler.xml')

        p.freshness = 0
        p.network_use = model.network_full
        #import logging
        #logging.getLogger().setLevel(logging.DEBUG)
        assert p.need_download()

        def assertSel(s):
            self.assertEquals('http://foo/Source.xml', s.interface)
            self.assertEquals(2, len(s.selections))

            sels = [(sel.interface, sel) for sel in s.selections.values()]
            sels.sort()
            sels = [sel for uri, sel in sels]

            self.assertEquals('http://foo/Compiler.xml', sels[0].interface)
            self.assertEquals('http://foo/Source.xml', sels[1].interface)

            self.assertEquals("sha1=345", sels[0].id)
            self.assertEquals("1.0", sels[0].version)

            self.assertEquals('sha1=234', sels[1].id)
            self.assertEquals("1.0", sels[1].version)
            self.assertEquals("bar", sels[1].attrs['http://namespace foo'])
            self.assertEquals("1.0", sels[1].attrs['version'])
            assert 'version-modifier' not in sels[1].attrs

            self.assertEquals(0, len(sels[0].bindings))
            self.assertEquals(0, len(sels[0].dependencies))

            self.assertEquals(1, len(sels[1].bindings))
            self.assertEquals('.', sels[1].bindings[0].insert)

            self.assertEquals(1, len(sels[1].dependencies))
            dep = sels[1].dependencies[0]
            self.assertEquals('http://foo/Compiler.xml', dep.interface)
            self.assertEquals(3, len(dep.bindings))
            self.assertEquals('bin', dep.bindings[0].insert)
            self.assertEquals('PATH', dep.bindings[0].name)
            self.assertEquals('prepend', dep.bindings[0].mode)

            self.assertEquals('bin', dep.bindings[1].value)
            self.assertEquals('NO_PATH', dep.bindings[1].name)

            self.assertEquals('bin', dep.bindings[2].insert)
            self.assertEquals('BINDIR', dep.bindings[2].name)
            self.assertEquals('replace', dep.bindings[2].mode)

            self.assertEquals(["sha1=345"], sels[0].digests)

        s1 = selections.Selections(p)
        s1.selections['http://foo/Source.xml'].attrs[
            'http://namespace foo'] = 'bar'
        assertSel(s1)

        xml = s1.toDOM().toxml("utf-8")
        root = qdom.parse(StringIO(xml))
        self.assertEquals(namespaces.XMLNS_IFACE, root.uri)

        s2 = selections.Selections(root)
        assertSel(s2)