Skip to content

Commit

Permalink
review: Use TemporaryDirectory for cloning git repos
Browse files Browse the repository at this point in the history
  • Loading branch information
kimwnasptd committed Sep 5, 2024
1 parent c553807 commit a8ee551
Showing 1 changed file with 41 additions and 34 deletions.
75 changes: 41 additions & 34 deletions scripts/get-all-images.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import argparse
import logging
import shutil
import subprocess
import os
import sys
import contextlib
import tempfile

import git
import yaml

from typing import Iterator

# logging
LOG_FORMAT = "%(levelname)s \t| %(message)s"
logging.basicConfig(format=LOG_FORMAT, level=logging.INFO)
Expand Down Expand Up @@ -59,7 +63,8 @@ def bundle_app_contains_gh_metadata(app: dict) -> bool:
return False


def clone_repo(repo_name: str, branch: str):
@contextlib.contextmanager
def clone_git_repo(repo_name: str, branch: str) -> Iterator[git.PathLike]:
"""
Clones locally a repo and returns the path of the folder created.
Expand All @@ -69,11 +74,16 @@ def clone_repo(repo_name: str, branch: str):
"""
repo_url = f"https://github.com/canonical/{repo_name}.git"

logging.info(f"Cloning repo {repo_url}")
repo = git.Repo.clone_from(repo_url, str(repo_name))
# we can't use the default /tmp/ dir because of
# https://github.com/mikefarah/yq/issues/1808
with tempfile.TemporaryDirectory(dir=os.getcwd()) as tmp:
logging.info(f"Cloning repo {repo_url}")
repo = git.Repo.clone_from(repo_url, tmp)

logging.info(f"Checking out to branch {branch}")
repo.git.checkout(branch)

logging.info(f"Checking out to branch {branch}")
repo.git.checkout(branch)
yield repo.working_dir


def get_analytics_app_images(app: dict) -> list[str]:
Expand All @@ -87,21 +97,19 @@ def get_analytics_app_images(app: dict) -> list[str]:
script will also fail.
"""
images = []
repo_dir = app[GH_REPO_KEY]
clone_repo(app[GH_REPO_KEY], app[GH_BRANCH_KEY])

logging.info(f"Executing repo's {GET_IMAGES_SH} script")
try:
process = subprocess.run(["bash", "tools/get-images.sh"],
cwd=repo_dir, capture_output=True,
text=True, check=True)
except subprocess.CalledProcessError as exc:
logging.error("Script '%s' for charm '%s' failed: %s",
GET_IMAGES_SH, app["charm"], exc.stderr)
raise exc
finally:
# cleanup
shutil.rmtree(repo_dir)
repo_name = app[GH_REPO_KEY]
repo_branch = app[GH_BRANCH_KEY]

with clone_git_repo(repo_name, repo_branch) as repo_dir:
logging.info(f"Executing repo's {GET_IMAGES_SH} script")
try:
process = subprocess.run(["bash", "tools/get-images.sh"],
cwd=repo_dir, capture_output=True,
text=True, check=True)
except subprocess.CalledProcessError as exc:
logging.error("Script '%s' for charm '%s' failed: %s",
GET_IMAGES_SH, app["charm"], exc.stderr)
raise exc

images = process.stdout.strip().split("\n")

Expand All @@ -120,24 +128,23 @@ def get_dependency_app_images(app: dict) -> list[str]:
3. Delete the repo
"""
images = []
repo_dir = app[GH_DEPENDENCY_REPO_KEY]
clone_repo(app[GH_DEPENDENCY_REPO_KEY],
app[GH_DEPENDENCY_BRANCH_KEY])
with open("%s/metadata.yaml" % repo_dir, 'r') as metadata_file:
metadata_dict = yaml.safe_load(metadata_file)
repo_name = app[GH_DEPENDENCY_REPO_KEY]
repo_branch = app[GH_DEPENDENCY_BRANCH_KEY]
with clone_git_repo(repo_name, repo_branch) as repo_dir:
with open("%s/metadata.yaml" % repo_dir, 'r') as metadata_file:
metadata_dict = yaml.safe_load(metadata_file)

for _, rsrc in metadata_dict["resources"].items():
if "type" in rsrc and rsrc["type"] == "oci-image":
img = rsrc["upstream-source"]
logging.info("Found image %s" % img)
images.append(img)
for _, rsrc in metadata_dict["resources"].items():
if rsrc.get("type", "") != "oci-image":
continue

# cleanup
shutil.rmtree(repo_dir)
if "type" in rsrc and rsrc["type"] == "oci-image":
img = rsrc["upstream-source"]
images.append(img)

logging.info("Found the following images:")
for image in images:
logging.info(image)
logging.info("* " + image)

return images

Expand Down

0 comments on commit a8ee551

Please sign in to comment.