How to install packages with python scripts

1.6k views Asked by At

I want to install a package with a python script. I have read the documentation about PackageManager API (http://doc.aldebaran.com/2-4/naoqi/core/packagemanager-api.html):

So I have packaged the app with choregraphe as it is described in http://doc.aldebaran.com/2-4/naoqi/core/packagemanager.html and I have tried to install it with a python script that looks like:

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)

And here is what I receive as an error:

# 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

I guess this is because the package must be uploaded on the robot and the package file path must be the one that is on the robot.

EDITED

I have added the package to a choreographe blank project and run this blank project on the robot. This way the package was saved to the robot with path /home/nao/.local/share/PackageManager/apps/.lastUploadedChoregrapheBehavior/test_package_handlers_01-835a92-1.0.0.pkg and when I have changed the path in my script ("C:\\test_package_handlers_01-835a92-1.0.0.pkg" with "/home/nao/.local/share/PackageManager/apps/.lastUploadedChoregrapheBehavior/test_package_handlers_01-835a92-1.0.0.pkg") the script worked as it was intended and the package was installed on the robot.

So is there a way to install packages from my PC without uploading them to the robot, because otherwise it is better to use Choregraphe to upload projects.

Maybe it is good to give the following explanation of what I want to achieve:

  • I have a folder on my PC with 20 packages for example
  • I want to install all those 20 packages with one python script
  • There is a python script that installs all the packages from the folder when it is invoked like this: python package_installer.py path_to_packages_folder

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()

This piece of code ftp = ftplib.FTP(ROBOT_URL) throws the following exception:

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

Also when I connect to the robot with username 'nao' and pass 'nao' as described in http://doc.aldebaran.com/2-5/dev/tools/opennao.html and then try to create a folder in /home/nao/.local/share/PackageManager/apps/ with sudo mkdir it informs me that: Sorry, user nao is not allowed to execute '/bin/mkdir dasdas' as root on Pepper.. If I use only mkdir here is what it tells me: mkdir: cannot create directory 'new_folder': Permission denied

2

There are 2 answers

5
JLS On BEST ANSWER

You indeed need to upload the file before. You can use scp or sftp to do this. Once the .pkg is on the robot then you can use PackageManager.install.

Imagine something like:

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()
0
JLS On

Using qibuild, you can also directly install using:

qipkg deploy-package /path/to/my-package.pkg --url [email protected]