From 478668783c95e8e39196bb0da6716fba57ee4114 Mon Sep 17 00:00:00 2001
From: www user for firmware <www@firmware.selfhosted.de>
Date: Sat, 12 Dec 2020 17:04:30 +0000
Subject: [PATCH] move symlink creation to python

---
 download-experimental.sh          |  7 +------
 download-latest-firmware-build.py | 12 ++++++++++--
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/download-experimental.sh b/download-experimental.sh
index 571b227..71325cf 100755
--- a/download-experimental.sh
+++ b/download-experimental.sh
@@ -8,9 +8,4 @@ PIPELINE_ID_FILE="/home/www/html/firmware/gluon/.last-downloaded-pipeline-id"
 MYDIR=$(readlink -f $(dirname "$0"))
 
 cd "$ARCHIVE_DIR"
-downloaded_version=$("$MYDIR/download-latest-firmware-build.py" --pipeline-id-file "$PIPELINE_ID_FILE")
-downloaded_version_abs=$(readlink -f "$downloaded_version/images")
-
-rm "$EXPERIMENTAL_LINK"
-ln -s "$downloaded_version_abs" "$EXPERIMENTAL_LINK"
-
+"$MYDIR/download-latest-firmware-build.py" --pipeline-id-file "$PIPELINE_ID_FILE" --create-symlink "$EXPERIMENTAL_LINK"
diff --git a/download-latest-firmware-build.py b/download-latest-firmware-build.py
index 12e367a..60b9526 100755
--- a/download-latest-firmware-build.py
+++ b/download-latest-firmware-build.py
@@ -17,6 +17,7 @@ PROJECT_ID = 1
 ap = argparse.ArgumentParser()
 ap.add_argument("--pipeline-id", help="Pipeline ID to download. If omitted, download latest successfull.", default=None)
 ap.add_argument("--pipeline-id-file", help="Store downloaded Pipeline ID in this file and only download if latest pipeline ID doesn't match file contents")
+ap.add_argument("--create-symlink", help="Create symlink to downloaded firmware at the specified path")
 args = ap.parse_args()
 
 def find_version_from_archive(archive_file_list):
@@ -32,11 +33,11 @@ def find_version_from_archive(archive_file_list):
 def extract_zip(artifact_zipfile):
     with zipfile.ZipFile(artifact_zipfile) as artifact_zip:
         version = find_version_from_archive(artifact_zip.infolist())
-        print(f"Version is {version}")
         with tempfile.TemporaryDirectory(dir=os.getcwd()) as tempdir:
             artifact_zip.extractall(tempdir)
             outputdir = os.path.join(tempdir, "gluon", "output")
             os.rename(outputdir, version)
+        return version
 
 def find_latest_pipeline_id():
     pipelines_request = requests.get("{}/projects/{}/pipelines".format(GITLAB_API_BASE, PROJECT_ID))
@@ -74,7 +75,14 @@ for job in pipeline_jobs:
                 for chunk in artifact_request.iter_content(chunk_size=1024*1024):
                     artifact_temp.write(chunk)
 
-            extract_zip(artifact_temp)
+            downloaded_version = extract_zip(artifact_temp)
+
+            if args.create_symlink:
+                if os.path.islink(args.create_symlink):
+                    os.remove(args.create_symlink)
+                images_dir = os.path.abspath(os.path.join(downloaded_version, "images"))
+                os.symlink(images_dir, args.create_symlink)
+
             if args.pipeline_id_file:
                 with open(args.pipeline_id_file, "w") as pipeline_id_file:
                     pipeline_id_file.write(str(pipeline_id))
-- 
GitLab