diff mbox series

[AUH,v4,1/2] steps: read changelog through devtool's metadata file

Message ID 20261027133046.300849-1-daniel.turull@ericsson.com
State New
Headers show
Series [AUH,v4,1/2] steps: read changelog through devtool's metadata file | expand

Commit Message

Daniel Turull Oct. 27, 2026, 1:30 p.m. UTC
From: Daniel Turull <daniel.turull@ericsson.com>

devtool upgrade describes the changelog it extracted in
workspace/changelogs/<pn>.json, including the name of the changelog
file. Load that once per recipe and expose its fields in pkg_ctx with a
devtool_ prefix, so AUH relies on what devtool reports instead of
guessing the changelog file name, and the rest of the metadata is
available without reading it again.

Assisted-by: kiro:claude-sonnet-5.5
Signed-off-by: Daniel Turull <daniel.turull@ericsson.com>
---
Changes in v4:
- Load <pn>.json once, expose its fields in pkg_ctx with a devtool_
  prefix and get the changelog through the changelog_file field
Changes in v3:
- New patch: refactor split out of "steps: skip changelog in commit
  message when devtool flags it"
---
 modules/steps.py | 44 +++++++++++++++++++++++++++-----------------
 1 file changed, 27 insertions(+), 17 deletions(-)

Comments

Alexander Kanavin Oct. 28, 2026, 9:34 a.m. UTC | #1
On Tue, 27 Oct 2026 at 14:31, <daniel.turull@ericsson.com> wrote:

> +def _get_changelog(pkg_ctx, workdir):
> +    """Load the changelog and its metadata written by devtool upgrade into
> +    pkg_ctx and copy the changelog to workdir."""
> +    pn = pkg_ctx['PN']
> +    changelog_dir = os.path.join(os.environ.get('BUILDDIR', ''),
> +                                 'workspace', 'changelogs')
> +    metadata_path = os.path.join(changelog_dir, '{}.json'.format(pn))
> +    try:
> +        with open(metadata_path, 'r', encoding='utf-8') as f:
> +            metadata = json.load(f)
> +    except (OSError, ValueError):
> +        # devtool writes no metadata when there is no changelog
> +        return
> +    for key, value in metadata.items():
> +        pkg_ctx['devtool_' + key] = value
> +    changelog_path = os.path.join(changelog_dir,
> metadata['changelog_file'])
> +    try:
> +        with open(changelog_path, 'r', encoding='utf-8',
> errors='replace') as f:
> +            text = f.read()
> +        shutil.copy2(changelog_path, os.path.join(workdir,
> '{}-changelog.txt'.format(pn)))
> +    except OSError as e:
> +        W('Failed to copy changelog for {}: {}'.format(pn, e))
> +        return
> +    if text:
> +        pkg_ctx['changelog_text'] = text


This is closer to what I had in mind, but still not quite there. Claude's
code output isn't particularly good here, at this point I would just write
it by hand if I were you.

1. Don't suppress absent files or filesystem issues in exceptions. Json
file should always be there (this probably requires a tweak to devtool), so
that there's a track of what happened with changelog extraction. Similarly,
if anything in the secont try..except block failed, then it should be a
hard error.

2. Have a function that returns the json content, and call it thus:

pkg_ctx['devtool_upgrade_metadata'] = _get_devtool_json(...)

And then when needed:

changelog_text = _get_changelog_text(pkg_ctx['devtool_upgrade_metadata'])

Alex
Daniel Turull Oct. 28, 2026, 2:06 p.m. UTC | #2
On Wed, 2026-10-28 at 10:34 +0100, Alexander Kanavin wrote:
> On Tue, 27 Oct 2026 at 14:31, <daniel.turull@ericsson.com> wrote:
> > +def _get_changelog(pkg_ctx, workdir):
> > +    """Load the changelog and its metadata written by devtool upgrade into
> > +    pkg_ctx and copy the changelog to workdir."""
> > +    pn = pkg_ctx['PN']
> > +    changelog_dir = os.path.join(os.environ.get('BUILDDIR', ''),
> > +                                 'workspace', 'changelogs')
> > +    metadata_path = os.path.join(changelog_dir, '{}.json'.format(pn))
> > +    try:
> > +        with open(metadata_path, 'r', encoding='utf-8') as f:
> > +            metadata = json.load(f)
> > +    except (OSError, ValueError):
> > +        # devtool writes no metadata when there is no changelog
> > +        return
> > +    for key, value in metadata.items():
> > +        pkg_ctx['devtool_' + key] = value
> > +    changelog_path = os.path.join(changelog_dir, metadata['changelog_file'])
> > +    try:
> > +        with open(changelog_path, 'r', encoding='utf-8', errors='replace') as f:
> > +            text = f.read()
> > +        shutil.copy2(changelog_path, os.path.join(workdir, '{}-changelog.txt'.format(pn)))
> > +    except OSError as e:
> > +        W('Failed to copy changelog for {}: {}'.format(pn, e))
> > +        return
> > +    if text:
> > +        pkg_ctx['changelog_text'] = text
> > 
> 
> 
> This is closer to what I had in mind, but still not quite there. Claude's code output isn't particularly good here, at this point I would just write it by hand if I were you.
> 
> 1. Don't suppress absent files or filesystem issues in exceptions. Json file should always be there (this probably requires a tweak to devtool), so that there's a track of what happened with changelog extraction. Similarly, if anything in the secont try..except block failed, then it should be a hard error.
> 

