Skip to main content

Helpers for interacting with the Snap system within a Snap

Project description

Build Status Coverage Status Documentation Status

The library exposes a top-level snaphelpers.Snap object which provides access to:

  • snap details:

    >>> snap = snaphelpers.Snap()
    >>> snap.name
    'testapp'
    >>> snap.instance_name
    'testapp'
    >>> snap.version
    '0+git.fdf80cd'
    >>> snap.revision
    'x1'
  • paths:

    >>> snap.paths.common
    PosixPath('/var/snap/testapp/common')
    >>> snap.paths.data
    PosixPath('/var/snap/testapp/x1')
    >>> snap.paths.snap
    PosixPath('/snap/testapp/x1')
    >>> snap.paths.user_common
    PosixPath('/home/ack/snap/testapp/common')
    >>> snap.paths.user_data
    PosixPath('/home/ack/snap/testapp/x1')
  • snap-related environment variables:

    >>> pprint.pprint(dict(snap.environ))
    {'ARCH': 'amd64',
     'COMMON': '/var/snap/testapp/common',
     'CONTEXT': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
     'COOKIE': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
     'DATA': '/var/snap/testapp/x1',
     'INSTANCE_KEY': '',
     'INSTANCE_NAME': 'testapp',
     'LIBRARY_PATH': '/var/lib/snapd/lib/gl:/var/lib/snapd/lib/gl32:/var/lib/snapd/void',
     'NAME': 'testapp',
     'REEXEC': '',
     'REVISION': 'x1',
     'SNAP': '/snap/testapp/x1',
     'USER_COMMON': '/home/ack/snap/testapp/common',
     'USER_DATA': '/home/ack/snap/testapp/x1',
     'VERSION': '0+git.fdf80cd'}
    >>> snap.environ.ARCH
    'amd64'
  • configuration options:

    >>> snap.config.set({'foo.bar': 'baz', 'asdf': 3})  # this needs to be run as root
    >>> options = snap.config.get_options('foo', 'asdf')
    >>> options['foo']
    {'bar': 'baz'}
    >>> options['foo.bar']
    'baz'
    >>> options['asdf']
    3
    >>> options.as_dict()
    {'asdf': 3, 'foo': {'bar': 'baz'}}

Hook helpers

The library provides helpers to reduce boilerplate when setting up hooks for the snap.

Hooks can be defined by simply registering functions to be called as hooks via entry_points in the application setup.py:

setup(
    # ...
    entry_points={
        'snaphelpers.hooks': [
            'configure = testapp:configure_hook',
            'install = testapp:install_hook'
        ]
    }
)

Hook functions are called with a Snap objects as argument:

def install_hook(snap: snaphelpers.Snap):
    # ...


def configure_hook(snap: snaphelpers.Snap):
    # ...

snap-helpers will take care of the hooks plumbing (i.e. creating hook files in $SNAP/snap/hooks).

Testing with the snap

The snap-helpers snap provides a way to easily test code using the library in a real snap environment with strict confinement.

It provides the python and ipython commands:

$ snap-helpers.python -c 'from pprint import pprint; import snaphelpers; pprint(dict(snaphelpers.SnapEnviron()))'
{'ARCH': 'amd64',
 'COMMON': '/var/snap/snap-helpers/common',
 'CONTEXT': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
 'COOKIE': 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890',
 'DATA': '/var/snap/snap-helpers/x1',
 'INSTANCE_KEY': '',
 'INSTANCE_NAME': 'snap-helpers',
 'LIBRARY_PATH': '/var/lib/snapd/lib/gl:/var/lib/snapd/lib/gl32:/var/lib/snapd/void',
 'NAME': 'snap-helpers',
 'REEXEC': '',
 'REVISION': 'x1',
 'SNAP': '/snap/snap-helpers/x1',
 'USER_COMMON': '/home/ack/snap/snap-helpers/common',
 'USER_DATA': '/home/ack/snap/snap-helpers/x1',
 'VERSION': '0+git.26e1e9d'}

The snap can be built and installed as follows:

$ snapcraft
$ sudo snap install --dangerous snap-helpers_*.snap

Documentation

Full documentation is available on ReadTheDocs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snap-helpers-0.0.1.tar.gz (16.1 kB view details)

Uploaded Source

File details

Details for the file snap-helpers-0.0.1.tar.gz.

File metadata

  • Download URL: snap-helpers-0.0.1.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.7

File hashes

Hashes for snap-helpers-0.0.1.tar.gz
Algorithm Hash digest
SHA256 0e30849d6cab35cacbd00e8180ea4bd0dffb0fb39e150bd6a168a4798303027a
MD5 c852d47b92418d196ae6241977ec7719
BLAKE2b-256 6c1fbce769d1e3cc07a9519737d207b7e7f3bca472243bcce4acd307ab1910ca

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page