-
Notifications
You must be signed in to change notification settings - Fork 1
/
__pack_your_addon__.py
56 lines (40 loc) · 1.4 KB
/
__pack_your_addon__.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
import os
from pathlib import Path
import zipfile
import shutil
parent_path = Path(__file__).parent
def get_tg_dir() -> Path:
tg_dir = parent_path.joinpath(parent_path.name)
if tg_dir.exists():
shutil.rmtree(tg_dir)
tg_dir.mkdir()
return tg_dir
def copy_files() -> Path:
tg_dir = get_tg_dir()
sub_dir = tg_dir.joinpath(parent_path.name)
sub_dir.mkdir()
for file in parent_path.glob('*'):
if file.is_dir():
if file.name == parent_path.name: continue
if file.name.startswith('__') or file.name.startswith('.'): continue
shutil.copytree(file, sub_dir.joinpath(file.name))
elif file.is_file():
if file.name == __file__: continue
shutil.copy(file, sub_dir.joinpath(file.name))
return tg_dir
def zip_dir() -> None:
tg_dir = copy_files()
zip_file = parent_path.joinpath(f'{parent_path.name}.zip')
if zip_file.exists():
os.remove(zip_file)
# utf-8 encoding
with zipfile.ZipFile(zip_file, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) as zip:
for root, dirs, files in os.walk(tg_dir):
for file in files:
zip.write(os.path.join(root, file), arcname=os.path.join(root, file).replace(str(tg_dir), ''))
print(f'Zip file: {zip_file}')
shutil.rmtree(tg_dir)
print('Remove temp dir')
if __name__ == '__main__':
copy_files()
zip_dir()