I wanted to keep the existence of the metadata optional in case we run it on
wrynose or older releases that do not generate the changelogs. I'll limit the
try/except blocks

> 2. Have a function that returns the json content, and call it thus:
> 
> pkg_ctx['devtool_upgrade_metadata'] = _get_devtool_json(...)
> 
> And then when needed:
> 
> changelog_text = _get_changelog_text(pkg_ctx['devtool_upgrade_metadata'])
> 
> Alex

Sure. I'll rewrite the function in a v5.

Best regards,

Daniel
diff mbox series

Patch

diff --git a/modules/steps.py b/modules/steps.py
index e678a4b..a68dec4 100644
--- a/modules/steps.py
+++ b/modules/steps.py
@@ -19,6 +19,7 @@ 
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 #
 
+import json
 import os
 import shutil
 import re
@@ -80,20 +81,31 @@  def _make_commit_msg(group):
     versions = list(dict.fromkeys(_get_version(p) for p in group['pkgs']))
     return "{}: upgrade {}".format(pn, ",".join(versions))
 
-def _get_changelog(pn, workdir):
-    """Read the changelog generated by devtool upgrade and copy it to workdir.
-    Returns the changelog text or None."""
-    changelog_path = os.path.join(os.environ.get('BUILDDIR', ''),
-                                  'workspace', 'changelogs', '{}.txt'.format(pn))
-    if os.path.isfile(changelog_path):
-        try:
-            with open(changelog_path, 'r', encoding='utf-8', errors='replace') as f:
-                text = f.read()
-            shutil.copy2(changelog_path, os.path.join(workdir, '{}-changelog.txt'.format(pn)))
-            return text
-        except OSError as e:
-            W('Failed to copy changelog for {}: {}'.format(pn, e))
-    return None
+def _get_changelog(pkg_ctx, workdir):
+    """Load the changelog and its metadata written by devtool upgrade into
+    pkg_ctx and copy the changelog to workdir."""
+    pn = pkg_ctx['PN']
+    changelog_dir = os.path.join(os.environ.get('BUILDDIR', ''),
+                                 'workspace', 'changelogs')
+    metadata_path = os.path.join(changelog_dir, '{}.json'.format(pn))
+    try:
+        with open(metadata_path, 'r', encoding='utf-8') as f:
+            metadata = json.load(f)
+    except (OSError, ValueError):
+        # devtool writes no metadata when there is no changelog
+        return
+    for key, value in metadata.items():
+        pkg_ctx['devtool_' + key] = value
+    changelog_path = os.path.join(changelog_dir, metadata['changelog_file'])
+    try:
+        with open(changelog_path, 'r', encoding='utf-8', errors='replace') as f:
+            text = f.read()
+        shutil.copy2(changelog_path, os.path.join(workdir, '{}-changelog.txt'.format(pn)))
+    except OSError as e:
+        W('Failed to copy changelog for {}: {}'.format(pn, e))
+        return
+    if text:
+        pkg_ctx['changelog_text'] = text
 
 
 def _append_changelog_to_commit_msg(group, opts):
@@ -119,9 +131,7 @@  def _devtool_upgrade(devtool, bb, git, opts, pkg_ctx, workdir):
         devtool_output = devtool.upgrade(pkg_ctx['PN'], pkg_ctx['NPV'], pkg_ctx['NSRCREV'])
         D(" 'devtool upgrade' printed:\n%s" %(devtool_output))
         # Copy changelog before checking for conflicts — it's useful even on failure
-        changelog_text = _get_changelog(pkg_ctx['PN'], workdir)
-        if changelog_text:
-            pkg_ctx['changelog_text'] = changelog_text
+        _get_changelog(pkg_ctx, workdir)
         # If devtool failed to rebase patches, it does not fail, but we should
         if 'conflict' in devtool_output:
             raise DevtoolError("Running 'devtool upgrade' for recipe %s failed." %(pkg_ctx['PN']), devtool_output)