Skip to content

Commit

Permalink
Add support for custom data
Browse files Browse the repository at this point in the history
This patch implements proposal ST090. It allows to add custom
metadata into the snapcraft.yaml files without interferring
with snapcraft.

This is useful for adding extra data for tools that manage
snap files, like updatesnap.py, which searches for new versions
of the parts in a snapcraft.yaml file.

Proposal: https://docs.google.com/document/d/1QX0moP6V709D7L267pIT5AREQetbPb7KsFrpgvCLKVM
  • Loading branch information
sergio-costas committed Jul 17, 2023
1 parent dbe76f2 commit d810408
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions snapcraft/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
convert_architecture_deb_to_platform,
get_effective_base,
get_host_architecture,
remove_custom_data,
)


Expand Down Expand Up @@ -639,6 +640,7 @@ def unmarshal(cls, data: Dict[str, Any]) -> "Project":
if not isinstance(data, dict):
raise TypeError("Project data is not a dictionary")

data = remove_custom_data(data)
try:
project = Project(**data)
except pydantic.ValidationError as err:
Expand Down
19 changes: 19 additions & 0 deletions snapcraft/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,22 @@ def process_version(version: Optional[str]) -> str:
def is_snapcraft_running_from_snap() -> bool:
"""Check if snapcraft is running from the snap."""
return os.getenv("SNAP_NAME") == "snapcraft" and os.getenv("SNAP") is not None

def _remove_custom_data_from_dict(data):
prog = re.compile('^custom_data_[a-zA-Z0-9][a-zA-Z0-9_-]*$')
for element in list(data.keys()):
if prog.match(element) is not None:
data.pop(element)
return data

def remove_custom_data(data):
# remove custom data from the root
data = _remove_custom_data_from_dict(data)

# remove custom data from apps, plugs, slots and parts
for entry_name in ["apps", "plugs", "slots", "parts"]:
if entry_name in data:
for app in list(data[entry_name].keys()):
data[entry_name][app] = _remove_custom_data_from_dict(
data[entry_name][app])
return data
7 changes: 5 additions & 2 deletions snapcraft_legacy/project/_project_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
from snapcraft_legacy import yaml_utils

from . import _schema

from snapcraft.utils import (
remove_custom_data,
)

class ProjectInfo:
"""Information gained from the snap's snapcraft.yaml file."""

def __init__(self, *, snapcraft_yaml_file_path) -> None:
self.snapcraft_yaml_file_path = snapcraft_yaml_file_path
self.__raw_snapcraft = yaml_utils.load_yaml_file(snapcraft_yaml_file_path)
self.__raw_snapcraft = remove_custom_data(
yaml_utils.load_yaml_file(snapcraft_yaml_file_path))

try:
self.name = self.__raw_snapcraft["name"]
Expand Down

0 comments on commit d810408

Please sign in to comment.