-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgenerate_release.py
142 lines (115 loc) · 4.91 KB
/
generate_release.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
from pathlib import Path
from git import Repo
from zipfile import ZipFile, ZIP_DEFLATED
from hashlib import sha256
from itertools import chain
import json, shutil, requests
TARGET_DIRECTORY = Path("release")
DESCRIPTION = """
The pcb2blender workflow lets you create professionally looking product renders of all your
KiCad projects in minutes! Simply export your board as a .pcb3d file in KiCad, import it into
Blender and start creating!
""".replace("\n", " ")
METADATA_CONTACT = {
"name": "Bobbe",
"contact": {
"web": "https://30350n.de/",
"github": "https://github.com/30350n",
"discord": "Bobbe#8552"
},
}
ORIGIN = "https://github.com/30350n/pcb2blender"
METADATA = {
"$schema": "https://go.kicad.org/pcm/schemas/v1",
"name": "pcb2blender",
"description": "Export PCB 3D Models from Pcbnew to Blender",
"description_full": DESCRIPTION,
"identifier": "com.github.30350n.pcb2blender",
"type": "plugin",
"author": METADATA_CONTACT,
"maintainer": METADATA_CONTACT,
"license": "GPL-3.0",
"resources": {
"homepage": ORIGIN,
},
}
def generate_kicad_addon(path, metadata, icon_path=None, extra_files=[]):
repo = Repo()
tags = list(reversed(sorted(repo.tags, key=lambda tag: tag.commit.committed_datetime)))
latest_tag = tags[0]
latest_version, latest_kicad_version, _ = latest_tag.name.split("-")
TARGET_DIRECTORY.mkdir(exist_ok=True)
zip_path = TARGET_DIRECTORY / f"{path.name}_{latest_version[1:].replace('.', '-')}.zip"
with ZipFile(zip_path, mode="w", compression=ZIP_DEFLATED) as zip_file:
plugin_dir = Path("plugins")
for filepath in path.glob("**/*.py"):
zip_file.write(filepath, str(plugin_dir / filepath.relative_to(path)))
for filepath in extra_files:
zip_file.write(path / filepath, plugin_dir / filepath)
if icon_path:
zip_file.write(path / icon_path, "resources/icon.png")
metadata_latest_version = {
"version": latest_version[1:],
"status": "stable",
"kicad_version": latest_kicad_version[1:]
}
metadata["versions"] = [metadata_latest_version]
metadata_json = json.dumps(metadata, indent=4)
zip_file.writestr("metadata.json", metadata_json)
zip_hash_path = Path(f"{str(zip_path)}.sha256")
with open(zip_path, "rb") as file:
zip_hash = sha256(file.read()).hexdigest()
zip_hash_path.write_text(zip_hash)
metadata_dir = TARGET_DIRECTORY / "metadata"
metadata_dir.mkdir(exist_ok=True)
if icon_path:
shutil.copy((path / icon_path), metadata_dir / "icon.png")
metadata_latest_version["download_sha256"] = zip_hash
download_url = f"{ORIGIN}/releases/download/{latest_tag.name}/{zip_path.name}"
metadata_latest_version["download_url"] = download_url
with ZipFile(zip_path, mode="r") as zip_file:
metadata_latest_version["download_size"] = sum(
(info.compress_size for info in zip_file.infolist()))
metadata_latest_version["install_size"] = sum(
(info.file_size for info in zip_file.infolist()))
version_json = json.dumps(metadata_latest_version, indent=4)
(TARGET_DIRECTORY / "version.json").write_text(version_json)
for tag in tags[1:]:
url = f"{ORIGIN}/releases/download/{tag.name}/version.json"
result = requests.get(url)
if result.ok:
metadata["versions"].append(result.json())
else:
print(f"skipping {tag.name}, missing version.json")
metadata_json = json.dumps(metadata, indent=4)
(metadata_dir / "metadata.json").write_text(metadata_json)
def generate_blender_addon(path, extra_files=[]):
repo = Repo()
tags = list(reversed(sorted(repo.tags, key=lambda tag: tag.commit.committed_datetime)))
latest_tag = tags[0]
version, _, blender_version = latest_tag.name.split("-")
TARGET_DIRECTORY.mkdir(exist_ok=True)
zip_path = TARGET_DIRECTORY / f"{path.name}_{version[1:].replace('.', '-')}.zip"
with ZipFile(zip_path, mode="w", compression=ZIP_DEFLATED) as zip_file:
extra_paths = (path / extra for extra in extra_files)
for filepath in chain(path.glob("**/*.py"), extra_paths):
if "site-packages" in str(filepath):
continue
zip_file.write(filepath, filepath.relative_to(path.parent))
zip_hash_path = Path(f"{str(zip_path)}.sha256")
with open(zip_path, "rb") as file:
zip_hash = sha256(file.read()).hexdigest()
zip_hash_path.write_text(zip_hash)
metadata_dir = TARGET_DIRECTORY / "metadata"
metadata_dir.mkdir(exist_ok=True)
if __name__ == "__main__":
generate_kicad_addon(
Path(__file__).parent / "pcb2blender_exporter",
METADATA,
"images/icon.png",
["images/blender_icon_32x32.png"],
)
generate_blender_addon(
Path(__file__).parent / "pcb2blender_importer",
)