Skip to content

Fix Python 3 incompatibilities in bin/docker-to-squash.py - #9296

Open
ToniAdreal wants to merge 3 commits into
apache:masterfrom
ToniAdreal:fix-9291-docker-to-squash-py3
Open

ToniAdreal wants to merge 3 commits into
apache:masterfrom
ToniAdreal:fix-9291-docker-to-squash-py3

Conversation

@ToniAdreal

@ToniAdreal ToniAdreal commented Nov 22, 2026 •

Copy link
Copy Markdown

Part of #9291.

bin/docker-to-squash.py cannot run on Python 3 at all. Reproduced on Python 3.12.3 against master (4fca209):

  • python3 docker-to-squash.py --help fails immediately with ImportError: cannot import name 'Iterable' from 'collections'.
  • After fixing only that import, building the argument parser raises NameError: name 'long' is not defined for --untagged-transition-sec.
  • calculate_string_hash("x") raises TypeError: Strings must be encoded before hashing.
  • write_local_image_tag_to_hash raises AttributeError: 'dict' object has no attribute 'iteritems'.

Changes, all in bin/docker-to-squash.py:

  • import Iterable from collections.abc
  • type=long -> type=int for --untagged-transition-sec
  • encode the string as UTF-8 before hashing in calculate_string_hash
  • dict.iteritems() -> dict.items()
  • two image is not "" comparisons -> != "" (SyntaxWarning since Python 3.8)

Testing: python3 -m unittest test_docker_to_squash.py in bin/ errors on the import before this change and passes after it. --help now builds the full parser, and calculate_string_hash / write_local_image_tag_to_hash were exercised directly.

This is one of the four docker-to-squash.py fixes that #9284 left to separate PRs (the others are #9289, #9290, #9292). It is also a prerequisite for testing the rest, since the test file cannot be imported on Python 3.10+ without it.

