Skip to content

Commit 324b1c1

Browse files
committed
improve diff script to show versions diff for a package and its dependencies
1 parent cfb4b95 commit 324b1c1

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

diff

+21-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Options:
1111
-b, --base The base image to diff against.
1212
-t, --target The image to diff against the base image.
1313
Default is the locally built image.
14+
-p, --package Only show diff for this package and its dependencies.
1415
EOF
1516
}
1617

@@ -19,6 +20,7 @@ BASE_IMAGE_TAG='gcr.io/kaggle-images/python:latest'
1920
BASE_IMAGE_TAG_OVERRIDE=''
2021
TARGET_IMAGE_TAG='kaggle/python-build'
2122
TARGET_IMAGE_TAG_OVERRIDE=''
23+
PACKAGE_NAME=''
2224

2325
while :; do
2426
case "$1" in
@@ -48,6 +50,15 @@ while :; do
4850
TARGET_IMAGE_TAG_OVERRIDE="$2"
4951
shift # skip the flag value
5052
;;
53+
-p|--package)
54+
if [[ -z "$2" ]]; then
55+
usage
56+
printf 'ERROR: No PACKAGE specified after the %s flag.\n' "$1" >&2
57+
exit
58+
fi
59+
PACKAGE_NAME="$2"
60+
shift # skip the flag value
61+
;;
5162
-?*)
5263
usage
5364
printf 'ERROR: Unknown option: %s\n' "$1" >&2
@@ -78,11 +89,18 @@ if [[ "$BASE_IMAGE_TAG" == "gcr.io/"* ]]; then
7889
docker pull "$BASE_IMAGE_TAG"
7990
fi
8091

81-
CMDS=('dpkg-query --show -f "${Package}==${Version}\n"' 'pip freeze')
92+
93+
if [[ -n "$PACKAGE_NAME" ]]; then
94+
echo "Package: $PACKAGE_NAME"
95+
CMDS=("python /tools/pip_list_versions.py $PACKAGE_NAME | sort")
96+
else
97+
CMDS=('dpkg-query --show -f "${Package}==${Version}\n"' 'pip freeze')
98+
fi
99+
82100
for cmd in "${CMDS[@]}"; do
83101
echo "== Comparing $cmd =="
84102
diff --suppress-common-lines --side-by-side \
85-
<(docker run --rm "$BASE_IMAGE_TAG" $cmd) \
86-
<(docker run --rm "$TARGET_IMAGE_TAG" $cmd) \
103+
<(docker run -v $PWD/tools:/tools --rm "$BASE_IMAGE_TAG" /bin/bash -c "$cmd") \
104+
<(docker run -v $PWD/tools:/tools --rm "$TARGET_IMAGE_TAG" /bin/bash -c "$cmd") \
87105
&& echo 'No diff' || true
88106
done

tools/pip_list_versions.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import argparse
2+
import logging
3+
import re
4+
import subprocess
5+
6+
7+
def pip_show(package_name, packages=[]):
8+
if package_name in packages:
9+
return # avoid checking the same package twice if multiple packages depends on it.
10+
packages.append(package_name)
11+
12+
result = subprocess.run(['pip', 'show', package_name], stdout=subprocess.PIPE)
13+
if result.returncode != 0:
14+
logging.error("pip show %s failed", package_name)
15+
16+
show_stdout = result.stdout.decode("utf-8")
17+
print(package_name + "==" + get_version(show_stdout))
18+
19+
for dependency in get_dependencies(show_stdout):
20+
pip_show(dependency, packages=packages)
21+
22+
def get_version(show_stdout):
23+
for line in show_stdout.split("\n"):
24+
m = re.match(r"^Version:\s(?P<version>.+)$", line)
25+
if m:
26+
return m.group('version')
27+
return "not found"
28+
29+
def get_dependencies(show_stdout):
30+
for line in show_stdout.split("\n"):
31+
m = re.match(r"^Requires:\s(?P<requires>.+)$", line)
32+
if m:
33+
return m.group('requires').split(', ')
34+
return []
35+
36+
37+
if __name__ == "__main__":
38+
parser = argparse.ArgumentParser()
39+
parser.add_argument('package', type=str, help='package name')
40+
args = parser.parse_args()
41+
42+
pip_show(args.package)

0 commit comments

Comments
 (0)