How to communicate with /run/snapd.socket using Python?

Hi!

After more than 2 years (!!!), I’ve revisited this topic and tried your method.

As long as I’m testing locally (i.e. without a snap), it works well \o/

In my snapcraft.yaml file, I added the two packages you mentioned in my program’s part:

  qabro:
    plugin: python
    source: .
    parse-info: [setup.py]
    ...
    stage-packages:
      ...
      - python3-gi
      - gir1.2-snapd-1
  ...

It seems both packages get pulled at build time:

$ snapcraft 
Launching a VM.
...
Get:1 gir1.2-snapd-1_1.49-0ubuntu0.18.04.2_amd64.deb [12.9 kB]                                                                                                                                                    
Fetched 12.9 kB in 0s (0 B/s)
...
Get:1 python3-gi_3.26.1-2ubuntu1_amd64.deb [153 kB]                                                                                                                                                               
Fetched 153 kB in 0s (0 B/s)
...
Snapping |                                                                                                                                                                                              
Snapped qabro_0.14dev_amd64.snap

But! my program fails to run.

If I investigate a bit:

$ snap run --shell qabro
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

pieq@cbrown:/home/pieq/dev/qabro$ python3
Python 3.6.9 (default, Jul 17 2020, 12:50:27) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> gi.require_version('Snapd', '1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/snap/qabro/x1/usr/lib/python3/dist-packages/gi/__init__.py", line 130, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Snapd not available

$ ls /usr/lib/x86_64-linux-gnu/girepository-1.0/Snapd-1.typelib
ls: cannot access '/usr/lib/x86_64-linux-gnu/girepository-1.0/Snapd-1.typelib': No such file or directory

According to the Ubuntu package, installing gir1.2-snapd-1 should create /usr/lib/x86_64-linux-gnu/girepository-1.0/Snapd-1.typelib.

Any idea what’s wrong here?

More info: