-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_release.py
More file actions
executable file
·145 lines (116 loc) · 4.25 KB
/
build_release.py
File metadata and controls
executable file
·145 lines (116 loc) · 4.25 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
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
143
144
145
#!/usr/bin/env python3
import re
import os
import zipfile
import subprocess
import sys
import shutil
# --- Configuration ---
MANIFEST_PATH = "manifest"
DIRS_TO_ZIP = ["components", "images", "source"]
FILES_TO_ZIP = ["manifest"]
ARCHIVE_DIR = "_builds_archive"
# ---------------------
def check_clean_git_state():
"""Aborts if there are uncommitted changes in the repo."""
result = subprocess.run(
["git", "status", "--porcelain"],
capture_output=True,
text=True,
check=True
)
if result.stdout.strip():
print("ERROR: Working directory is not clean.")
print("You have uncommitted changes:")
print(result.stdout)
print("Please commit or stash your changes before releasing.")
sys.exit(1)
def get_current_version(content):
major = re.search(r'^major_version=(\d+)', content, re.MULTILINE).group(1)
minor = re.search(r'^minor_version=(\d+)', content, re.MULTILINE).group(1)
build = re.search(r'^build_version=(\d+)', content, re.MULTILINE).group(1)
return int(major), int(minor), int(build)
def update_manifest():
with open(MANIFEST_PATH, 'r') as f:
content = f.read()
major, minor, build = get_current_version(content)
new_build = build + 1
new_content = re.sub(
r'^build_version=\d+',
f'build_version={new_build}',
content,
flags=re.MULTILINE
)
with open(MANIFEST_PATH, 'w') as f:
f.write(new_content)
version_str = f"{major}.{minor}.{new_build}"
print(f"[-] Version bumped: {major}.{minor}.{build} -> {version_str}")
return version_str
def create_zip(version_str):
zip_name = f"Security_Spy_Viewer_v{version_str}.zip"
with zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED) as zf:
for file in FILES_TO_ZIP:
zf.write(file)
for directory in DIRS_TO_ZIP:
for root, dirs, files in os.walk(directory):
for file in files:
if file.startswith('.'): continue
file_path = os.path.join(root, file)
zf.write(file_path)
print(f"[-] Created Artifact: {zip_name}")
return zip_name
def git_commit_and_push(version_str):
subprocess.run(["git", "add", MANIFEST_PATH], check=True)
msg = f"Build Release v{version_str}"
subprocess.run(["git", "commit", "-m", msg], check=True)
print(f"[-] Git Commit completed: {msg}")
print("[-] Pushing to remote...")
subprocess.run(["git", "push"], check=True)
def create_github_release(version_str, zip_name):
tag = f"v{version_str}"
title = f"{tag} Release"
notes = "release"
cmd = [
"gh", "release", "create", tag, zip_name,
"--title", title,
"--notes", notes
]
print(f"[-] Creating GitHub Release {tag}...")
subprocess.run(cmd, check=True)
def cleanup_and_archive():
if not os.path.exists(ARCHIVE_DIR):
os.makedirs(ARCHIVE_DIR)
print(f"[-] Created archive directory: {ARCHIVE_DIR}")
# Move all zip files starting with the project name
count = 0
for file in os.listdir('.'):
if file.endswith('.zip') and file.startswith("Security_Spy_Viewer_v"):
shutil.move(file, os.path.join(ARCHIVE_DIR, file))
count += 1
print(f"[-] Archived {count} build artifact(s) to {ARCHIVE_DIR}/")
def main():
if not os.path.exists(MANIFEST_PATH):
print(f"Error: {MANIFEST_PATH} not found.")
sys.exit(1)
try:
# 0. Safety Check
check_clean_git_state()
# 1. Bump Version
version_str = update_manifest()
# 2. Generate ZIP
zip_name = create_zip(version_str)
# 3. Commit and Push
git_commit_and_push(version_str)
# 4. Create GitHub Release
create_github_release(version_str, zip_name)
# 5. Archive Artifacts
cleanup_and_archive()
print("\nSUCCESS: Release Published to GitHub and Archived.")
except subprocess.CalledProcessError as e:
print(f"\nFAILED: Subprocess error (Exit Code {e.returncode})")
sys.exit(1)
except Exception as e:
print(f"\nFAILED: {e}")
sys.exit(1)
if __name__ == "__main__":
main()