Skip to content

Commit c761da1

Browse files
committed
[platform] drop python formatting-only churn from platform commit
1 parent 08aa224 commit c761da1

3 files changed

Lines changed: 91 additions & 138 deletions

File tree

build_scripts/file_utilities.py

Lines changed: 51 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import sys
2727
from pathlib import Path
2828

29-
3029
def install_and_import(package):
3130
try:
3231
importlib.import_module(package)
@@ -36,20 +35,13 @@ def install_and_import(package):
3635
finally:
3736
globals()[package] = importlib.import_module(package)
3837

39-
40-
install_and_import("tqdm")
41-
install_and_import("requests")
42-
install_and_import("tenacity")
38+
install_and_import('tqdm')
39+
install_and_import('requests')
40+
install_and_import('tenacity')
4341

4442
import requests
4543
from tqdm import tqdm
46-
from tenacity import (
47-
retry,
48-
stop_after_attempt,
49-
wait_exponential,
50-
retry_if_exception_type,
51-
)
52-
44+
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
5345

5446
def calculate_file_hash(file_path):
5547
hash_func = hashlib.new("sha256")
@@ -58,11 +50,10 @@ def calculate_file_hash(file_path):
5850
hash_func.update(chunk)
5951
return hash_func.hexdigest()
6052

61-
6253
def download_file(url, destination, expected_hash, max_retries=3, chunk_size=1024):
6354
"""
6455
Download a file with retry and resume support.
65-
56+
6657
Args:
6758
url (str): URL of the file to download.
6859
destination (str): Local path to save the file.
@@ -72,106 +63,94 @@ def download_file(url, destination, expected_hash, max_retries=3, chunk_size=102
7263
"""
7364
# Ensure destination directory exists
7465
os.makedirs(os.path.dirname(destination), exist_ok=True)
75-
66+
7667
# Check if file exists and get its size
7768
current_size = 0
7869
if os.path.exists(destination):
7970
if calculate_file_hash(destination) == expected_hash:
80-
print(
81-
f"File {destination} already exists with the correct hash. Skipping download."
82-
)
71+
print(f"File {destination} already exists with the correct hash. Skipping download.")
8372
return True
8473
current_size = os.path.getsize(destination)
8574

