← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/launchpad:add-is_unified_format-for-charms into launchpad:master

 

Jürgen Gmach has proposed merging ~jugmac00/launchpad:add-is_unified_format-for-charms into launchpad:master.

Commit message:
Add is_unified_format for charm configuration

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/launchpad/+git/launchpad/+merge/467151
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/launchpad:add-is_unified_format-for-charms into launchpad:master.
diff --git a/lib/lp/charms/model/charmrecipe.py b/lib/lp/charms/model/charmrecipe.py
index e2fbb39..8436375 100644
--- a/lib/lp/charms/model/charmrecipe.py
+++ b/lib/lp/charms/model/charmrecipe.py
@@ -124,6 +124,29 @@ from lp.services.webhooks.model import WebhookTargetMixin
 from lp.soyuz.model.distroarchseries import DistroArchSeries, PocketChroot
 
 
+def is_unified_format(configuration_as_dict):
+    """CharmCraft's configuration file exits in two versions.
+
+    The new version, referred to as `unified`, was introduced to unify the
+    syntax across the various craft tools, see the corresponding spec:
+    https://docs.google.com/document/d/1HrGw_MpfJoMpoGRw74Qk3eP7cl7viwcmoPe9nICag2U/edit
+    (access restricted to Canonical employees)
+
+    The unified format is not backwards compatible.
+
+    Relevant for the detection are the following differences compared to the
+    old version:
+    - `bases` was renamed to `base`
+    - a `build-base` key was introduced
+    - a `platforms` key was introduced
+    """
+    new_keys = ["base", "build-base", "platforms"]
+    for key in new_keys:
+        if key in configuration_as_dict.keys():
+            return True
+    return False
+
+
 def charm_recipe_modified(recipe, event):
     """Update the date_last_modified property when a charm recipe is modified.
 
diff --git a/lib/lp/charms/tests/test_charmrecipe.py b/lib/lp/charms/tests/test_charmrecipe.py
index 1493f6f..740d415 100644
--- a/lib/lp/charms/tests/test_charmrecipe.py
+++ b/lib/lp/charms/tests/test_charmrecipe.py
@@ -7,6 +7,7 @@ import base64
 import json
 from datetime import datetime, timedelta, timezone
 from textwrap import dedent
+from unittest import TestCase
 
 import iso8601
 import responses
@@ -73,7 +74,7 @@ from lp.charms.interfaces.charmrecipebuildjob import ICharmhubUploadJobSource
 from lp.charms.interfaces.charmrecipejob import (
     ICharmRecipeRequestBuildsJobSource,
 )
-from lp.charms.model.charmrecipe import CharmRecipeSet
+from lp.charms.model.charmrecipe import CharmRecipeSet, is_unified_format
 from lp.charms.model.charmrecipebuild import CharmFile
 from lp.charms.model.charmrecipebuildjob import CharmRecipeBuildJob
 from lp.charms.model.charmrecipejob import CharmRecipeJob
@@ -118,6 +119,34 @@ from lp.testing.matchers import DoesNotSnapshot, HasQueryCount
 from lp.testing.pages import webservice_for_person
 
 
+class TestCharmRecipeFormatDetector(TestCase):
+
+    def test_is_unified_format_with_old_format(self):
+        # 'bases' is only used with the old configuration format
+        d = {
+            "bases": {
+                "build-on": [
+                    {
+                        "name": "ubuntu",
+                        "channel": "20.04",
+                        "architectures": ["sparc"],
+                    }
+                ]
+            },
+        }
+        self.assertFalse(is_unified_format(d))
+
+    def test_is_unified_format_with_new_syntax(self):
+        # 'base', 'build-base', and 'platforms' were introduced with the
+        # 'unified' configuration
+        d = {
+            "base": "ubuntu@24.04",
+            "build-base": "ubuntu@24.04",
+            "platforms": {"amd64": [{"build-on": "riscv64"}]},
+        }
+        self.assertTrue(is_unified_format(d))
+
+
 class TestCharmRecipeFeatureFlags(TestCaseWithFactory):
     layer = LaunchpadZopelessLayer