Skip to content
This repository was archived by the owner on Apr 3, 2026. It is now read-only.

Commit 3648fbe

Browse files
committed
avoiding CI race conditions test
1 parent e6d04df commit 3648fbe

1 file changed

Lines changed: 74 additions & 8 deletions

File tree

setup.py

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,94 @@
4545

4646

4747
def download_raylib(platform, ext):
48-
if not os.path.exists(platform):
48+
"""Download and extract raylib with proper error handling for CI environments."""
49+
import tempfile
50+
import time
51+
52+
# Use a lock file to prevent concurrent downloads
53+
lock_file = platform + ".lock"
54+
max_wait = 60 # seconds
55+
start_time = time.time()
56+
57+
# Wait for any ongoing download to complete
58+
while os.path.exists(lock_file) and (time.time() - start_time) < max_wait:
59+
print(f"Waiting for another process to finish downloading {platform}...")
60+
time.sleep(1)
61+
62+
# Check if already successfully downloaded
63+
success_marker = platform + ".success"
64+
if os.path.exists(success_marker) and os.path.exists(platform):
65+
print(f"{platform} already downloaded successfully")
66+
return
67+
68+
# Clean up any partial downloads
69+
if os.path.exists(platform) and not os.path.exists(success_marker):
70+
print(f"Cleaning up partial download of {platform}")
71+
shutil.rmtree(platform, ignore_errors=True)
72+
73+
# Create lock file
74+
try:
75+
with open(lock_file, "w") as f:
76+
f.write(str(os.getpid()))
77+
78+
# Download to a temporary file first
79+
temp_file = None
4980
try:
5081
print(f"Downloading {platform}{ext}...")
51-
urllib.request.urlretrieve(RAYLIB_URL + platform + ext, platform + ext)
82+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=ext)
83+
urllib.request.urlretrieve(RAYLIB_URL + platform + ext, temp_file.name)
84+
temp_file.close()
85+
86+
# Extract to a temporary directory first
87+
temp_dir = tempfile.mkdtemp()
5288
if ext == ".zip":
53-
with zipfile.ZipFile(platform + ext, "r") as zip_ref:
54-
zip_ref.extractall()
89+
with zipfile.ZipFile(temp_file.name, "r") as zip_ref:
90+
zip_ref.extractall(temp_dir)
91+
else:
92+
with tarfile.open(temp_file.name, "r") as tar_ref:
93+
tar_ref.extractall(temp_dir)
94+
95+
# Move the extracted content to the final location
96+
extracted_dir = os.path.join(temp_dir, platform)
97+
if os.path.exists(extracted_dir):
98+
shutil.move(extracted_dir, platform)
5599
else:
56-
with tarfile.open(platform + ext, "r") as tar_ref:
57-
tar_ref.extractall()
100+
# Sometimes the archive extracts directly without a parent folder
101+
shutil.move(temp_dir, platform)
58102

59-
os.remove(platform + ext)
60-
# Create the include directory if it doesn't exist
103+
# Download rlights.h
61104
include_dir = os.path.join(platform, "include")
62105
os.makedirs(include_dir, exist_ok=True)
63106
urllib.request.urlretrieve(
64107
RLIGHTS_URL, os.path.join(include_dir, "rlights.h")
65108
)
109+
110+
# Mark as successfully downloaded
111+
with open(success_marker, "w") as f:
112+
f.write("success")
113+
66114
print(f"Successfully downloaded and extracted {platform}")
115+
67116
except Exception as e:
68117
print(f"Error downloading {platform}: {e}")
118+
# Clean up on error
119+
if os.path.exists(platform):
120+
shutil.rmtree(platform, ignore_errors=True)
69121
raise
122+
finally:
123+
# Clean up temp files
124+
if temp_file and os.path.exists(temp_file.name):
125+
os.unlink(temp_file.name)
126+
if "temp_dir" in locals() and os.path.exists(temp_dir):
127+
shutil.rmtree(temp_dir, ignore_errors=True)
128+
129+
finally:
130+
# Remove lock file
131+
if os.path.exists(lock_file):
132+
try:
133+
os.unlink(lock_file)
134+
except:
135+
pass
70136

71137

72138
download_raylib("raylib-5.5_webassembly", ".zip")

0 commit comments

Comments
 (0)