This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Packer.py
84 lines (67 loc) · 2.44 KB
/
Packer.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
import os
import json
import shutil
import zipfile
import requests
with open('ReleaseConfig.json', encoding='utf-8') as f:
RELEASE_CONFIG = json.load(f)
PROJECT_NAME = RELEASE_CONFIG['projectName']
PACK_VERSION = RELEASE_CONFIG['packVersion']
VERSION = os.environ['version'] if 'version' in os.environ.keys() else input()
RELEASE_NAME = '-'.join([VERSION, 'CN', PROJECT_NAME, PACK_VERSION])
RELEASE_DIR = os.path.join('temp', RELEASE_NAME)
RESOURCES_PACK_URL = 'https://api.github.com/repos/ShaBaiTianCN/Minecraft-Mod-Language-Package-Not-Included-Language-Package/releases/latest'
def touch_dir(path: str):
"""
Create folder if not exist.
:param path: Folder path.
"""
if not os.path.isdir(path):
os.makedirs(path)
def copy(file_path: str, check_dir: bool = True):
"""
Copy file to release folder.
:param file_path: File path.
:param check_dir: Enable check dir exist.
"""
target_path = os.path.join(RELEASE_DIR, file_path)
if check_dir:
touch_dir(os.path.dirname(target_path))
shutil.copyfile(file_path, target_path)
def main():
# Touch folder
touch_dir(RELEASE_DIR)
# Copy file
for i in RELEASE_CONFIG['files']:
if os.path.isfile(i):
copy(i)
else:
for dirpath, dirnames, filenames in os.walk(i):
for filename in filenames:
copy(os.path.join(dirpath, filename))
# Download resource pack
release_info = requests.get(RESOURCES_PACK_URL).json()['assets'][0]
resourcepack_dir = os.path.join(RELEASE_DIR, 'resourcepacks')
resourcepack_path = os.path.join(resourcepack_dir, '汉化补充资源包.zip')
touch_dir(resourcepack_dir)
with open(resourcepack_path, 'wb') as f:
f.write(requests.get(release_info['browser_download_url']).content)
# Make Zipfile
with zipfile.ZipFile(
f'{RELEASE_DIR}.zip',
mode='w',
compression=zipfile.ZIP_DEFLATED
) as archive:
for dirpath, dirnames, filenames in os.walk(RELEASE_DIR):
for filename in filenames:
file_path = os.path.join(dirpath, filename)
archive.write(
file_path,
arcname=file_path.replace(RELEASE_DIR, '')
)
# Remove folder
shutil.rmtree(RELEASE_DIR)
# Release Info
print(f'::set-output name=release_name::{RELEASE_NAME}')
if __name__ == '__main__':
main()