Skip to content

Commit

Permalink
abstract perm logic into new helper
Browse files Browse the repository at this point in the history
named normalize_artifact_permissions().
  • Loading branch information
mattp- committed May 28, 2024
1 parent 6bfbe67 commit 498f948
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
5 changes: 2 additions & 3 deletions backend/src/hatchling/builders/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from hatchling.builders.utils import (
get_reproducible_timestamp,
normalize_archive_path,
normalize_artifact_permissions,
normalize_file_permissions,
normalize_relative_path,
replace_file,
Expand All @@ -36,10 +37,8 @@ def __init__(self, name: str, *, reproducible: bool) -> None:
self.timestamp: int | None = get_reproducible_timestamp() if reproducible else None

raw_fd, self.path = tempfile.mkstemp(suffix='.tar.gz')
normalize_artifact_permissions(self.path)
self.fd = os.fdopen(raw_fd, 'w+b')
file_stat = os.stat(self.path)
new_mode = normalize_file_permissions(file_stat.st_mode)
os.chmod(self.path, new_mode)
self.gz = gzip.GzipFile(fileobj=self.fd, mode='wb', mtime=self.timestamp)
self.tf = tarfile.TarFile(fileobj=self.gz, mode='w', format=tarfile.PAX_FORMAT)
self.gettarinfo = lambda *args, **kwargs: self.normalize_tar_metadata(self.tf.gettarinfo(*args, **kwargs))
Expand Down
9 changes: 9 additions & 0 deletions backend/src/hatchling/builders/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ def normalize_file_permissions(st_mode: int) -> int:
return new_mode


def normalize_artifact_permissions(path: str) -> None:
"""
Normalize the permission bits for artifacts
"""
file_stat = os.stat(path)
new_mode = normalize_file_permissions(file_stat.st_mode)
os.chmod(path, new_mode)


def set_zip_info_mode(zip_info: ZipInfo, mode: int = 0o644) -> None:
"""
https://github.com/python/cpython/blob/v3.12.3/Lib/zipfile/__init__.py#L574
Expand Down
5 changes: 2 additions & 3 deletions backend/src/hatchling/builders/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
get_known_python_major_versions,
get_reproducible_timestamp,
normalize_archive_path,
normalize_artifact_permissions,
normalize_file_permissions,
normalize_inclusion_map,
replace_file,
Expand Down Expand Up @@ -79,9 +80,7 @@ def __init__(self, project_id: str, *, reproducible: bool) -> None:
self.time_tuple = None

raw_fd, self.path = tempfile.mkstemp(suffix='.whl')
file_stat = os.stat(self.path)
new_mode = normalize_file_permissions(file_stat.st_mode)
os.chmod(self.path, new_mode)
normalize_artifact_permissions(self.path)
self.fd = os.fdopen(raw_fd, 'w+b')
self.zf = zipfile.ZipFile(self.fd, 'w', compression=zipfile.ZIP_DEFLATED)

Expand Down
2 changes: 1 addition & 1 deletion tests/backend/builders/test_sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ def test_no_strict_naming(self, hatch, helpers, temp_dir, config_file):
stat = os.stat(str(extraction_directory / builder.artifact_project_id / 'PKG-INFO'))
assert stat.st_mtime == get_reproducible_timestamp()

def test_file_permissions_normalized(self, hatch, helpers, temp_dir, config_file):
def test_file_permissions_normalized(self, hatch, temp_dir, config_file):
config_file.model.template.plugins['default']['src-layout'] = False
config_file.save()

Expand Down
18 changes: 17 additions & 1 deletion tests/helpers/templates/wheel/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import hashlib
import os
import tempfile

from hatchling.builders.utils import format_file_hash
from hatchling.builders.utils import format_file_hash, normalize_artifact_permissions


def update_record_file_contents(record_file, files, generated_files=()):
Expand Down Expand Up @@ -42,3 +43,18 @@ def update_record_file_contents(record_file, files, generated_files=()):
record_file.contents += f'{template_file.path.as_posix()},sha256={hash_digest},{len(raw_contents)}\n'

record_file.contents += f'{record_file.path.as_posix()},,\n'


def test_normalize_artifact_permissions():
"""
assert that this func does what we expect on a tmpfile that that starts at 600
"""
_, path = tempfile.mkstemp()

file_stat = os.stat(path)
assert file_stat.st_mode == 0o100600

normalize_artifact_permissions(path)

file_stat = os.stat(path)
assert file_stat.st_mode == 0o100644

0 comments on commit 498f948

Please sign in to comment.