-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathmake_plugin
executable file
·52 lines (44 loc) · 1.56 KB
/
make_plugin
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
#!/usr/bin/python3
import os
import sys
import hashlib
import json
import zipfile
import zipimport
# todo: use version number
if len(sys.argv) != 2:
print(f"usage: {os.path.basename(__file__)} <plugin_directory>", file=sys.stderr)
sys.exit(1)
source_dir = sys.argv[1] # where the plugin source code is
if source_dir.endswith('/'):
source_dir = source_dir[:-1]
plugin_name = os.path.basename(source_dir)
dest_dir = os.getcwd()
zip_path = os.path.join(dest_dir, plugin_name + '.zip')
# remove old zipfile
if os.path.exists(zip_path):
os.unlink(zip_path)
# create zipfile
print('creating', zip_path)
with zipfile.ZipFile(zip_path, 'w') as zip_object:
for folder_name, sub_folders, file_names in os.walk(source_dir):
for filename in file_names:
file_path = os.path.join(folder_name, filename)
dest_path = os.path.join(plugin_name, os.path.relpath(folder_name, source_dir), os.path.basename(file_path))
zip_object.write(file_path, dest_path)
print('added', dest_path)
# read version
try:
with open(os.path.join(source_dir, 'manifest.json'), 'r') as f:
manifest = json.load(f)
version = manifest.get('version')
except FileNotFoundError:
raise Exception(f"plugin doesn't contain manifest.json")
if version:
versioned_plugin_name = plugin_name + '-' + version + '.zip'
zip_path_with_version = os.path.join(dest_dir, versioned_plugin_name)
# rename zip file
os.rename(zip_path, zip_path_with_version)
print(f'Created {zip_path_with_version}')
else:
print(f'Created {zip_path}')