forked from Liquipedia/Lua-Modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotect.py
More file actions
92 lines (73 loc) · 2.81 KB
/
Copy pathprotect.py
File metadata and controls
92 lines (73 loc) · 2.81 KB
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
import itertools
import os
import pathlib
import sys
from deploy_util import get_wikis
from mediawiki_session import MediaWikiSession
from protect_page import (
protect_non_existing_pages,
protect_existing_pages,
handle_protect_errors,
)
WIKI_TO_PROTECT = os.getenv("WIKI_TO_PROTECT")
def protect_new_wiki(wiki_to_protect: str):
lua_files = itertools.chain(
pathlib.Path("./lua/wikis/commons/").rglob("*.lua"),
pathlib.Path("./lua/wikis/" + wiki_to_protect + "/").rglob("*.lua"),
)
commons_modules: set[str] = set()
local_modules: set[str] = set()
for file_to_protect in sorted(lua_files):
wiki = file_to_protect.parts[2]
module = "/".join(file_to_protect.parts[3:])[:-4]
page = "Module:" + module
if wiki == wiki_to_protect:
local_modules.add(page)
elif wiki == "commons":
commons_modules.add(page)
with MediaWikiSession(wiki_to_protect) as session:
print(f"::group::Protecting {WIKI_TO_PROTECT}")
protect_non_existing_pages(session, commons_modules - local_modules)
protect_existing_pages(session, local_modules)
print("::endgroup::")
handle_protect_errors()
def main():
if WIKI_TO_PROTECT:
protect_new_wiki(WIKI_TO_PROTECT)
return
elif len(sys.argv[1:]) == 0:
print("Nothing to protect")
exit(0)
lua_files = [pathlib.Path(arg) for arg in sys.argv[1:]]
files_to_protect_by_wiki: dict[str, set[str]] = dict()
for wiki, files_to_protect in itertools.groupby(
sorted(lua_files), lambda path: path.parts[2]
):
files_to_protect_by_wiki[wiki] = set(
[
"Module:" + "/".join(file_to_protect.parts[3:])[:-4]
for file_to_protect in files_to_protect
]
)
new_commons_modules = files_to_protect_by_wiki.get("commons")
if new_commons_modules:
for wiki in sorted(get_wikis()):
with MediaWikiSession(wiki) as session:
if wiki == "commons":
protect_existing_pages(session, new_commons_modules)
else:
new_local_modules = files_to_protect_by_wiki.get(wiki)
if new_local_modules:
protect_existing_pages(session, new_local_modules)
protect_non_existing_pages(
session, new_commons_modules - new_local_modules
)
else:
protect_non_existing_pages(session, new_commons_modules)
else:
for wiki, new_modules in files_to_protect_by_wiki.items():
with MediaWikiSession(wiki) as session:
protect_existing_pages(session, new_modules)
handle_protect_errors()
if __name__ == "__main__":
main()