-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_ladder_zip.py
269 lines (221 loc) · 9.06 KB
/
create_ladder_zip.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
"""
Zips the relevant files and directories so that Bot can be updated
to ladder or tournaments.
TODO: check all files and folders are present before zipping
"""
import os
import platform
import shutil
import site
import zipfile
from os import path, remove, walk
from subprocess import Popen, run
from typing import Dict, List, Tuple
import yaml
MY_BOT_NAME: str = "MyBotName"
ZIPFILE_NAME: str = "bot.zip"
CONFIG_FILE: str = "config.yml"
ZIP_FILES: List[str] = [
"config.yml",
"config.yaml",
"ladder.py",
"run.py",
"terran_builds.yml",
"terran_builds.yaml",
"protoss_builds.yml",
"protoss_builds.yaml",
"zerg_builds.yml",
"zerg_builds.yaml",
]
if platform.system() == "Windows":
FILETYPES_TO_IGNORE: Tuple = (".c", ".so", "pyx", "pyi")
ROOT_DIRECTORY = "./"
else:
FILETYPES_TO_IGNORE: Tuple = (".c", ".pyd", "pyx", "pyi")
ROOT_DIRECTORY = "./"
ZIP_DIRECTORIES: Dict[str, Dict] = {
"bot": {"zip_all": True, "folder_to_zip": "bot"},
"ares-sc2": {"zip_all": True, "folder_to_zip": ""},
"python-sc2": {"zip_all": False, "folder_to_zip": "sc2"},
# "sc2_helper": {"zip_all": True, "folder_to_zip": "sc2_helper"},
"SC2MapAnalysis": {"zip_all": False, "folder_to_zip": "map_analyzer"},
"cython-extensions-sc2": {"zip_all": False, "folder_to_zip": "cython_extensions"},
}
def zip_dir(dir_path, zip_file):
"""
Will walk through a directory recursively and add all folders and files to zipfile
@param dir_path:
@param zip_file:
@return:
"""
for root, _, files in walk(dir_path):
if "ares-sc2/build" in root or "ares-sc2/dist" in root:
continue
for file in files:
if file.lower().endswith(FILETYPES_TO_IGNORE):
continue
zip_file.write(
path.join(root, file),
path.relpath(path.join(root, file), path.join(dir_path, "..")),
)
def zip_files_and_directories(zipfile_name: str) -> None:
"""
@return:
"""
path_to_zipfile = path.join(ROOT_DIRECTORY, zipfile_name)
# if the zip file already exists remove it
if path.isfile(path_to_zipfile):
remove(path_to_zipfile)
# create a new zip file
zip_file = zipfile.ZipFile(path_to_zipfile, "w", zipfile.ZIP_DEFLATED)
# write directories to the zipfile
for directory, values in ZIP_DIRECTORIES.items():
if values["zip_all"]:
zip_dir(path.join(ROOT_DIRECTORY, directory), zip_file)
else:
path_to_dir = path.join(ROOT_DIRECTORY, directory, values["folder_to_zip"])
zip_dir(path_to_dir, zip_file)
# write individual files
for single_file in ZIP_FILES:
_path: str = path.join(ROOT_DIRECTORY, single_file)
if path.isfile(_path):
zip_file.write(_path, single_file)
# close the zip file
zip_file.close()
def get_library_from_site_packages(library_name, project_directory):
# Find the site packages directory
site_packages_dir = site.getsitepackages()[0]
# Construct the library path
library_path = os.path.join(site_packages_dir, "Lib", "site-packages", library_name)
# Check if the library path exists
if not os.path.exists(library_path):
raise ValueError(f"Library '{library_name}' not found in site packages.")
# Determine the destination directory in the zip file
destination_directory = os.path.join(project_directory, library_name)
# Remove the destination directory if it already exists
if os.path.exists(destination_directory):
shutil.rmtree(destination_directory)
# Copy the library directory into the project directory
shutil.copytree(library_path, destination_directory)
def check_git_status():
"""
Make sure the branch is master and has no uncommitted changes.
Not currently used
@return:
"""
difference = run("git diff", capture_output=True, text=True)
branch_name = run("git rev-parse --abbrev-ref HEAD", capture_output=True, text=True)
assert not difference.stdout, "Uncommitted changes are present"
assert branch_name.stdout.strip() == "master", "This is not the master branch"
def check_config_values():
"""
Make sure debug is False.
"""
config_path: str = path.join(ROOT_DIRECTORY, CONFIG_FILE)
if path.isfile(config_path):
with open(path.join(ROOT_DIRECTORY, CONFIG_FILE), "r") as f:
config = yaml.safe_load(f)
assert not config["Debug"], "Debug is not False"
def get_zipfile_name() -> str:
"""Attempt to get bot name from config."""
__user_config_location__: str = path.abspath(".")
user_config_path: str = path.join(__user_config_location__, CONFIG_FILE)
zipfile_name = ZIPFILE_NAME
# attempt to get race and bot name from config file if they exist
if path.isfile(user_config_path):
with open(user_config_path) as config_file:
config: dict = yaml.safe_load(config_file)
if MY_BOT_NAME in config:
zipfile_name = f"{config[MY_BOT_NAME]}.zip"
return zipfile_name
def on_error(func, path, exc_info):
"""
Error handler for ``shutil.rmtree``.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error.
Usage : ``shutil.rmtree(path, onerror=onerror)``
"""
import stat
# Is the error an access error?
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
if __name__ == "__main__":
print("Cloning python-sc2...")
destination_directory = os.path.join("../", "python-sc2")
if os.path.exists(destination_directory):
shutil.rmtree(destination_directory, ignore_errors=False, onerror=on_error)
# clone python-sc2
run("git clone https://github.com/august-k/python-sc2", shell=True)
# clone map-analyzer
run("git clone https://github.com/spudde123/SC2MapAnalysis", shell=True)
# cython extensions
run("git clone https://github.com/AresSC2/cython-extensions-sc2", shell=True)
run("cd cython-extensions-sc2 && poetry build", shell=True)
# clone sc2-helper
# run("git clone https://github.com/danielvschoor/sc2-helper", shell=True)
# # install rust build tools
# run("curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh", shell=True)
# # activate Rust
# run("source $HOME/.cargo/env", shell=True)
# # compile rust code
# run("cd sc2-helper && cargo build --release", shell=True)
# run("cd ..", shell=True)
"""
Move the sc2 helper binary to correct place
"""
# source_file = "sc2-helper/target/release/libsc2_helper.so"
# # Define the destination directory path
# destination_directory = "sc2-helper/sc2_helper/"
# # Define the new name for the file
# new_file_name = "sc2_helper.so"
# # Combine the destination directory path and the new file name
# new_file_path = os.path.join(destination_directory, new_file_name)
# # Move the file to the destination directory with the new name
# shutil.move(source_file, new_file_path)
# print structure out for debugging purposes
current_directory = os.getcwd()
print("Current Directory:", current_directory)
# List all files and directories in the current directory
for item in os.listdir(current_directory):
item_path = os.path.join(current_directory, item)
if os.path.isdir(item_path):
print("Directory:", item)
elif os.path.isfile(item_path):
print("File:", item)
# get name of bot from config if possible (otherwise use default name)
# zipfile_name = get_zipfile_name()
zipfile_name = ZIPFILE_NAME
print("Setting up poetry environment...")
# ensure env is setup and dependencies are installed
p = Popen(["poetry", "install"], cwd=f"{ROOT_DIRECTORY}")
# makes the process wait, otherwise files get zipped before compile is complete
p.communicate()
p_status = p.wait()
# compile the cython code
print("Compiling cython code...")
p = Popen(["poetry", "build"], cwd=f"{ROOT_DIRECTORY}ares-sc2")
# makes the process wait, otherwise files get zipped before compile is complete
p.communicate()
p_status = p.wait()
# at the moment -> ensure debug=False
print("Checking config values...")
check_config_values()
print("Copying sc2 folder from site packages...")
print(f"Zipping files and directories to {zipfile_name}...")
# copy everything we need into a zip file
zip_files_and_directories(zipfile_name)
print(f"Cleaning up...")
destination_directory = os.path.join("./", "python-sc2")
if os.path.exists(destination_directory):
shutil.rmtree(destination_directory, onerror=on_error)
destination_directory = os.path.join("./", "sc2-helper")
if os.path.exists(destination_directory):
shutil.rmtree(destination_directory, onerror=on_error)
destination_directory = os.path.join("./", "SC2MapAnalysis")
if os.path.exists(destination_directory):
shutil.rmtree(destination_directory, onerror=on_error)
print(f"Ladder zip complete.")