|
| 1 | +import itertools |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import re |
| 5 | +import sys |
| 6 | + |
| 7 | +from typing import Iterable |
| 8 | + |
| 9 | +import requests |
| 10 | + |
| 11 | +from deploy_util import ( |
| 12 | + get_git_deploy_reason, |
| 13 | + deploy_file_to_wiki, |
| 14 | + read_cookie_jar, |
| 15 | + read_file_from_path, |
| 16 | + write_to_github_summary_file, |
| 17 | +) |
| 18 | +from login_and_get_token import get_token |
| 19 | + |
| 20 | +HEADER_PATTERN = re.compile( |
| 21 | + r"\A---\n" r"-- @Liquipedia\n" r"-- page=(?P<pageName>[^\n]*)\n" |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def deploy_all_files_for_wiki( |
| 26 | + wiki: str, file_paths: Iterable[pathlib.Path], deploy_reason: str |
| 27 | +) -> bool: |
| 28 | + all_modules_deployed = True |
| 29 | + token = get_token(wiki) |
| 30 | + with requests.Session() as session: |
| 31 | + session.cookies = read_cookie_jar(wiki) |
| 32 | + for file_path in file_paths: |
| 33 | + print(f"::group::Checking {str(file_path)}") |
| 34 | + file_content = read_file_from_path(file_path) |
| 35 | + header_match = HEADER_PATTERN.match(file_content) |
| 36 | + if not header_match: |
| 37 | + print("...skipping - no magic comment found") |
| 38 | + write_to_github_summary_file(f"{str(file_path)} skipped") |
| 39 | + else: |
| 40 | + page = header_match.groupdict()["pageName"] + ( |
| 41 | + os.getenv("LUA_DEV_ENV_NAME") or "" |
| 42 | + ) |
| 43 | + module_deployed, _ = deploy_file_to_wiki( |
| 44 | + session, file_path, file_content, wiki, page, token, deploy_reason |
| 45 | + ) |
| 46 | + all_modules_deployed &= module_deployed |
| 47 | + print("::endgroup::") |
| 48 | + return all_modules_deployed |
| 49 | + |
| 50 | + |
| 51 | +def main(): |
| 52 | + all_modules_deployed = True |
| 53 | + lua_files: Iterable[pathlib.Path] |
| 54 | + git_deploy_reason: str |
| 55 | + if len(sys.argv[1:]) == 0: |
| 56 | + lua_files = pathlib.Path("./lua/wikis/").rglob("*.lua") |
| 57 | + git_deploy_reason = "Automated Weekly Re-Sync" |
| 58 | + else: |
| 59 | + lua_files = [pathlib.Path(arg) for arg in sys.argv[1:]] |
| 60 | + git_deploy_reason = get_git_deploy_reason() |
| 61 | + |
| 62 | + for wiki, files in itertools.groupby(sorted(lua_files), lambda path: path.parts[2]): |
| 63 | + all_modules_deployed &= deploy_all_files_for_wiki( |
| 64 | + wiki, list(files), git_deploy_reason |
| 65 | + ) |
| 66 | + |
| 67 | + if not all_modules_deployed: |
| 68 | + print("::warning::Some modules were not deployed!") |
| 69 | + sys.exit(1) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
0 commit comments