8675
@retry(
8776
stop=stop_after_attempt(max_retries),
8877
wait=wait_exponential(multiplier=1, min=4, max=10),
89-
retry=retry_if_exception_type(
90-
(requests.ConnectionError, requests.Timeout, requests.HTTPError)
91-
),
92-
reraise=True,
78+
retry=retry_if_exception_type((requests.ConnectionError, requests.Timeout, requests.HTTPError)),
79+
reraise=True
9380
)
9481
def download_with_retry():
9582
nonlocal current_size
96-
headers = {"Range": f"bytes={current_size}-"} if current_size > 0 else {}
97-
mode = "ab" if current_size > 0 else "wb"
98-
83+
headers = {'Range': f'bytes={current_size}-'} if current_size > 0 else {}
84+
mode = 'ab' if current_size > 0 else 'wb'
85+
9986
print(f"\nDownloading {destination} (Starting from: {current_size:,} bytes)...")
100-
87+
10188
response = None
10289
try:
10390
response = requests.get(url, stream=True, headers=headers, timeout=30)
10491
response.raise_for_status() # Raise exception for bad status codes
105-
92+
10693
# Determine total_size
10794
total_size = None
108-
if "content-length" in response.headers:
109-
remaining = int(response.headers["content-length"])
95+
if 'content-length' in response.headers:
96+
remaining = int(response.headers['content-length'])
11097
else:
11198
remaining = None
112-
99+
113100
# Parse content-range if available
114-
if "content-range" in response.headers:
115-
cr = response.headers["content-range"]
116-
total_str = cr.rsplit("/", 1)[-1]
101+
if 'content-range' in response.headers:
102+
cr = response.headers['content-range']
103+
total_str = cr.rsplit('/', 1)[-1]
117104
if total_str.isdigit():
118105
total_size = int(total_str)
119-
106+
120107
# If content-length available but no total from range, compute it
121108
if remaining is not None and total_size is None:
122109
total_size = current_size + remaining
123-
110+
124111
# Check if server supports range requests
125112
if current_size > 0 and response.status_code != 206:
126-
print(
127-
"Server does not support range requests. Restarting download from scratch..."
128-
)
113+
print("Server does not support range requests. Restarting download from scratch...")
129114
response.close()
130115
# Truncate the file to zero
131-
open(destination, "wb").close()
116+
open(destination, 'wb').close()
132117
current_size = 0
133-
raise requests.RequestException(
134-
"Restarting due to lack of range support"
135-
) # Trigger retry to restart
136-
118+
raise requests.RequestException("Restarting due to lack of range support") # Trigger retry to restart
119+
137120
# Print total size if known
138121
if total_size is not None:
139122
print(f"Total size detected: {total_size:,} bytes")
140-
123+
141124
# Now create tqdm with known total or None
142-
t = tqdm(total=total_size, initial=current_size, unit="iB", unit_scale=True)
143-
125+
t = tqdm(total=total_size, initial=current_size, unit='iB', unit_scale=True)
126+
144127
# Open file and download
145128
with open(destination, mode) as f:
146129
for chunk in response.iter_content(chunk_size):
147130
if chunk: # Filter out keep-alive chunks
148131
f.write(chunk)
149132
t.update(len(chunk))
150-
133+
151134
except requests.RequestException as e:
152135
print(f"Download failed: {e}. Retrying...")
153136
raise
154137
finally:
155138
if response:
156139
response.close()
157140
t.close()
158-
141+
159142
# Verify file size if known
160143
downloaded_size = os.path.getsize(destination)
161144
if total_size is not None and downloaded_size != total_size:
162-
print(
163-
f"Download incomplete: {downloaded_size:,} of {total_size:,} bytes downloaded."
164-
)
145+
print(f"Download incomplete: {downloaded_size:,} of {total_size:,} bytes downloaded.")
165146
raise requests.RequestException("Incomplete download")
166-
147+
167148
# Verify hash
168149
downloaded_hash = calculate_file_hash(destination)
169150
if downloaded_hash != expected_hash:
170-
print(
171-
f"Hash mismatch. Expected: {expected_hash}, Got: {downloaded_hash}. Downloaded file is corrupted."
172-
)
151+
print(f"Hash mismatch. Expected: {expected_hash}, Got: {downloaded_hash}. Downloaded file is corrupted.")
173152
raise requests.RequestException("Hash mismatch")
174-
153+
175154
print(f"Successfully downloaded {destination}.")
176155
return True
177156

@@ -181,42 +160,34 @@ def download_with_retry():
181160
print(f"Failed to download {destination} after {max_retries} attempts: {e}")
182161
return False
183162

184-
185163
def extract_archive(archive_path, destination_path):
186164
import platform
187165

188-
is_windows = platform.system() == "Windows"
166+
is_windows = platform.system() == 'Windows'
189167

190168
if is_windows:
191-
current_dir_7z = Path("7z.exe")
169+
# Check if 7z.exe exists locally
170+
current_dir_7z = Path("7z.exe")
192171
if current_dir_7z.exists():
193172
seven_zip_exe = current_dir_7z
194173
else:
174+
# define the path where 7z.exe should be if not in the current directory
195175
seven_zip_exe = Path("build_scripts") / "7z.exe"
196176
seven_zip_exe = seven_zip_exe.resolve()
197177
else:
198-
seven_zip_exe_path = shutil.which("7z")
178+
seven_zip_exe_path = shutil.which('7z')
199179
if not seven_zip_exe_path:
200-
raise FileNotFoundError(
201-
"The 7z executable was not found. Please install p7zip or 7zip."
202-
)
180+
raise FileNotFoundError("The 7z executable was not found. Please install p7zip or 7zip.")
203181
seven_zip_exe = Path(seven_zip_exe_path)
204182

