Skip to content

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

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

ToniAdreal wants to merge 2 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.

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.

2 participants