@rzo1 rzo1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ToniAdreal for picking this up. The changes are right individually, but they are only part of #9291, and merged alone they would make things worse: today the script fails at startup on Python 3, with this PR it runs but

  • calculate_string_hash(str(out)) hashes the text "b'...'" instead of the manifest bytes, so manifests get wrong hashes,
  • read_image_tag_to_hash still does str(line) on bytes and mangles the tag file, which is then uploaded back to HDFS,
  • long( is still used in three places, and get_all_known_images still splits bytes with a string.

Could you decode the subprocess output and file lines, hash the raw bytes, and replace the remaining long(? A round-trip test for the tag file and a hash test against hashlib.sha256 would help. Until it covers all of #9291, please change "Fixes #9291" to "Part of #9291".

ToniAdreal added a commit to ToniAdreal/storm that referenced this pull request Nov 24, 2026
…aining long()

Part of apache#9291. Per rzo1's review on apache#9296:
- Manifest hashes now computed over raw bytes (calculate_bytes_hash)
  instead of the str(bytes) repr.
- Subprocess output and tag-file lines decoded to str at use sites
  (decode_output helper); read_image_tag_to_hash no longer mangles
  entries via str(line).
- Remaining long() calls replaced with int(); get_all_known_images
  decodes hdfs_stat output before splitting.
- Tests: hash checked against hashlib.sha256, tag-file write/read
  round-trip, decode_output cases. 4/4 pass on Python 3.12.
The script cannot run on Python 3: Iterable is imported from collections (removed in 3.10), the --untagged-transition-sec argument uses type=long, calculate_string_hash passes a str to hashlib, and write_local_image_tag_to_hash uses dict.iteritems(). Import Iterable from collections.abc, use type=int, encode the string as UTF-8 before hashing, and use dict.items(). Also replace two 'is not ""' comparisons with '!=' to silence SyntaxWarning.

Signed-off-by: ToniAdreal <184365500+ToniAdreal@users.noreply.github.com>
…aining long()

Part of apache#9291. Per rzo1's review on apache#9296:
- Manifest hashes now computed over raw bytes (calculate_bytes_hash)
  instead of the str(bytes) repr.
- Subprocess output and tag-file lines decoded to str at use sites
  (decode_output helper); read_image_tag_to_hash no longer mangles
  entries via str(line).
- Remaining long() calls replaced with int(); get_all_known_images
  decodes hdfs_stat output before splitting.
- Tests: hash checked against hashlib.sha256, tag-file write/read
  round-trip, decode_output cases. 4/4 pass on Python 3.12.
@ToniAdreal
ToniAdreal force-pushed the fix-9291-docker-to-squash-py3 branch from 805aed7 to 256173e Compare November 24, 2026 04:49
@ToniAdreal

Copy link
Copy Markdown
Author

Thanks for the detailed review, that was really helpful. Pushed a follow-up in 256173e that covers each point: manifests are now hashed from the raw bytes through a new calculate_bytes_hash, subprocess output goes through decode_output instead of str(bytes), read_image_tag_to_hash reads the file as bytes and decodes each line, and the remaining long() uses are gone. Also added the two tests you asked for, a hash check against hashlib.sha256 and a write/read round-trip for the tag file. Changed the wording to Part of #9291 as well. Would appreciate another look when you have a moment.

@reiabreu reiabreu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was generated with the help of an LLM (Claude). Claude ran the checks described below.

Thanks @ToniAdreal, this covers all four points from rzo1's review.

Claude checked the current head (256173ec3) on Python 3.12:

  • Every use of subprocess output (from shell_command and the hdfs_* wrappers) is either decoded or used only as bytes, and no long, iteritems or str(out) is left.
  • Manifest hashes now match sha256sum, for a local file and for one and several manifests read through hdfs_cat (mocked).
  • The tag file survives a write-then-read round trip, including a non-ASCII comment.
  • All 6 tests in test_docker_to_squash.py pass.

Two optional inline suggestions below.

Note that CI doesn't run test_docker_to_squash.py yet, so these tests only run locally for now.

Comment thread bin/docker-to-squash.py Outdated

known_manifest_paths, err, returncode = hdfs_stat(HDFS_MANIFEST_DIR + "/*", "%a %Y %n", False, False, False)
known_manifests = [image for image in known_manifest_paths.split("\n") if image is not "" or None]
known_manifests = [image for image in decode_output(known_manifest_paths).split("\n") if image != "" or None]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: the or None does nothing here, so if image is the same:

Suggested change
known_manifests = [image for image in decode_output(known_manifest_paths).split("\n") if image != "" or None]
known_manifests = [image for image in decode_output(known_manifest_paths).split("\n") if image]

Comment thread bin/docker-to-squash.py Outdated
unref_manifests = []
if unref_manifest_paths:
unref_manifests = [image for image in unref_manifest_paths.split("\n") if image is not "" or None]
unref_manifests = [image for image in decode_output(unref_manifest_paths).split("\n") if image != "" or None]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: same here.

Suggested change
unref_manifests = [image for image in decode_output(unref_manifest_paths).split("\n") if image != "" or None]
unref_manifests = [image for image in decode_output(unref_manifest_paths).split("\n") if image]

reiabreu noted the `or None` in the two manifest list comprehensions
does nothing; `if image` filters empty strings the same way.

Signed-off-by: ToniAdreal <184365500+ToniAdreal@users.noreply.github.com>
@ToniAdreal

Copy link
Copy Markdown
Author

Thanks @reiabreu for the review and the local verification. Applied both optional suggestions in 2a5050a (if image in both manifest filters) and re-ran test_docker_to_squash.py locally on Python 3.12: all 6 tests pass.

One note on CI: the only failing check on this head is the Java Integration-Test job (SlidingWindowTest.testTimeWindow, JsonSyntaxException: Unterminated string ... path $.now), which looks unrelated to this Python-only change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants