3
我想調用Flatpak的new Development DBus service to spawn a process on the host,而不是在沙箱中。通過來自Python的dbus函數調用傳遞文件描述符(也稱爲flatpak的HostCommand)
要調用的DBus服務,我想出了下面的一段代碼:
#!/usr/bin/env python
import logging
import os
import sys
import dbus
def call_on_host(cmd):
"Calls Flatpak via DBus to spawn a process"
name = "org.freedesktop.Flatpak"
path = "/org/freedesktop/Flatpak/Development"
bus = dbus.SessionBus()
proxy = bus.get_object(name, path)
iface = "org.freedesktop.Flatpak.Development"
fp_helper = dbus.Interface(proxy, iface)
wd = '/tmp/'
read_fd, write_fd = os.pipe()
fds = {0:dbus.types.UnixFd(read_fd)}
envs = {'FOO':'bar'}
flags = 1
# cwd, cmd, fds, env, flags = ('/', ['ls'], {0:dbus.types.UnixFd(open('/etc/passwd'))}, {'foo':'bar'}, 1)
logging.info("Executing %r %r %r %r %r", wd, cmd, fds, envs, flags)
ret = fp_helper.HostCommand(wd, cmd, fds, envs, flags)
return ret
logging.basicConfig(level=logging.DEBUG)
print (call_on_host(sys.argv[1:]))
然而,這不工作這麼好。 Flatpak DBus幫助程序不會收到任何值,即它們都是NULL。
$ python execute_on_host.py 'ls'/
INFO:root:Executing '/tmp/' ['ls'] {0: <dbus.UnixFd object at 0x7f4b5ae6c120>} {'FOO': 'bar'} 1
Traceback (most recent call last):
File "execute_on_host.py", line 42, in <module>
print (call_on_host(sys.argv[1:]))
File "execute_on_host.py", line 35, in call_on_host
ret = fp_helper.HostCommand(wd, cmd, fds, envs, flags)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 70, in __call__
return self._proxy_method(*args, **keywords)
File "/usr/lib/python2.7/dist-packages/dbus/proxies.py", line 145, in __call__
**keywords)
File "/usr/lib/python2.7/dist-packages/dbus/connection.py", line 651, in call_blocking
message, timeout)
dbus.exceptions.DBusException: org.freedesktop.DBus.Error.InvalidArgs: No command 19430 given!!1 - *arg_argv[0] == 0
我現在有點困惑。在調用GVariants之前,是否需要包裝我的類型代理對象上的函數?
爲了測試我是否可以在所有的調用與簽名服務,我偷了大部分SUFF從this question並用以下想出了:
import unittest
import os
import sys
import subprocess
import time
import dbus
import dbus.service
import dbus.glib
import gobject
class MyDBUSService(dbus.service.Object):
def __init__(self):
bus_name = dbus.service.BusName('test.helloservice', bus = dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, '/test/helloservice')
def listen(self):
loop = gobject.MainLoop()
loop.run()
@dbus.service.method('test.helloservice', in_signature="ayaaya{uh}a{ss}u")
def hello(self, cwd, cmd, fds, env, flags):
print ([type(foo) for foo in (cwd, cmd, fds, env, flags)] )
print ("cwd: %s" % cwd)
print ("cmd: %s" % cmd)
print ("fsd: %s" % fds)
r = os.fdopen(fds[0].take()).read()
return r
class BaseTestCase(unittest.TestCase):
def setUp(self):
env = os.environ.copy()
self.p = subprocess.Popen(['python', __file__, 'server'], env=env)
# Wait for the service to become available
time.sleep(1)
assert self.p.stdout == None
assert self.p.stderr == None
open("/tmp/dbus-test", "w").write("Hello, World!")
def testHelloService(self):
bus = dbus.SessionBus()
helloservice = bus.get_object('test.helloservice', '/test/helloservice')
hello = helloservice.get_dbus_method('hello', 'test.helloservice')
cwd, cmd, fds, env, flags = ('/', ['ls'], {0:dbus.types.UnixFd(open('/tmp/dbus-test'))}, {'foo':'bar'}, 1)
r = hello(cwd, cmd, fds, env, flags)
assert r == "Hello, World!"
def tearDown(self):
# terminate() not supported in Python 2.5
#self.p.terminate()
os.kill(self.p.pid, 15)
if __name__ == '__main__':
arg = ""
if len(sys.argv) > 1:
arg = sys.argv[1]
if arg == "server":
myservice = MyDBUSService()
myservice.listen()
else:
unittest.main()
這很好地工作。
所以我想知道:我怎樣才能從Python調用Flatpak開發服務?