Skip to content

Commit ac65344

Browse files
authored
Enhancement on add custom package script (#331)
1. Improve output message. 2. Append new package instead of creating replacing the file. 3. Added verify flag to check if package is exists in custom packages and image config. Signed-off-by: Tan Jia Yong <jia.yong.tan@intel.com>
1 parent f334e01 commit ac65344

File tree

1 file changed

+104
-20
lines changed

1 file changed

+104
-20
lines changed

toolkit/scripts/add_custom_packages.sh

Lines changed: 104 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,42 @@
1010

1111
# Usage:
1212
# ./add_custom_packages.sh "<package1 package2 ...>" path/to/image.json [custom-packages.json]
13+
# ./add_custom_packages.sh --verify "<package1 package2 ...>" path/to/image.json [custom-packages.json]
1314

1415
# Example:
1516
# ./add_custom_packages.sh "nano docker" ../imageconfigs/full.json custom-packages.json
17+
# ./add_custom_packages.sh --verify "nano docker" ../imageconfigs/full.json custom-packages.json
18+
19+
# Options:
20+
# --verify Verifies if the specified packages exist in the custom package list
21+
# and if the custom package list is included in the image configuration file.
22+
23+
# Parse arguments
24+
VERIFY=false
25+
if [[ "$1" == "--verify" ]]; then
26+
VERIFY=true
27+
shift
28+
fi
1629

1730
PACKAGE_LISTS="$1"
1831
IMAGE_JSON="$2"
1932
CUSTOM_PACKAGELIST_NAME="${3:-custom-packages.json}"
2033
CUSTOM_PACKAGELIST_PATH="packagelists/${CUSTOM_PACKAGELIST_NAME}"
2134

35+
# Check if jq is installed
2236
if ! command -v jq >/dev/null 2>&1; then
2337
echo "Error: jq is not installed." >&2
2438
exit 1
2539
fi
2640

41+
# Check jq version
2742
JQ_VERSION=$(jq --version | cut -d- -f2)
2843
if [ "$(printf '%s\n' "1.6" "$JQ_VERSION" | sort -V | head -n1)" != "1.6" ]; then
2944
echo "Error: jq version 1.6 or higher is required. Detected: $JQ_VERSION" >&2
3045
exit 1
3146
fi
3247

48+
# Validate input arguments
3349
if [[ -z "$PACKAGE_LISTS" || -z "$IMAGE_JSON" ]]; then
3450
echo "Usage: $0 \"<pkg1 pkg2 ...>\" path/to/image.json [custom-packages.json]" >&2
3551
exit 1
@@ -46,28 +62,96 @@ PACKAGELIST_DIR="$BASE_DIR/imageconfigs/packagelists"
4662
CUSTOM_PACKAGES_JSON="$PACKAGELIST_DIR/$CUSTOM_PACKAGELIST_NAME"
4763

4864
mkdir -p "$PACKAGELIST_DIR"
49-
PKG_ARRAY=$(for pkg in $PACKAGE_LISTS; do printf '"%s", ' "$pkg"; done | sed 's/, $//')
50-
echo "{ \"packages\": [ $PKG_ARRAY ] }" | jq . > "$CUSTOM_PACKAGES_JSON"
51-
echo "Created $CUSTOM_PACKAGES_JSON"
5265

53-
# Check if the custom packagelist is already present
66+
# Prepare the package array
67+
NEW_PACKAGES=()
68+
for pkg in $PACKAGE_LISTS; do
69+
NEW_PACKAGES+=("\"$pkg\"")
70+
done
71+
72+
# Verification logic
73+
if $VERIFY; then
74+
echo "Verification Results:"
75+
76+
# Check the custom package list file
77+
if [[ -f "$CUSTOM_PACKAGES_JSON" ]]; then
78+
MISSING_PACKAGES=()
79+
for pkg in $PACKAGE_LISTS; do
80+
if ! jq -e --arg pkg "$pkg" '.packages[] | select(. == $pkg)' "$CUSTOM_PACKAGES_JSON" >/dev/null; then
81+
MISSING_PACKAGES+=("$pkg")
82+
fi
83+
done
84+
if [[ ${#MISSING_PACKAGES[@]} -eq 0 ]]; then
85+
echo "- $CUSTOM_PACKAGELIST_NAME: All packages found."
86+
else
87+
echo "- $CUSTOM_PACKAGELIST_NAME: Missing packages: ${MISSING_PACKAGES[*]}"
88+
fi
89+
else
90+
echo "- $CUSTOM_PACKAGELIST_NAME: File not found."
91+
fi
92+
93+
# Check if the custom packagelist is included in the image JSON
94+
if jq --arg path "$CUSTOM_PACKAGELIST_PATH" '
95+
[.. | objects | select(has("PackageLists")) | .PackageLists[]?] | index($path)
96+
' "$IMAGE_JSON" | grep -qv null; then
97+
echo "- $IMAGE_JSON: The custom package list '$CUSTOM_PACKAGELIST_NAME' is included."
98+
else
99+
echo "- $IMAGE_JSON: The custom package list '$CUSTOM_PACKAGELIST_NAME' is not included."
100+
fi
101+
102+
exit 0
103+
fi
104+
105+
# Check if the custom-packages.json file already exists
106+
if [[ -f "$CUSTOM_PACKAGES_JSON" ]]; then
107+
echo "Appending to custom package list file '$CUSTOM_PACKAGELIST_NAME'..."
108+
# Get existing packages
109+
EXISTING_PACKAGES=$(jq -r '.packages[]' "$CUSTOM_PACKAGES_JSON")
110+
FILTERED_PACKAGES=()
111+
112+
# Filter out packages that already exist
113+
for pkg in "${NEW_PACKAGES[@]}"; do
114+
if ! echo "$EXISTING_PACKAGES" | grep -q -w "${pkg//\"}"; then
115+
FILTERED_PACKAGES+=("$pkg")
116+
fi
117+
done
118+
119+
if [[ ${#FILTERED_PACKAGES[@]} -eq 0 ]]; then
120+
echo "No new packages to add. All packages already exist in '$CUSTOM_PACKAGELIST_NAME'."
121+
else
122+
# Append new packages to the existing file
123+
TMP_JSON=$(mktemp)
124+
jq --argjson newPackages "$(printf '[%s]' "$(IFS=,; echo "${FILTERED_PACKAGES[*]}")")" '
125+
.packages += $newPackages | .packages |= unique
126+
' "$CUSTOM_PACKAGES_JSON" > "$TMP_JSON" && mv "$TMP_JSON" "$CUSTOM_PACKAGES_JSON"
127+
128+
# Join the filtered packages into a single string
129+
FORMATTED_PACKAGES=$(printf '%s ' "${FILTERED_PACKAGES[@]//\"/}" | sed 's/ $//')
130+
echo "Packages \"$FORMATTED_PACKAGES\" added successfully to '$CUSTOM_PACKAGELIST_NAME'."
131+
fi
132+
else
133+
# Create a new custom-packages.json file
134+
echo "{ \"packages\": [ $(printf '%s,' "${NEW_PACKAGES[@]}" | sed 's/,$//') ] }" | jq . > "$CUSTOM_PACKAGES_JSON"
135+
echo "Created custom package list file '$CUSTOM_PACKAGES_JSON'."
136+
echo "Packages \"${NEW_PACKAGES[*]//\"}\" added successfully to '$CUSTOM_PACKAGELIST_NAME'."
137+
fi
138+
139+
# Ensure the custom packagelist is present in the image JSON
54140
if jq --arg path "$CUSTOM_PACKAGELIST_PATH" '
55141
[.. | objects | select(has("PackageLists")) | .PackageLists[]?] | index($path)
56142
' "$IMAGE_JSON" | grep -qv null; then
57-
echo "'$CUSTOM_PACKAGELIST_PATH' is already present in PackageLists in $IMAGE_JSON."
58-
echo "No changes made."
59-
exit 0
60-
fi
143+
echo "The custom package '$CUSTOM_PACKAGELIST_NAME' already exists in the image configuration file '$IMAGE_JSON'."
144+
else
145+
# Append to PackageLists arrays in the image JSON
146+
TMP_JSON=$(mktemp)
147+
jq --arg path "$CUSTOM_PACKAGELIST_PATH" '
148+
walk(
149+
if type == "object" and has("PackageLists") and (.PackageLists | type == "array")
150+
then .PackageLists += [$path]
151+
else .
152+
end
153+
)
154+
' "$IMAGE_JSON" > "$TMP_JSON" && mv "$TMP_JSON" "$IMAGE_JSON"
61155

62-
# Append to all PackageLists arrays
63-
TMP_JSON=$(mktemp)
64-
jq --arg path "$CUSTOM_PACKAGELIST_PATH" '
65-
walk(
66-
if type == "object" and has("PackageLists") and (.PackageLists | type == "array")
67-
then .PackageLists += [$path]
68-
else .
69-
end
70-
)
71-
' "$IMAGE_JSON" | jq . > "$TMP_JSON" && mv "$TMP_JSON" "$IMAGE_JSON"
72-
73-
echo "Appended '$CUSTOM_PACKAGELIST_PATH' to all PackageLists in $IMAGE_JSON"
156+
echo "The custom package '$CUSTOM_PACKAGELIST_PATH' has been added to PackageLists in '$IMAGE_JSON'."
157+
fi

0 commit comments

Comments
 (0)