Skip to content

Commit 5508dcf

Browse files
committed
Tiny code improvements.
Signed-off-by: Kurt Garloff <[email protected]>
1 parent a47a6af commit 5508dcf

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[flake8]
2+
ignore = E501

user-docs/usage-hints/find-image/find_img.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22
#
3-
# find-img.py
3+
# find_img.py
44
#
5-
# Searches for an image with distribution and version
5+
# Searches for an image with distribution and version and purpose
66
#
77
# (c) Kurt Garloff <[email protected]>, 7/2025
88
# SPDX-License-Identifier: MIT
@@ -71,17 +71,19 @@ def img_sort_heuristic(images, distro, version, purpose):
7171
def find_image(conn, distro, version, purpose="generic", strict=False, log=None):
7272
"""Return a sorted list of ID,Name pairs that contain the wanted image.
7373
Empty list indicates no image has been found. The list is sorted such
74-
that (on SCS-compliant clouds), it will very likely contain the most
75-
vanilla, most recent image as first element.
74+
that (on SCS-compliant clouds), it will very likely contain the best
75+
matching, most recent image as first element.
7676
If strict is set, multiple matches are not allowed.
7777
"""
7878
ldistro = distro.lower()
7979
# FIXME: The image.images() method only passes selected filters
80+
purpose_out = purpose
8081
images = [x for x in conn.image.images(os_distro=ldistro, os_version=version,
8182
sort="name:desc,created_at:desc", visibility="public")
8283
if x.properties.get("os_purpose") == purpose]
8384
if len(images) == 0:
8485
warn(log, f"No image found with os_distro={ldistro} os_version={version} os_purpose={purpose}")
86+
purpose_out = ""
8587
# images = list(conn.image.images(os_distro=ldistro, os_version=version,
8688
# sort="name:desc,created_at:desc"))
8789
images = [x for x in conn.image.images(os_distro=ldistro, os_version=version,
@@ -92,8 +94,8 @@ def find_image(conn, distro, version, purpose="generic", strict=False, log=None)
9294
return []
9395
# Now comes sorting magic for best backwards compatibility
9496
if len(images) > 1:
95-
debug(log, f"Several {purpose} images found with os_distro={ldistro} os_version={version}")
96-
if (strict):
97+
debug(log, f"Several {purpose_out} images found with os_distro={ldistro} os_version={version}")
98+
if strict:
9799
return []
98100
return img_sort_heuristic(images, distro, version, purpose)
99101
return [(img.id, img.name) for img in images]

user-docs/usage-hints/find-image/find_img.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
usage()
99
{
10-
echo "Usage: find-img distro version [purpose]"
10+
echo "Usage: find-img [-s] distro version [purpose]"
1111
echo "Returns all images matching, latest first, purpose defaults to generic"
1212
echo "If some images have the wanted purpose, only those will be shown"
1313
}
@@ -28,7 +28,7 @@ img_sort_heuristic()
2828
{
2929
# Acts on global OS_RESP
3030
# FIXME: We could do all sorts of advanced heuristics here, looking at the name etc.
31-
# We only do one thing here: Sort the image that matches the old naming scheme first.
31+
# We only do a few pattern matches here
3232
# distro version purpose
3333
local NEW_RESP0=$(echo "$OS_RESP" | grep -i "^[0-9a-f\-]* $1 $2 $3\$")
3434
# distro version purpose with extras appended
@@ -48,10 +48,12 @@ img_sort_heuristic()
4848

4949
get_images()
5050
{
51-
PURPOSE=${3:-generic}
51+
PURPOSE="${3:-generic}"
52+
PURP="$PURPOSE"
5253
get_images_raw "$1" "$2" --property os_purpose=$PURPOSE
5354
if test -z "$OS_RESP"; then
5455
echo "WARN: No image found with os_distro=$1 os_version=$2 os_purpose=$PURPOSE" 1>&2
56+
PURP=""
5557
# We're screwed as we can not filter for the absence of os_purpose with CLI
5658
# We could loop and do an image show and then flter out, but that's very slow
5759
get_images_raw "$1" "$2" # --property os_purpose=
@@ -69,7 +71,7 @@ get_images()
6971
if test "$NR_IMG" = "0"; then echo "ERROR: No image found with os_distro=$1 os_version=$2" 1>&2; return 1
7072
elif test "$NR_IMG" = "1"; then return 0
7173
else
72-
echo "DEBUG: Several $PURPOSE images matching os_distro=$1 os_version=$2" 1>&2;
74+
echo "DEBUG: Several $PURP images matching os_distro=$1 os_version=$2" 1>&2;
7375
if test -n "$STRICT"; then return 1; fi
7476
img_sort_heuristic "$1" "$2" "$PURPOSE"
7577
return 0
@@ -80,10 +82,8 @@ if test -z "$OS_CLOUD" -a -z "$OS_AUTH_URL"; then
8082
echo "You need to configure clouds.yaml/secure.yaml and set OS_CLOUD" 1>&2
8183
exit 2
8284
fi
83-
if test -z "$1"; then
84-
usage
85-
exit 1
86-
fi
85+
if test "$1" = "-s"; then STRICT=1; shift; fi
86+
if test -z "$1"; then usage; exit 1; fi
8787

8888
get_images "$@"
8989
RC=$?

0 commit comments

Comments
 (0)