Files
RedScarfEngine/prepare_env.py
T

86 lines
2.9 KiB
Python

#!/usr/bin/python
# !WARNING! When run, this script will create prepare.lock file, which will indicate that the script has been run before.
# This is used for automation with CMake, which will run this script if and only if this file does not exists.
# If you want to run this script again, remove this file manually first.
# Script used for preparing build envirionment. I cannot use submodules, because I cannot specify commit/tag
# in submodule config. What's more, preparing vulkan SDK is complicated, so a custom scrip is needed for that.
import io
import re
import git
import os
import requests
import subprocess
REPO_DIR = os.path.join(os.getcwd(), "third_party")
class Dependency:
def __init__(self, name, file: io.TextIOWrapper):
self.name = name
while 1:
line = file.readline().split("=", 1)
if len(line) > 1:
match line[0].strip():
case "git_url":
self.git_url = line[1].strip()
case "dl_url":
self.dl_url = line[1].strip()
case "tag":
self.tag = line[1].strip()
case "custom_commands":
self.custom_commands = line[1].strip()
else:
break
def download(self):
dl_counter = 0
# Git repository
if hasattr(self, "git_url") and self.git_url:
repo = git.Repo.clone_from(self.git_url, os.path.join(REPO_DIR, self.name))
repo.git.checkout(self.tag)
elif hasattr(self, "dl_url") and self.dl_url:
while dl_counter < 10:
response = requests.get(self.dl_url)
if response.status_code == 200: # everything went fine
open(os.path.join(REPO_DIR, self.name), "wb").write(response.content)
break
dl_counter += 1
print("Failed to download {}. Retrying {}th time...", self.name, dl_counter)
time.sleep(5.0)
if hasattr(self, "custom_commands") and self.custom_commands:
p = subprocess.run(self.custom_commands, shell=True)
p.chek_returncode()
def main():
if not os.path.isdir(REPO_DIR):
os.mkdir(REPO_DIR)
f = open("dependencies.dep", "r")
os.chdir(REPO_DIR)
while True:
line = f.readline()
if not line:
break
stripped = line.strip()
# Remove comments
if stripped.startswith("#"):
return
uncommented = stripped.split("#", 1)[0]
if len(uncommented) == 0:
return
if uncommented.startswith("["):
dep_name = re.search('\[(.*)\]', uncommented).group(1)
dep = Dependency(dep_name, f)
print("Downloading dependency " + dep_name + "...")
dep.download()
f.close()
if __name__ == "__main__":
main()