-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
65 lines (50 loc) · 1.76 KB
/
build.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
import os
import sys
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('.') or file.name == 'dist': 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(tg_dir) -> None:
dist = parent_path.joinpath('dist')
if not dist.exists():
dist.mkdir(parents=True)
zip_file = dist.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')
# move zip file to dist
if __name__ == '__main__':
tg_dir = copy_files()
if len(sys.argv) > 1 and sys.argv[1] == "--no-zip":
dist = parent_path.joinpath('dist')
if not dist.exists():
dist.mkdir(parents=True)
shutil.move(tg_dir, dist)
else:
zip_dir(tg_dir)