Skip to content

Commit

Permalink
fix: newer python syntax, methods (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
metaist committed Sep 13, 2024
1 parent b881183 commit e518654
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
8 changes: 5 additions & 3 deletions src/cosmofy/bundler.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ def compile_python(path: Path, source: Optional[bytes] = None) -> bytearray:
def move_set_executable(src: Path, dest: Path) -> Path:
"""Move a file and set its executable bit."""
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.move(src, dest)
# TODO 2024-10-31 @ py3.8 EOL: use `Path` instead of `str`
shutil.move(str(src), str(dest))
mode = dest.stat().st_mode | stat.S_IEXEC
dest.chmod(mode)
return dest
Expand Down Expand Up @@ -159,9 +160,10 @@ def archive_from_cache(
if self.args.for_real:
download_if_newer(COSMOFY_PYTHON_URL, self.args.cache / "python")

log.debug(f"{self.banner}copy: {self.args.cache / "python"} to {dest}")
src = self.args.cache / "python"
log.debug(f"{self.banner}copy: {src} to {dest}")
if self.args.for_real:
shutil.copy(self.args.cache / "python", dest)
shutil.copy(src, dest)

return archive or _archive(dest)

Expand Down
29 changes: 14 additions & 15 deletions src/cosmofy/zipfile2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@
class ZipFile2(ZipFile):
"""Extension of `zipfile.ZipFile` that allows removing members."""

def add_dir(self, name: str, mode: int = 0o777, date: datetime = now) -> ZipFile2:
"""Add a directory to an archive with permissions."""

if not name.endswith("/"):
name += "/"

info = ZipInfo(name, date.timetuple()[:6])
info.compress_size = 0
info.CRC = 0
info.external_attr = ((0o40000 | mode) & 0xFFFF) << 16
info.external_attr |= 0x10
info.file_size = 0
info.flag_bits |= 0x800
self.mkdir(info)
return self
# NOTE: This function only works on python >= 3.11
# def add_dir(self, name: str, mode: int = 0o777, date: datetime = now) -> ZipFile2:
# """Add a directory to an archive with permissions."""
# if not name.endswith("/"):
# name += "/"
# info = ZipInfo(name, date.timetuple()[:6])
# info.compress_size = 0
# info.CRC = 0
# info.external_attr = ((0o40000 | mode) & 0xFFFF) << 16
# info.external_attr |= 0x10
# info.file_size = 0
# info.flag_bits |= 0x800
# self.mkdir(info)
# return self

def add_file(
self,
Expand Down
2 changes: 1 addition & 1 deletion test/test_bundler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Test bundler."""

# std
import io
from pathlib import Path
import io

# pkg
from cosmofy.bundler import RE_MAIN
Expand Down

0 comments on commit e518654

Please sign in to comment.