-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathpackageTools.py
More file actions
37 lines (29 loc) · 1.63 KB
/
packageTools.py
File metadata and controls
37 lines (29 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# packageTools.py
import json
import os
import zipfile
from typing import Any, Dict, List
def update_version(metadata_file: str, new_version: str, kicad_version: str = "8.0", status: str = "stable") -> None:
"""Update the latest version in the metadata file."""
with open(metadata_file, "r", encoding="utf-8") as f:
metadata: Dict[str, Any] = json.load(f)
# Remove all existing versions and add new one
metadata["versions"] = [{"version": new_version, "status": status, "kicad_version": kicad_version}]
with open(metadata_file, "w", encoding="utf-8") as f:
json.dump(metadata, f, indent=4)
def create_zip_archive(output_zip_file: str, files_and_dirs: List[str], min_size_bytes: int = 128) -> None:
"""Create a ZIP archive excluding files <= specified size in bytes."""
with zipfile.ZipFile(output_zip_file, "w", zipfile.ZIP_DEFLATED, compresslevel=9) as zip_file:
for path in files_and_dirs:
if os.path.isdir(path):
for root, _, files in os.walk(path):
for filename in files:
file_path = os.path.join(root, filename)
if _should_include(file_path, min_size_bytes):
rel_path = os.path.relpath(file_path, start=".")
zip_file.write(file_path, rel_path)
elif _should_include(path, min_size_bytes):
zip_file.write(path)
def _should_include(file_path: str, min_size: int) -> bool:
"""Helper to validate if a file should be included in the archive."""
return os.path.isfile(file_path) and os.path.getsize(file_path) > min_size