-
Notifications
You must be signed in to change notification settings - Fork 8
/
buildPlugins.py
140 lines (101 loc) · 4.13 KB
/
buildPlugins.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
import os
import sys
import json
import base64
import shutil
import zipfile
import logging
import importlib
from amiyabot import PluginInstance
from amiyabot.util import temp_sys_path
sys.path += [os.path.dirname(os.path.abspath(__file__)) + '/../']
remote = 'plugins/official'
def build(dist, upload=False):
if os.path.exists(dist):
shutil.rmtree(dist)
os.makedirs(dist)
profiles = []
plugin_dir = []
for root, dirs, files in os.walk(os.path.dirname(os.path.abspath(__file__)) + '/src'):
is_plugin = False
for exist in plugin_dir:
if exist in root:
is_plugin = True
if is_plugin:
continue
for item in dirs:
plugin: str = os.path.join(root, item)
if not os.path.exists(f'{plugin}/__init__.py') or '__pycache__' in plugin:
continue
print(f'packaging... {plugin}')
path_split = plugin.replace('\\', '/').split('/')
parent_dir = os.path.abspath('/'.join(path_split[:-1]))
with temp_sys_path(parent_dir):
with temp_sys_path(plugin):
module = importlib.import_module(path_split[-1])
if not hasattr(module, 'bot'):
continue
instance: PluginInstance = getattr(module, 'bot')
profile = {
'name': instance.name,
'version': instance.version,
'plugin_id': instance.plugin_id,
'plugin_type': instance.plugin_type,
'description': instance.description,
'document': instance.description,
'logo': '',
}
doc = instance.document
if doc and os.path.isfile(doc):
with open(doc, mode='r', encoding='utf-8') as doc_file:
profile['document'] = doc_file.read()
else:
profile['document'] = doc
if os.path.exists(os.path.join(plugin, 'logo.png')):
with open(os.path.join(plugin, 'logo.png'), mode='rb') as logo:
profile['logo'] = 'data:image/png;base64,' + base64.b64encode(logo.read()).decode()
profiles.append(profile)
plugin_dir.append(plugin)
package = f'{dist}/{instance.plugin_id}-{instance.version}.zip'
with zipfile.ZipFile(package, 'w') as pack:
for plugin_root, _, plugin_files in os.walk(plugin):
for index, filename in enumerate(plugin_files):
target = str(os.path.join(plugin_root, filename))
if '__pycache__' in target:
continue
pack.write(target, target.replace(plugin, '').strip('\\'))
print(f'\t --> {package}')
with open(f'{dist}/plugins.json', mode='w', encoding='utf-8') as file:
file.write(json.dumps(profiles, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': ')))
if upload:
upload_list = []
for root, _, files in os.walk(dist):
upload_list += [(os.path.join(root, file), f'{remote}/{file}') for file in files]
upload_all_plugins(upload_list)
def upload_all_plugins(upload_list: list):
from build.uploadFile import COSUploader
secret_id = os.environ.get('SECRETID')
secret_key = os.environ.get('SECRETKEY')
cos = COSUploader(secret_id, secret_key, logger_level=logging.ERROR)
cos.delete_folder(remote)
for item in upload_progress(upload_list):
cos.upload_file(*item)
def upload_progress(upload_list: list):
count = len(upload_list)
def print_bar(name: str = ''):
p = int(curr / count * 100)
block = int(p / 4)
progress_line = '=' * block + ' ' * (25 - block)
msg = f'uploading...{name}\nprogress: [{progress_line}] {curr}/{count} ({p}%)'
print('\r', end='')
print(msg, end='')
sys.stdout.flush()
curr = 0
print_bar()
for item in upload_list:
yield item
curr += 1
print_bar(item[1])
print()
if __name__ == '__main__':
build('plugins')