-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcompress_util.py
More file actions
62 lines (46 loc) · 1.91 KB
/
compress_util.py
File metadata and controls
62 lines (46 loc) · 1.91 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
import ast
import logging
import os
import zipfile
allowed_file_extensions = {'.py', '.json', '.md', '.toml', 'LICENSE'}
exclude_folders = {'doc', 'venv', '.git', '.idea'}
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
def zipdir(path, ziph: zipfile.ZipFile, zip_subdir_name):
for root, dirs, files in os.walk(path):
root_path = str(root)
if any(root_path.__contains__(folder_name) for folder_name in exclude_folders):
continue
for file in files:
if str(file).startswith('.') or not any(file.endswith(ext) for ext in allowed_file_extensions):
log.debug('Skipping {}'.format(str(file)))
continue
orig_hier = os.path.join(root, file)
arc_hier = os.path.join(zip_subdir_name, orig_hier)
ziph.write(orig_hier, arc_hier)
def generate_zip_filename(addon_name):
major, minor, patch = get_addon_version('__init__.py')
return '{}-{}-{}-{}.zip'.format(addon_name, major, minor, patch)
def get_addon_version(init_path):
with open(init_path, 'r') as f:
node = ast.parse(f.read())
n: ast.Module
for n in ast.walk(node):
for b in n.body:
if isinstance(b, ast.Assign) and isinstance(b.value, ast.Dict) and (
any(t.id == 'bl_info' for t in b.targets)):
bl_info_dict = ast.literal_eval(b.value)
return bl_info_dict['version']
raise ValueError('Cannot find bl_info')
def zip_main(addon_name):
filename = generate_zip_filename(addon_name)
try:
zipf = zipfile.ZipFile(filename, 'w', zipfile.ZIP_DEFLATED)
zipdir('.', zipf, addon_name)
zipf.close()
log.info('Successfully created zip file: {}'.format(filename))
except Exception as e:
log.error('Failed to create {}: {}'.format(filename, e))
exit(1)
if __name__ == '__main__':
zip_main('blint')