kas: Improve script to generate kas yaml files

Message ID 20240318125206.31874-1-ubely@ilbers.de
State Accepted, archived
Headers show
Series kas: Improve script to generate kas yaml files | expand

Commit Message

Uladzimir Bely March 18, 2024, 12:51 p.m. UTC
Changes:
 - Avoid duplicated code.
 - Don't update old files if there are no real changes in them.

Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
---
 scripts/generate_yaml.sh | 89 +++++++++++++++++++++++++++++-----------
 1 file changed, 64 insertions(+), 25 deletions(-)

Comments

Uladzimir Bely March 22, 2024, 8:17 a.m. UTC | #1
On Mon, 2024-03-18 at 15:51 +0300, Uladzimir Bely wrote:
> Changes:
>  - Avoid duplicated code.
>  - Don't update old files if there are no real changes in them.
> 
> Signed-off-by: Uladzimir Bely <ubely@ilbers.de>
> ---
>  scripts/generate_yaml.sh | 89 +++++++++++++++++++++++++++++---------
> --
>  1 file changed, 64 insertions(+), 25 deletions(-)

Applied to next.

Patch

diff --git a/scripts/generate_yaml.sh b/scripts/generate_yaml.sh
index e17d6d3d..8e8651a0 100755
--- a/scripts/generate_yaml.sh
+++ b/scripts/generate_yaml.sh
@@ -1,7 +1,7 @@ 
 #!/usr/bin/env bash
 
-# This software is a part of ISAR.
-# Copyright (c) 2023 ilbers GmbH
+# This software is a part of Isar.
+# Copyright (c) 2023-2024 ilbers GmbH
 # Authors:
 #  Uladzimir Bely <ubely@ilbers.de>
 
@@ -14,52 +14,87 @@  set -e
 cd "$(dirname "$0")/.."
 
 HEADER="\
-# This software is a part of ISAR.
-# Copyright (C) 2023 ilbers GmbH
+# This software is a part of Isar.
+# Copyright (C) $(date +%Y) ilbers GmbH
 
 header:
   version: 14"
 
-# Scan for distro configs, except "debian-common" used only for including
+update_yaml() {
+  yaml=${1}
 
-DISTROS=$(find {meta,meta-isar}/conf/distro -iname *.conf -printf "%f\n" \
-  | sed -e 's/.conf$//' | grep -v "debian-common" | sort)
+  printf "%-45s | " ${yaml}
 
-for distro in ${DISTROS}
-do
-  cat << _EOF_ > kas/distro/${distro}.yaml
+  # Use temporary file if old one not exists
+  if [ ! -f "${yaml}" ]; then
+    echo "Not existed before, saving"
+    mv ${yaml}_tmp ${yaml}
+    return
+  fi
+
+  # Compare "pure" contents, without comments (e.g., copyrights, year)
+  old=$(grep -v "^#" ${yaml})
+  new=$(grep -v "^#" ${yaml}_tmp)
+
+  if [ "${new}" = "${old}" ]; then
+    echo "No real changes, keeping  "
+    rm ${yaml}_tmp
+  else
+    echo "File changed, saving"
+    mv ${yaml}_tmp ${yaml}
+  fi
+}
+
+make_yaml() {
+  dir=${1}
+  name=${2}
+  value=${3}
+
+  yaml="kas/${dir}/${value}.yaml"
+
+  # Generate temporary file
+  cat << _EOF_ > ${yaml}_tmp
 ${HEADER}
 
-distro: ${distro}
+${name}: ${value}
 _EOF_
+
+  update_yaml ${yaml}
+}
+
+
+# Scan for distro configs, except:
+# - "debian-common" used only for including
+# - "debian-sid-ports" not used currently
+
+DISTROS=$(find {meta,meta-isar}/conf/distro -iname *.conf -printf "%f\n" \
+  | sed -e 's/.conf$//' | grep -v "debian-common\|debian-sid-ports" | sort)
+
+for distro in ${DISTROS}
+do
+  make_yaml "distro" "distro" "${distro}"
 done
 
-# Scan for image recipes
+# Scan for image recipes, except:
+# - "isar-image-installer" having more complex structure
 
 IMAGES=$(find {meta,meta-isar}/recipes-core/images -iname *.bb -printf "%f\n" \
-  | sed -e 's/.bb$//' | sort)
+  | sed -e 's/.bb$//' | grep -v "isar-image-installer"| sort)
 
 for image in ${IMAGES}
 do
-  cat << _EOF_ > kas/image/${image}.yaml
-${HEADER}
-
-target: ${image}
-_EOF_
+  make_yaml "image" "target" "${image}"
 done
 
-# Scan for machine configs, except "rpi-common" used only for including
+# Scan for machine configs, except:
+# - "rpi-common" used only for including
 
 MACHINES=$(find meta-isar/conf/machine -iname *.conf -printf "%f\n" \
   | sed -e 's/.conf$//' | grep -v "rpi-common" | sort)
 
 for machine in ${MACHINES}
 do
-  cat << _EOF_ > kas/machine/${machine}.yaml
-${HEADER}
-
-machine: ${machine}
-_EOF_
+  make_yaml "machine" "machine" "${machine}"
 done
 
 # Generate configs for fixed list of Isar packages
@@ -87,11 +122,15 @@  do
     package=${pkg}-\$\{KERNEL_NAME\}
   fi
 
-  cat << _EOF_ > kas/package/pkg_${pkg}.yaml
+  yaml="kas/package/pkg_${pkg}.yaml"
+
+  cat << _EOF_ > ${yaml}_tmp
 ${HEADER}
 
 local_conf_header:
   package-${pkg}: |
     IMAGE_INSTALL:append = " ${package}"
 _EOF_
+
+  update_yaml ${yaml}
 done