183+
# check if the 7z executable exists
205184
if not os.path.exists(seven_zip_exe):
206-
raise FileNotFoundError(
207-
f"The 7z executable was not found at {seven_zip_exe}. Please check the path or installation."
208-
)
209-
185+
raise FileNotFoundError(f"The 7z executable was not found at {seven_zip_exe}. Please check the path or installation.")
186+
210187
archive_path_str = str(Path(archive_path).resolve())
211188
destination_path_str = str(Path(destination_path).resolve())
212189

213-
cmd = [
214-
str(seven_zip_exe),
215-
"x",
216-
archive_path_str,
217-
"-o" + destination_path_str,
218-
"-aoa",
219-
]
190+
cmd = [str(seven_zip_exe), 'x', archive_path_str, '-o'+destination_path_str, '-aoa']
220191

221192
print(f"Extracting {archive_path} to {destination_path} using: {seven_zip_exe}")
222193

@@ -227,8 +198,7 @@ def extract_archive(archive_path, destination_path):
227198
print(f"An error occurred while extracting: {e}")
228199
print(f"Error output: {e.stderr}")
229200
raise
230-
231-
201+
232202
def copy(source, destination):
233203
def on_rm_error(func, path, exc_info):
234204
os.chmod(path, stat.S_IWRITE)
@@ -240,21 +210,17 @@ def on_rm_error(func, path, exc_info):
240210
# check if source is a directory or file
241211
if source_path.is_dir():
242212
# if source is a directory, ensure destination is a directory too
243-
dest_path.mkdir(
244-
parents=True, exist_ok=True
245-
) # Create the destination directory if it doesn't exist
246-
print(f'Copying directory "{source_path}" to directory "{dest_path}"...')
213+
dest_path.mkdir(parents=True, exist_ok=True) # Create the destination directory if it doesn't exist
214+
print(f"Copying directory \"{source_path}\" to directory \"{dest_path}\"...")
247215
shutil.rmtree(str(dest_path), onerror=on_rm_error)
248216
shutil.copytree(str(source_path), str(dest_path), dirs_exist_ok=True)
249217
elif source_path.is_file():
250218
# if source is a file, ensure the parent directory of the destination exists
251-
dest_path.parent.mkdir(
252-
parents=True, exist_ok=True
253-
) # Create parent directory if it doesn't exist
219+
dest_path.parent.mkdir(parents=True, exist_ok=True) # Create parent directory if it doesn't exist
254220
target = dest_path if dest_path.is_file() else dest_path / source_path.name
255-
print(f'Copying file "{source_path}" to "{target}"...')
221+
print(f"Copying file \"{source_path}\" to \"{target}\"...")
256222
shutil.copy2(str(source_path), str(target))
257223
else:
258224
print(f"Error: Source '{source_path}' is neither a file nor a directory.")
259225
return False
260-
return True
226+
return True

build_scripts/generate_project_files.py

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,43 @@
4545
},
4646
}
4747

48-
4948
def generate_project_files():
5049
args = list(sys.argv[1:])
5150
if len(args) < 2:
52-
if os.name == "nt":
53-
args = ["vs2026", "vulkan"]
51+
if os.name == 'nt':
52+
args = ['vs2026', 'vulkan']
5453
else:
55-
args = ["gmake2", "vulkan"]
54+
args = ['gmake2', 'vulkan']
5655

57-
action = args[0].strip('"')
58-
is_windows = action.startswith("vs")
56+
# determine if we're using Windows or another platform
57+
action = args[0].strip('\"')
58+
is_windows = action.startswith("vs") # Assuming 'vs' prefix for Visual Studio
5959

60+
# construct the command, stripping any surrounding quotes from arguments
6061
if is_windows:
6162
premake_exe = Path.cwd() / "build_scripts" / "premake5.exe"
6263
if not premake_exe.exists():
63-
raise FileNotFoundError(
64-
"premake5.exe executable not found in build_scripts/."
65-
)
64+
raise FileNotFoundError("premake5.exe executable not found in build_scripts/.")
6665
else:
67-
premake_from_path = shutil.which("premake5")
66+
premake_from_path = shutil.which('premake5')
6867
if premake_from_path:
6968
premake_exe = Path(premake_from_path)
7069
else:
7170
premake_exe = Path.cwd() / "build_scripts" / "premake5"
7271

