Repository navigation
Fix Python 3 incompatibilities in bin/docker-to-squash.py - #9296
ToniAdreal wants to merge 2 commits into
Conversation
rzo1
left a comment
There was a problem hiding this comment.
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_hashstill doesstr(line)on bytes and mangles the tag file, which is then uploaded back to HDFS,long(is still used in three places, andget_all_known_imagesstill 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".
…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.
805aed7 to
256173e
Compare
|
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. |
Part of #9291.
bin/docker-to-squash.pycannot run on Python 3 at all. Reproduced on Python 3.12.3 against master (4fca209):python3 docker-to-squash.py --helpfails immediately withImportError: cannot import name 'Iterable' from 'collections'.NameError: name 'long' is not definedfor--untagged-transition-sec.calculate_string_hash("x")raisesTypeError: Strings must be encoded before hashing.write_local_image_tag_to_hashraisesAttributeError: 'dict' object has no attribute 'iteritems'.Changes, all in
bin/docker-to-squash.py:Iterablefromcollections.abctype=long->type=intfor--untagged-transition-seccalculate_string_hashdict.iteritems()->dict.items()image is not ""comparisons ->!= ""(SyntaxWarning since Python 3.8)Testing:
python3 -m unittest test_docker_to_squash.pyinbin/errors on the import before this change and passes after it.--helpnow builds the full parser, andcalculate_string_hash/write_local_image_tag_to_hashwere 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.