Skip to content

Commit

Permalink
Larger hash space
Browse files Browse the repository at this point in the history
  • Loading branch information
tofarr committed Oct 17, 2024
1 parent b5811f8 commit bc7a464
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
15 changes: 15 additions & 0 deletions openhands/runtime/utils/runtime_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
import hashlib
import os
import shutil
import string
import tempfile
from pathlib import Path
from typing import List

import docker
from dirhash import dirhash
Expand Down Expand Up @@ -238,6 +240,19 @@ def prep_build_folder(
file.write(dockerfile_content) # type: ignore


_ALPHABET = string.digits + string.ascii_lowercase


def truncate_hash(hash: str) -> str:
"""Convert the base16 hash to base36 and truncate at 16 characters."""
value = int(hash, 16)
result: List[str] = []
while value > 0 and len(result) < 16:
value, remainder = divmod(value, len(_ALPHABET))
result.append(_ALPHABET[remainder])
return ''.join(result)


def get_hash_for_lock_files(base_image: str):
openhands_source_dir = Path(openhands.__file__).parent
md5 = hashlib.md5()
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/test_runtime_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
get_runtime_image_repo,
get_runtime_image_repo_and_tag,
prep_build_folder,
truncate_hash,
)

OH_VERSION = f'oh_v{oh_version}'
Expand Down Expand Up @@ -522,3 +523,10 @@ def test_image_exists_not_found():
mock_client.api.pull.assert_called_once_with(
'nonexistent', tag='image', stream=True, decode=True
)


def test_truncate_hash():
truncated = truncate_hash('b08f254d76b1c6a7ad924708c0032251')
assert truncated == 'pma2wc71uq3c9a85'
truncated = truncate_hash('102aecc0cea025253c0278f54ebef078')
assert truncated == '4titk6gquia3taj5'

0 comments on commit bc7a464

Please sign in to comment.