Python example of using snapd-glib to authenticate, install and remove snaps

I’ve prepared a simple Python example that uses GObject Introspection to authenticate with snapd, install and remove a snap. This example only covers very basic use cases, and does not securely store the snapd authentication credentials at rest (which you absolutely should do) but hopefully the progress_snap_cb() function will be of use to @ikey and his Software Centre for Solus.

The snapd-glib documentation can be found here:

I’ve been using install2_sync per the snapd-glib comments:

        flags = snapd.InstallFlags.NONE # CLASSIC, DEVMODE, DANGEROUS, JAILMODE
        name = "ohmygiraffe"
        channel = None  # default channel
        revision = None  # default revision
        progress_callback = self.progress_cb
        self.snapd_client.install2_sync(flags, name, channel, revision, progress_callback, None, None)

As a note to other API consumers, once polkit support lands in stable snapd in distributions then the login step could be skipped and PolkicyKit would be used to authenticate operations, however private snaps will be unavailable without first logging in…

1 Like

Yes, I should have explained that this example is based on what we implemented in Software Boutique where install and remove operations are started in their own thread.