Using external metadata

To help avoid unnecessary duplication, and for convenience, Snapcraft can process and incorporate external metadata from within snapcraft.yaml by using parse-info within a part and a corresponding adopt-info key.

For example, the following snapcraft.yaml will parse a file called metadata-file. Snapcraft will attempt to extract version, summary and description metadata for the snap, all of which are mandatory:

name: my-snap-name
adopt-info: part-with-metadata

parts:
  part-with-metadata:
    plugin: dump
    source: .
    parse-info: [metadata-file]

See The snapcraft format for further details on Snapcraft metadata and how it’s used.

Source types

An external metadata source can be one of the following:

  • AppStream: a standard for software components
  • Scriptlets: a snapcraftctl-driven command to generate version and grade

See below for details on incorporating each of the above into your snapcraft.yaml.

AppStream

AppStream is a metadata standard used to describe a common set software components. It can be parsed by snapcraft to provide the summary, description and icon for a snap, along with the location of an app’s desktop file.

The following is a typical example from an upstream project. It’s an appstream file called sampleapp.metainfo.xml:

<?xml version="1.0" encoding="UTF-8"?>
<component type="desktop-application">
  <id>com.example.sampleapp</id>
  <summary>Single-line elevator pitch for your amazing application</summary>
  <description>
    This is applications's description. A paragraph or two to tell the
    most important story about it.
  </description>
  <icon type="local">assets/icon.png</icon>
  <launchable type="desktop-id">
    com.example.sampleapp.desktop
  </launchable>
</component>

We adopt the above metadata into snapcraft.yaml with the following:

name: sampleapp-name
adopt-info: sampleapp

apps:
  sampleapp:
    command: sampleapp
    common-id: com.example.sampleapp

parts:
  sampleapp:
    plugin: dump
    source: http://github.com/example/sampleapp.git
    parse-info: [usr/share/metainfo/com.example.sampleapp.appdata.xml]

The path in parse-info is a relative path from the part source, build or install directory (SNAPCRAFT_PART_SRC, SNAPCRAFT_PART_BUILD, SNAPCRAFT_PART_INSTALL).

The resulting snap will use the summary and description from the AppStream file.

You can also link each app in your snap to specific AppStream metadata by pointing the common-id key of that app to the component id field in the AppStream metadata. Snapcraft will use the metadata of that component to get the .desktop entry file for that app.

For backwards compatibility, some component ids in the AppStream metadata have a .desktop suffix. If this is the case for your application, the common-id of your app should also use that suffix.

Note: The process to get the .desktop file entry from the AppStream metadata goes as follows. First, Snapcraft searches for a parsed AppStream file with the same component id as the app’s common-id and extracts the Desktop File ID (desktop-id) from that component. If that component doesn’t specify a desktop-id, Snapcraft will use the component id as the Desktop File ID. Snapcraft will then search for a desktop file matching the Desktop File ID in the usr/local/share and usr/share directories relative to the part source, and by following the Desktop File ID rules.

Part scriptlets

Individual parts in your snapcraft.yaml can set the version and grade by using snapcraftctl. All you need to do is select which part to adopt using adopt-info:

# ...
adopt-info: my-part
# ...
parts:
  my-part:
    # ...
    override-pull: |
      snapcraftctl pull
      snapcraftctl set-version "my-version"
      snapcraftctl set-grade "devel"

See Scriptlets for more details on using scripting elements within snapcraft.yaml.

[not recommended] `setup.py`

Using parse-info with setup.py is currently discouraged because it has many issues. For example, it incorrectly uses the project’s summary as the snap’s description and it might crash the snap build.

A setup.py file is used by many Python projects to help with package installation. If your setup.py uses setuptools and defines version and description, these can be extracted from setup.py and used as the version and description metadata in the resulting snap.

The following is an example setup.py in the root of a hypothetical git tree:

import setuptools

setuptools.setup(
    name='hello-world',
    version='1.0',
    author='snapcrafter',
    author_email='snapcraft@lists.snapcraft.io',
    description='A simple hello world in python',
    scripts=['hello']
)

You can adopt the relevant metadata in the above with the following snapcraft.yaml

name: sampleapp-name
summary: sampleapp summary
adopt-info: sampleapp

apps:
  sampleapp:
    command: sampleapp

parts:
  sampleapp:
    plugin: python
    source: http://github.com/example/sampleapp.git
    parse-info: [setup.py]

Snapcraft versions and compatibility

Change snapcraft version
Initial introduction 2.39
appstream support 2.39
common-id 2.40
setup.py support 2.41
snapcraftctl set-version 2.41
snapcraftctl set-grade 2.41

Last updated a month ago. Help improve this document in the forum.