-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathzip-addon.py
More file actions
150 lines (128 loc) · 4.86 KB
/
zip-addon.py
File metadata and controls
150 lines (128 loc) · 4.86 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
146
147
148
149
150
#!/usr/bin/env python
# Author: Christopher Gearhart
# System imports
import os
from os.path import join, split, exists, dirname, realpath, isfile, isdir
import shutil
import argparse
import zipfile
# TO RUN: python zip_addon --beta
# NOTE: only send the resulting zip file to verified customers
# initialize arguments
parser = argparse.ArgumentParser(description="Zip addon")
parser.add_argument(
"--alpha",
help="Bundle as alpha version",
dest="alpha",
action="store_true",
)
parser.add_argument(
"--beta",
help="Bundle as beta version",
dest="beta",
action="store_true",
)
args = parser.parse_args()
# helper functions
def copy_directory(src:str, dest:str):
try:
shutil.copytree(src, dest)
# Directories are the same
except shutil.Error as e:
print(f"Directory not copied. Error: {e}")
# Any error saying that the directory doesn"t exist
except OSError as e:
print(f"Directory not copied. Error: {e}")
def edit_bl_info_warning_message(filepath:str, warning_msg:str=""):
# read lines
with open(filepath, "r") as f:
data = f.readlines()
# make adjustments
for i, line in enumerate(data):
if "\"warning\"" in line:
start_idx = line.find(": \"")
end_idx = line.find("\",")
data[i] = line.replace(line[start_idx + 2:end_idx + 2], f"\"{warning_msg}\",")
break
# write lines
with open(filepath, "w") as f:
f.writelines(data)
def parse_file_for_bl_info_version(filepath:str):
version = ""
line = None
with open(filepath, "r") as f:
while line != "":
line = f.readline()
if "\"version\"" in line:
start_idx = line.find(": (")
end_idx = line.find(")")
version = line[start_idx + 3:end_idx]
break
return version
def get_addon_directory():
return dirname(realpath(__file__))
def parse_git_files_for_branch():
head_dir = join(get_addon_directory(), ".git", "HEAD")
with open(head_dir, "r") as f:
content = f.read().splitlines()
for line in content:
if line[0:4] == "ref:":
return line.partition("refs/heads/")[2]
# main functionality
def main():
current_dir_path = get_addon_directory()
parent_dir_path, current_dir_name = split(current_dir_path)
addon_version = parse_file_for_bl_info_version(join(current_dir_path, "__init__.py"))
branch_name = parse_git_files_for_branch()
demo_version = branch_name.startswith("demo")
master_version = branch_name == "master"
new_dir_name = current_dir_name
assert addon_version != "" and isinstance(addon_version, str)
new_dir_name += "_v" + addon_version.replace(", ", "-")
if demo_version:
new_dir_name += "_demo"
elif master_version:
new_dir_name += "_master"
elif args.alpha:
new_dir_name += "_alpha"
elif args.beta:
new_dir_name += "_beta"
# clear out old destination directories
new_dir_path = join(parent_dir_path, new_dir_name)
if exists(new_dir_path):
shutil.rmtree(new_dir_path)
if exists(f"{new_dir_path}.zip"):
os.remove(f"{new_dir_path}.zip")
# make the destination directory
copy_directory(current_dir_path, new_dir_path)
try:
# make sure the directory contents copied successfully (just checks for the init file)
new_init_filepath = join(new_dir_path, "__init__.py")
assert exists(new_init_filepath)
# adjust bl_info for beta
if demo_version:
edit_bl_info_warning_message(new_init_filepath, "Demo Version - Full version available at the Blender Market!")
elif args.alpha:
edit_bl_info_warning_message(new_init_filepath, "Unstable Alpha release - update to official release when available")
elif args.beta:
edit_bl_info_warning_message(new_init_filepath, "Unstable Beta release - update to official release when available")
else:
edit_bl_info_warning_message(new_init_filepath, "")
# remove unnecessary files/directories
if demo_version:
os.remove(join(new_dir_path, "lib", f"{current_dir_name}_purchase_verification.txt"))
for filename in ("developer-notes.md", "zip-addon.py", "zip_addon.py", "error_log", ".git", ".gitignore", ".github", "__pycache__", f"{current_dir_name}_updater"):
filepath = join(new_dir_path, filename)
if not exists(filepath):
continue
elif isfile(filepath):
os.remove(filepath)
elif isdir(filepath):
shutil.rmtree(filepath)
# zip new directory
shutil.make_archive(new_dir_path, "zip", parent_dir_path, new_dir_name)
print(f"Created new archive: '{split(new_dir_path)[-1]}'")
finally:
# remove new directory
shutil.rmtree(new_dir_path)
main()