1 |
commit: a6353ac1d8f55e41e1de7a7c6b352becb1dec264 |
2 |
Author: Michał Górny <mgorny <AT> gentoo <DOT> org> |
3 |
AuthorDate: Wed Sep 11 08:02:34 2019 +0000 |
4 |
Commit: Michał Górny <mgorny <AT> gentoo <DOT> org> |
5 |
CommitDate: Wed Sep 11 08:02:34 2019 +0000 |
6 |
URL: https://gitweb.gentoo.org/data/api.git/commit/?id=a6353ac1 |
7 |
|
8 |
bin: Add script to automate uid-gid table updates on wiki |
9 |
|
10 |
Signed-off-by: Michał Górny <mgorny <AT> gentoo.org> |
11 |
|
12 |
bin/update-wiki-table.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++ |
13 |
1 file changed, 101 insertions(+) |
14 |
|
15 |
diff --git a/bin/update-wiki-table.py b/bin/update-wiki-table.py |
16 |
new file mode 100755 |
17 |
index 0000000..b845e7f |
18 |
--- /dev/null |
19 |
+++ b/bin/update-wiki-table.py |
20 |
@@ -0,0 +1,101 @@ |
21 |
+#!/usr/bin/env python3 |
22 |
+ |
23 |
+import argparse |
24 |
+import os.path |
25 |
+import requests |
26 |
+import subprocess |
27 |
+import sys |
28 |
+ |
29 |
+ |
30 |
+def main(argv): |
31 |
+ default_api_url = 'https://wiki.gentoo.org/api.php' |
32 |
+ default_script_path = os.path.join(os.path.dirname(__file__), |
33 |
+ 'uidgid2wiki.awk') |
34 |
+ default_title = 'UID_GID_Assignment_Table' |
35 |
+ |
36 |
+ argp = argparse.ArgumentParser(prog=argv[0]) |
37 |
+ argp.add_argument('--api-url', default=default_api_url, |
38 |
+ help='URL to MediaWiki API (default: {})' |
39 |
+ .format(default_api_url)) |
40 |
+ argp.add_argument('-p', '--password', required=True, |
41 |
+ help='Bot password to log in with') |
42 |
+ argp.add_argument('--script', default=default_script_path, |
43 |
+ help='Path to uidgid2wiki script') |
44 |
+ argp.add_argument('--title', default=default_title, |
45 |
+ help='Title of page to edit (default: {})' |
46 |
+ .format(default_title)) |
47 |
+ argp.add_argument('-u', '--username', required=True, |
48 |
+ help='Username to log in with') |
49 |
+ argp.add_argument('path', nargs=1, metavar='uid-gid.txt', |
50 |
+ type=argparse.FileType('r', encoding='utf-8'), |
51 |
+ help='UID/GID listing to process') |
52 |
+ args = argp.parse_args(argv[1:]) |
53 |
+ |
54 |
+ # Get converted contents first. |
55 |
+ with subprocess.Popen([args.script], |
56 |
+ stdin=args.path[0], |
57 |
+ stdout=subprocess.PIPE) as s: |
58 |
+ page_data, _ = s.communicate() |
59 |
+ assert s.returncode == 0 |
60 |
+ |
61 |
+ # MediaWiki API is just HORRIBLE! Editing a page requires obtaining |
62 |
+ # a login token, logging in, obtaining a CSRF (!) token |
63 |
+ # and apparently preserving cookies as well! |
64 |
+ with requests.Session() as s: |
65 |
+ # get login token |
66 |
+ params = { |
67 |
+ 'action': 'query', |
68 |
+ 'meta': 'tokens', |
69 |
+ 'type': 'login', |
70 |
+ 'format': 'json', |
71 |
+ } |
72 |
+ with s.get(args.api_url, params=params) as r: |
73 |
+ token = r.json()['query']['tokens']['logintoken'] |
74 |
+ |
75 |
+ # log in |
76 |
+ params = { |
77 |
+ 'action': 'login', |
78 |
+ 'lgname': args.username, |
79 |
+ 'lgpassword': args.password, |
80 |
+ 'lgtoken': token, |
81 |
+ 'format': 'json', |
82 |
+ } |
83 |
+ with s.post(args.api_url, data=params) as r: |
84 |
+ assert r.json()['login']['result'] == 'Success', r.json() |
85 |
+ |
86 |
+ # get CSRF token (wtf?!) |
87 |
+ params = { |
88 |
+ 'action': 'query', |
89 |
+ 'meta': 'tokens', |
90 |
+ 'format': 'json', |
91 |
+ } |
92 |
+ with s.get(args.api_url, params=params) as r: |
93 |
+ token = r.json()['query']['tokens']['csrftoken'] |
94 |
+ |
95 |
+ # edit page (finally) |
96 |
+ params = { |
97 |
+ 'action': 'edit', |
98 |
+ 'title': args.title, |
99 |
+ 'token': token, |
100 |
+ 'format': 'json', |
101 |
+ 'text': page_data, |
102 |
+ 'summary': 'Automatic update from uid-gid.txt', |
103 |
+ 'bot': True, |
104 |
+ } |
105 |
+ with s.post(args.api_url, data=params) as r: |
106 |
+ assert 'error' not in r.json(), r.json() |
107 |
+ print(r.json()) |
108 |
+ |
109 |
+ # logout |
110 |
+ params = { |
111 |
+ 'action': 'logout', |
112 |
+ 'token': token, |
113 |
+ } |
114 |
+ with s.get(args.api_url, params=params) as r: |
115 |
+ pass |
116 |
+ |
117 |
+ return 0 |
118 |
+ |
119 |
+ |
120 |
+if __name__ == '__main__': |
121 |
+ sys.exit(main(sys.argv)) |