-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packer.py
72 lines (57 loc) · 2.12 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
import os
import json
import shutil
import requests
with open('PackerConfig.json', encoding='utf-8') as f:
PACKER_CONFIG = json.load(f)
PROJECT_NAME = PACKER_CONFIG['projectName']
PACK_VERSION = PACKER_CONFIG['packVersion']
if os.environ.get('event_name') == 'pull_request_target':
VERSION = os.environ['event_number']
elif os.environ.get('event_name') == 'workflow_dispatch':
VERSION = os.environ['version']
else:
VERSION = input('Version: ')
RELEASE_NAME = '-'.join([VERSION, 'CN', PROJECT_NAME, PACK_VERSION])
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 target folder.
:param file_path: File path.
:param check_dir: Enable check dir exist.
"""
target_path = os.path.join(RELEASE_NAME, 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_NAME)
# Copy file
for i in PACKER_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]
resourcepacks_dir = os.path.join(RELEASE_NAME, 'resourcepacks')
resourcepacks_path = os.path.join(resourcepacks_dir, '汉化补充资源包.zip')
touch_dir(resourcepacks_dir)
with open(resourcepacks_path, 'wb') as f:
f.write(requests.get(release_info['browser_download_url']).content)
# Set release name
if 'GITHUB_OUTPUT' in os.environ.keys():
with open(os.environ['GITHUB_OUTPUT'], 'a') as fh:
print(f'release_name={RELEASE_NAME}', file=fh)
if __name__ == '__main__':
main()