Snapcraft building arm snaps on raspberry pi! This post will focus on wrapping a python script using snap. Its recommended that you do this on ubuntu core 18, available from the ubuntu cd image repo here

Install snapd, snapcraft and lxd

sudo apt update && sudo apt upgrade
sudo apt install snapd
sudo snap install core
sudo snap install snapcraft --classic
sudo snap install lxd
sudo usermod -a -G lxd ${USER}
lxd init (answer the questions with their defaults)
reboot (this is important)

multipass doesnt work, so you need to install lxd

Getting your Python Project Ready

to use python inside a snap, you need a working setuptools env.
https://setuptools.readthedocs.io/en/latest/userguide/quickstart.html#entry-points-and-automatic-script-creationhttps://pythonhosted.org/an_example_pypi_project/setuptools.html

To install the latest version of setuptools, use:

sudo apt install python3-pip
pip3 install --upgrade setuptools

https://pythonhosted.org/an_example_pypi_project/setuptools.html
If you don’t already have a setup.py file, create one:

touch setup.py

Inside the file, add some template code:

import os
from setuptools import setup
def read(fname):
   return open(os.path.join(os.path.dirname(__file__), fname)).read()

setup(
   name = "an_example_project",
   version = "0.0.1",
   author = "Your Name",
   author_email = "whoever@someplace",
   description = ("Description goes here"),
   license = "BSD",
   keywords = "example documentation tutorial",
   url = "whatever.com",
   packages=['an_example_pypi_project'],
   long_description=read('README'),
   classifiers=[
       "Development Status :: 3 - Alpha",
       "Topic :: Utilities",
       "License :: OSI Approved :: BSD License",
   ],
)

The directory structure for your project may need changed: it should look like this:

some_root_dir/
|-- README
|-- setup.py
|-- an_example_project
|   |-- __init__.py
|   |-- useful_1.py
|   |-- useful_2.py

Getting SNAP build files ready

Next, make a few files:

touch pyproject.toml:

Inside that file, add the following:

[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
setup.cfg:
[metadata]
name = "an_example_project"
version = 0.0.1
[options]
packages = "an_example_project"
install_requires =
 requests,
 netifaces

You can then test if it all works with the following command:

python3 setup.py bdist_wheel

Once that works without error, then try to build your snap:

snapcraft --use-lxd

It takes a while to build on RPi, but once its built, you can install with:

snap install ./an_example_project_armhf.snap --dangerous --devmode

To run as regular user privs:

snap run an_example_project

Or with a shell to explore the snap

snap run --shell an_example_project

To run as root privs:

sudo snap run an_example_project
sudo snap run --shell an_example_project

0 Comments