7372
if not premake_exe.exists():
74-
raise FileNotFoundError(
75-
"premake5 executable not found in PATH or build_scripts/."
76-
)
77-
73+
raise FileNotFoundError("premake5 executable not found in PATH or build_scripts/.")
7874
premake_lua = Path("build_scripts") / "premake.lua"
79-
platform = args[1].strip('"')
75+
76+
# remove quotes if they exist around sys.argv[1] and sys.argv[2]
77+
action = sys.argv[1].strip('"')
78+
platform = sys.argv[2].strip('"')
79+
80+
# construct the command as a string with quoted paths
8081
cmd = f'"{str(premake_exe)}" --file="{str(premake_lua)}" "{action}" "{platform}"'
8182

8283
print("Running command:", cmd)
83-
84+
8485
try:
8586
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
8687
print(result.stdout)
@@ -91,48 +92,41 @@ def generate_project_files():
9192
sys.exit(1)
9293
except Exception as e:
9394
print(f"An unexpected error occurred: {e}")
95+
input("\nPress Enter to exit...")
9496
sys.exit(1)
9597

96-
9798
def main():
9899
is_ci = "ci" in sys.argv
99-
100+
100101
print("\n1. Create binaries folder with the required data files...\n")
101102
file_utilities.copy("data", paths["binaries"]["data"])
102103
file_utilities.copy(Path("build_scripts") / "file_utilities.py", "binaries")
103104

104-
if os.name == "nt":
105+
if os.name == 'nt':
105106
file_utilities.copy(Path("build_scripts") / "7z.exe", "binaries")
106107
file_utilities.copy(Path("build_scripts") / "7z.dll", "binaries")
107108

108109
print("\n2. Download and extract libraries...")
109-
library_url = "https://www.dropbox.com/scl/fi/bqm5ds2jgal9i4xqlq9p7/libraries.7z?rlkey=2tjj312w4jd0sxavdrpzt7aru&st=kkutkjir&dl=1"
110-
library_expected_hash = (
111-
"d84b0e2c9bf3622f48f3a0f7b3a00d0f9cf11925b75067879923081516eedf15"
112-
)
113-
library_destination = Path("third_party") / "libraries" / "libraries.7z"
114-
file_utilities.download_file(
115-
library_url, str(library_destination), library_expected_hash
116-
)
117-
file_utilities.extract_archive(
118-
str(library_destination), str(Path("third_party") / "libraries")
119-
)
120-
110+
library_url = 'https://www.dropbox.com/scl/fi/bqm5ds2jgal9i4xqlq9p7/libraries.7z?rlkey=2tjj312w4jd0sxavdrpzt7aru&st=kkutkjir&dl=1'
111+
library_expected_hash = 'd84b0e2c9bf3622f48f3a0f7b3a00d0f9cf11925b75067879923081516eedf15'
112+
library_destination = Path("third_party") / "libraries" / "libraries.7z"
113+
file_utilities.download_file(library_url, str(library_destination), library_expected_hash)
114+
file_utilities.extract_archive(str(library_destination), str(Path("third_party") / "libraries"))
115+
121116
print("3. Copying required DLLs to the binary directory...")
122-
if os.name == "nt":
117+
if os.name == 'nt':
123118
for lib in paths["third_party_libs"].values():
124119
file_utilities.copy(lib, Path("binaries"))
125120
else:
126121
print("Skipping DLL copy on non-Windows platform.")
127122

128123
print("\n4. Generate project files...\n")
129124
generate_project_files()
130-
125+
131126
if not is_ci:
132127
input("\nPress any key to continue...")
133-
128+
134129
sys.exit(0)
135130

136-
137131
if __name__ == "__main__":
138-
main()
132+
main()

0 commit comments

Comments
 (0)