如何使用 python 脚本安装包

How to install packages with python scripts

我想安装一个带有 python 脚本的包。我已阅读有关 PackageManager API (http://doc.aldebaran.com/2-4/naoqi/core/packagemanager-api.html) 的文档:

所以我已经按照 http://doc.aldebaran.com/2-4/naoqi/core/packagemanager.html 中的描述使用 choregraphe 打包了该应用程序,并且我尝试使用如下所示的 python 脚本安装它:

import qi
import sys


if __name__ == '__main__':
    ip = "11.1.11.111"
    port = 9559

    session = qi.Session()
    try:
        session.connect("tcp://" + ip + ":" + str(port))
    except RuntimeError:
        print ("Can't connect to Naoqi at ip \"" + ip + "\" on port " + str(port))
        sys.exit(1)

    service = session.service("PackageManager")

    package = "C:\test_package_handlers_01-835a92-1.0.0.pkg"

    # this is to see if the problem is that python can not locate the file
    with open(package) as f:
        print f

    service.install(package)

这是我收到的错误消息:

# provided package could be opened
<open file 'C:\test_package_handlers_01-835a92-1.0.0.pkg', mode 'r' at 0x02886288>
Traceback (most recent call last):
  File "C:/test.py", line 24, in <module>
    service.install(package)
RuntimeError: C:\test_package_handlers_01-835a92-1.0.0.pkg: no such file

我猜这是因为包必须上传到机器人上,包文件路径必须是机器人上的路径。

已编辑

我已经将包添加到编排空白项目中,并且 运行 机器人上的这个空白项目。通过这种方式,包被保存到路径为 /home/nao/.local/share/PackageManager/apps/.lastUploadedChoregrapheBehavior/test_package_handlers_01-835a92-1.0.0.pkg 的机器人中,并且当我更改脚本中的路径("C:\test_package_handlers_01-835a92-1.0.0.pkg""/home/nao/.local/share/PackageManager/apps/.lastUploadedChoregrapheBehavior/test_package_handlers_01-835a92-1.0.0.pkg")时,脚本按预期工作并且包是安装在机器人上。

那么有没有办法从我的电脑安装包而不上传它们到机器人,否则最好使用 Choregraphe 上传项目。

也许最好对我想要实现的目标给出以下解释:

EDITED_2

import qi
import ftplib
import os

ROBOT_URL = "10.80.129.90"

print "Uploading PKG"
pkg_file = "my-application-0.0.1.pkg"
pkg_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), pkg_file)

ftp = ftplib.FTP(ROBOT_URL)
ftp.login("nao", "nao")
with open(pkg_path) as pkg:
    ftp.storbinary("STOR "+pkg_file, pkg)

print "Connecting NAOqi session"
app = qi.Application(url='tcp://'+ROBOT_URL+':9559')
app.start()
session = app.session

print "Installing app"
packagemgr = session.service("PackageManager")
packagemgr.install("/home/nao/"+pkg_file)

print "Cleaning robot"
ftp.delete(pkg_file)
ftp.quit()

print "End"
app.stop()

这段代码ftp = ftplib.FTP(ROBOT_URL)抛出以下异常:

Traceback (most recent call last):
  File "C:/Stefan/DSK_PEPPER_clode_2/PythonScripts/_local_testing/uploading_and_installing_package.py", line 11, in <module>
    ftp = ftplib.FTP(ROBOT_URL)
  File "C:\Python27\lib\ftplib.py", line 120, in __init__
    self.connect(host)
  File "C:\Python27\lib\ftplib.py", line 135, in connect
    self.sock = socket.create_connection((self.host, self.port), self.timeout)
  File "C:\Python27\lib\socket.py", line 575, in create_connection
    raise err
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it

此外,当我使用用户名 'nao' 连接到机器人并按照 http://doc.aldebaran.com/2-5/dev/tools/opennao.html 中的说明传递 'nao',然后尝试使用 [= 在 /home/nao/.local/share/PackageManager/apps/ 中创建一个文件夹21=] 它告诉我:Sorry, user nao is not allowed to execute '/bin/mkdir dasdas' as root on Pepper.。如果我只使用 mkdir 这是它告诉我的:mkdir: cannot create directory 'new_folder': Permission denied

您之前确实需要上传文件。您可以使用 scpsftp 来执行此操作。一旦 .pkg 在机器人上,您就可以使用 PackageManager.install

想象一下:

import qi
import paramiko
import os

ROBOT_URL = "10.80.129.90"

print "Uploading PKG"
pkg_file = "my-application-0.0.1.pkg"
pkg_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), pkg_file)

transport = paramiko.Transport((ROBOT_URL, 22))
transport.connect(username="nao", password="nao")
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.put(pkg_path, pkg_file)

print "Connecting NAOqi session"
app = qi.Application(url='tcp://'+ROBOT_URL+':9559')
app.start()
session = app.session

print "Installing app"
packagemgr = session.service("PackageManager")
packagemgr.install("/home/nao/"+pkg_file)

print "Cleaning robot"
sftp.remove(pkg_file)
sftp.close()
transport.close()

print "End"
app.stop()

使用qibuild,也可以直接安装使用:

qipkg deploy-package /path/to/my-package.pkg --url nao@10.10.23.45