From 8840fc7e2dbeccb91fccc15ece507cdf24d858b5 Mon Sep 17 00:00:00 2001
From: Tim Jaacks <tim.jaacks@garz-fricke.com>
Date: Wed, 17 Aug 2022 15:33:44 +0200
Subject: [PATCH] Fix temporary dir path

We were passing the complete TemporaryDirectory object to the repo clone
function instead of just the path string, resulting in the repo being
cloned into a local dir "<TemporaryDirectory '/tmp/tmphwakypf8'>".

Fix this to actually use the generated temp dir.

This change makes it necessary to keep the TemporaryDirectory object
reference until we don't need the directory anymore, otherwise it will
be removed immediately.
---
 scripts/deploy_gitlab_ci.py |  2 ++
 scripts/update_submodule.py | 19 ++++++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/scripts/deploy_gitlab_ci.py b/scripts/deploy_gitlab_ci.py
index e673d61d..c3a1bf0a 100755
--- a/scripts/deploy_gitlab_ci.py
+++ b/scripts/deploy_gitlab_ci.py
@@ -49,6 +49,7 @@ def integrate_submodule_into(
 
     (
         project_repo,
+        project_dir,
         integration_branch_name,
         integration_commit,
         message,
@@ -67,6 +68,7 @@ def integrate_submodule_into(
     ret = {
         "project": gitlab_project,
         "repo": project_repo,
+        "dir": project_dir,
         "branch": integration_branch_name,
         "commit": integration_commit,
         "message": message,
diff --git a/scripts/update_submodule.py b/scripts/update_submodule.py
index d5f6b123..ea8f5a97 100755
--- a/scripts/update_submodule.py
+++ b/scripts/update_submodule.py
@@ -85,7 +85,7 @@ def clone_project_and_submodule(project: Project, submodule_name, branch=None):
 
     # Checkout project
     try:
-        repo = Repo.clone_from(clone_url.url, project_dir, branch=branch, depth=1)
+        repo = Repo.clone_from(clone_url.url, project_dir.name, branch=branch, depth=1)
     except GitCommandError as e:
         sys.exit("ERROR: could not clone repository\n" + str(e))
     except IndexError:
@@ -130,7 +130,9 @@ def clone_project_and_submodule(project: Project, submodule_name, branch=None):
     with submodule.config_writer() as writer:
         writer.set("url", submodule_relative_url)
 
-    return repo, submodule_project
+    # We need to keep the TemporaryDirectory object reference project_dir because we
+    # need the cloned repo later, otherwise the directory will be immediately deleted.
+    return repo, submodule_project, project_dir
 
 
 def update_submodule_in_repo(repo: Repo, submodule_project: Project, new_revision):
@@ -209,6 +211,7 @@ def update_submodule_and_include_ref(
 
     submodule_update_needed = True
     project_repo = None
+    project_dir = None
     integration_commit = None
 
     if branch is None:
@@ -233,7 +236,7 @@ def update_submodule_and_include_ref(
             "No commits found in range %s, probably submodule already up-to-date.",
             revision_range,
         )
-        return None, None, None, None
+        return None, None, None, None, None
     logging.debug("New commits: %s", commits)
 
     # Find out if top commit is part of a merge request
@@ -328,7 +331,7 @@ def update_submodule_and_include_ref(
             clone_branch = integration_branch_name
 
         # Actually clone
-        project_repo, submodule_project = clone_project_and_submodule(
+        project_repo, submodule_project, project_dir = clone_project_and_submodule(
             project, submodule_name, clone_branch
         )
 
@@ -389,7 +392,13 @@ def update_submodule_and_include_ref(
                 less_verbose=True,
             )
 
-    return project_repo, integration_branch_name, integration_commit, message
+    return (
+        project_repo,
+        project_dir,
+        integration_branch_name,
+        integration_commit,
+        message,
+    )
 
 
 def main():
-- 
GitLab