From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 63D0B138334 for ; Wed, 11 Sep 2019 08:03:00 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 9C2D2E0916; Wed, 11 Sep 2019 08:02:59 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 74C4CE0916 for ; Wed, 11 Sep 2019 08:02:59 +0000 (UTC) Received: from oystercatcher.gentoo.org (unknown [IPv6:2a01:4f8:202:4333:225:90ff:fed9:fc84]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 0C74D34B01F for ; Wed, 11 Sep 2019 08:02:58 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id B94A6720 for ; Wed, 11 Sep 2019 08:02:55 +0000 (UTC) From: "Michał Górny" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Michał Górny" Message-ID: <1568188954.a6353ac1d8f55e41e1de7a7c6b352becb1dec264.mgorny@gentoo> Subject: [gentoo-commits] data/api:master commit in: bin/ X-VCS-Repository: data/api X-VCS-Files: bin/update-wiki-table.py X-VCS-Directories: bin/ X-VCS-Committer: mgorny X-VCS-Committer-Name: Michał Górny X-VCS-Revision: a6353ac1d8f55e41e1de7a7c6b352becb1dec264 X-VCS-Branch: master Date: Wed, 11 Sep 2019 08:02:55 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 14c73dee-8d81-40fe-a5e3-6a3636145a63 X-Archives-Hash: 816c8d076d1d5fbf05f12fd18b4b77af commit: a6353ac1d8f55e41e1de7a7c6b352becb1dec264 Author: Michał Górny gentoo org> AuthorDate: Wed Sep 11 08:02:34 2019 +0000 Commit: Michał Górny gentoo org> CommitDate: Wed Sep 11 08:02:34 2019 +0000 URL: https://gitweb.gentoo.org/data/api.git/commit/?id=a6353ac1 bin: Add script to automate uid-gid table updates on wiki Signed-off-by: Michał Górny gentoo.org> bin/update-wiki-table.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/bin/update-wiki-table.py b/bin/update-wiki-table.py new file mode 100755 index 0000000..b845e7f --- /dev/null +++ b/bin/update-wiki-table.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python3 + +import argparse +import os.path +import requests +import subprocess +import sys + + +def main(argv): + default_api_url = 'https://wiki.gentoo.org/api.php' + default_script_path = os.path.join(os.path.dirname(__file__), + 'uidgid2wiki.awk') + default_title = 'UID_GID_Assignment_Table' + + argp = argparse.ArgumentParser(prog=argv[0]) + argp.add_argument('--api-url', default=default_api_url, + help='URL to MediaWiki API (default: {})' + .format(default_api_url)) + argp.add_argument('-p', '--password', required=True, + help='Bot password to log in with') + argp.add_argument('--script', default=default_script_path, + help='Path to uidgid2wiki script') + argp.add_argument('--title', default=default_title, + help='Title of page to edit (default: {})' + .format(default_title)) + argp.add_argument('-u', '--username', required=True, + help='Username to log in with') + argp.add_argument('path', nargs=1, metavar='uid-gid.txt', + type=argparse.FileType('r', encoding='utf-8'), + help='UID/GID listing to process') + args = argp.parse_args(argv[1:]) + + # Get converted contents first. + with subprocess.Popen([args.script], + stdin=args.path[0], + stdout=subprocess.PIPE) as s: + page_data, _ = s.communicate() + assert s.returncode == 0 + + # MediaWiki API is just HORRIBLE! Editing a page requires obtaining + # a login token, logging in, obtaining a CSRF (!) token + # and apparently preserving cookies as well! + with requests.Session() as s: + # get login token + params = { + 'action': 'query', + 'meta': 'tokens', + 'type': 'login', + 'format': 'json', + } + with s.get(args.api_url, params=params) as r: + token = r.json()['query']['tokens']['logintoken'] + + # log in + params = { + 'action': 'login', + 'lgname': args.username, + 'lgpassword': args.password, + 'lgtoken': token, + 'format': 'json', + } + with s.post(args.api_url, data=params) as r: + assert r.json()['login']['result'] == 'Success', r.json() + + # get CSRF token (wtf?!) + params = { + 'action': 'query', + 'meta': 'tokens', + 'format': 'json', + } + with s.get(args.api_url, params=params) as r: + token = r.json()['query']['tokens']['csrftoken'] + + # edit page (finally) + params = { + 'action': 'edit', + 'title': args.title, + 'token': token, + 'format': 'json', + 'text': page_data, + 'summary': 'Automatic update from uid-gid.txt', + 'bot': True, + } + with s.post(args.api_url, data=params) as r: + assert 'error' not in r.json(), r.json() + print(r.json()) + + # logout + params = { + 'action': 'logout', + 'token': token, + } + with s.get(args.api_url, params=params) as r: + pass + + return 0 + + +if __name__ == '__main__': + sys.exit(main(sys.argv))