-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathutils.py
137 lines (109 loc) · 4.12 KB
/
utils.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
import os
import re
import sys
import json
import shutil
import fnmatch
import zipfile
import subprocess
from pathlib import Path
from shutil import rmtree, move as move_dir
import urllib.error, urllib.request
class ProgressBar(object):
def __init__(self, total, prefix="", size=60, file=sys.stdout):
self.total = total
self.prefix = prefix
self.size = 60
self.file = file
self.total_count = 0
def update(self, count):
self.total_count += count
x = int(self.size * self.total_count / self.total)
self.file.write(
"%s[%s%s] %i/%i\r"
% (
self.prefix,
"#" * x,
"." * (self.size - x),
self.total_count,
self.total,
)
)
self.file.flush()
if self.total_count == self.total:
print()
def match_files(folder, ignores=[]):
excludes = r"|".join([fnmatch.translate(x) for x in ignores]) or r"$."
all_files = []
for root, dirs, files in os.walk(folder):
dirs[:] = [os.path.join(root, d) for d in dirs]
dirs[:] = [d for d in dirs if not re.match(excludes, d)]
# exclude/include files
files = [os.path.join(root, f) for f in files]
files = [f for f in files if not re.match(excludes, f)]
all_files.extend(files)
return all_files
def error(msg):
print(msg)
sys.exit(1)
def format_version(tuple_version):
return "v" + ".".join(map(str, tuple_version))
def get_latest_version(org, repo, version_param_length=2):
url = f"https://api.github.com/repos/{org}/{repo}/releases/latest"
try:
body = urllib.request.urlopen(url).read()
except urllib.error.HTTPError as e:
body = e.read()
data = json.loads(body.decode("UTF-8"))
if "tag_name" not in data:
version = ()
else:
version = tuple(map(int, re.sub(r"[^0-9\.]", "", data["tag_name"]).split(".")))
version += (0,) * (version_param_length - len(version))
return version
APP_NAME = "io-cesium-ion"
def package(module_dir, license_path, ignores=None, app_name=APP_NAME):
script_path = os.path.join(module_dir, "__init__.py")
print("Identifying version...")
with open(script_path, "r") as init_script:
bl_info_raw = re.findall(r"bl_info\s*=\s*(\{[^}]+\})", init_script.read())
if len(bl_info_raw) != 1:
error("Unable to find bl_info expected 1 found " + f"{len(bl_info_raw)}")
bl_info = eval(bl_info_raw[0])
local_version = bl_info["version"]
released_version = get_latest_version(
"AnalyticalGraphicsInc",
"ion-blender-exporter",
version_param_length=len(local_version),
)
if local_version <= released_version:
error(
f'"Local Version" ({format_version(local_version)}) in '
+ '"bl_info" must be newer than the last "Release Version" '
+ f"({format_version(released_version)})"
)
package_files = match_files(module_dir, ignores=ignores)
bar = ProgressBar(len(package_files), prefix="Zipping ")
zip_file_name = f"{app_name}-{format_version(local_version)}.zip"
zipf = zipfile.ZipFile(zip_file_name, "w", zipfile.ZIP_DEFLATED)
zip_path = ""
for file_path in package_files:
zip_rel_path = os.path.relpath(file_path, module_dir)
zipf.write(file_path, os.path.join(app_name, zip_rel_path))
bar.update(1)
zipf.write(license_path, os.path.join(app_name, "LICENSE"))
print(f"Zip written to {zip_file_name}")
zipf.close()
if __name__ == "__main__":
command = None
if len(sys.argv) >= 2:
command = sys.argv[1].upper()
script_dir = os.path.dirname(os.path.realpath(__file__))
module_dir = os.path.join(script_dir, APP_NAME)
if command == "PACKAGE":
with open(os.path.join(script_dir, ".packignore")) as packignore:
ignores = [line.rstrip("\n") for line in packignore.readlines()]
package(module_dir, os.path.join(script_dir, "LICENSE"), ignores)
else:
print("python3 utils.py arg")
print("\t package - build a zip for distribution")