2021-02-07 20:20:07 +01:00
|
|
|
#!/usr/bin/env python3
|
2021-10-08 18:49:20 +02:00
|
|
|
"""
|
|
|
|
This tool updates conf/*.src according to the available extension version at wmflabs.org
|
|
|
|
"""
|
2021-02-07 20:20:07 +01:00
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
import sys
|
2021-02-07 20:20:07 +01:00
|
|
|
import json
|
|
|
|
import urllib.request
|
|
|
|
from html.parser import HTMLParser
|
|
|
|
import subprocess
|
|
|
|
import hashlib
|
2021-10-08 18:49:20 +02:00
|
|
|
import fileinput
|
|
|
|
from typing import List
|
2021-02-07 20:20:07 +01:00
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
EXTENSIONS_HOST_URL = 'https://extdist.wmflabs.org/dist/extensions/'
|
2021-02-07 20:20:07 +01:00
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
|
|
|
|
def get_all_extensions() -> List[str]:
|
|
|
|
"""Get all available extensions."""
|
|
|
|
with urllib.request.urlopen(EXTENSIONS_HOST_URL) as page:
|
|
|
|
webpage = page.read().decode('utf-8')
|
2021-02-07 20:20:07 +01:00
|
|
|
|
|
|
|
class MyHTMLParser(HTMLParser):
|
2021-10-08 18:49:20 +02:00
|
|
|
"""Custom HTMLParser"""
|
2021-02-07 20:20:07 +01:00
|
|
|
links = []
|
2021-10-08 18:49:20 +02:00
|
|
|
|
2021-02-07 20:20:07 +01:00
|
|
|
def handle_starttag(self, tag, attrs):
|
|
|
|
# Only parse the 'anchor' tag.
|
|
|
|
if tag == 'a':
|
|
|
|
# Check the list of defined attributes.
|
|
|
|
for name, value in attrs:
|
|
|
|
# If href is defined, print it.
|
|
|
|
if name == "href":
|
|
|
|
self.links.append(value)
|
|
|
|
|
|
|
|
parser = MyHTMLParser()
|
|
|
|
parser.feed(webpage)
|
|
|
|
return parser.links
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
|
|
|
|
def get_mediawiki_ext_version() -> str:
|
|
|
|
"""Returns the mediawiki version for extensions."""
|
|
|
|
with open('manifest.json', encoding='utf-8') as manifest_json:
|
2021-02-07 20:20:07 +01:00
|
|
|
manifest = json.load(manifest_json)
|
|
|
|
mediawiki_version = manifest['version'].split('~')[0]
|
|
|
|
mediawiki_ext_version = '_'.join(mediawiki_version.split('.')[0:2])
|
|
|
|
return mediawiki_ext_version
|
|
|
|
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
def get_extensions_for_version(extensions: List[str], version: str) -> List[str]:
|
|
|
|
"""Returns available extensions compatible with mediawiki version."""
|
|
|
|
exts = [ext for ext in extensions if version in ext]
|
2021-02-07 20:20:07 +01:00
|
|
|
return exts
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
def sha256sum(filename: str) -> str:
|
|
|
|
"""Calculate the sha256 of a file."""
|
2021-02-07 20:20:07 +01:00
|
|
|
sha256_hash = hashlib.sha256()
|
2021-10-08 18:49:20 +02:00
|
|
|
with open(filename, "rb") as file:
|
2021-02-07 20:20:07 +01:00
|
|
|
# Read and update hash string value in blocks of 4K
|
2021-10-08 18:49:20 +02:00
|
|
|
for byte_block in iter(lambda: file.read(4096), b""):
|
2021-02-07 20:20:07 +01:00
|
|
|
sha256_hash.update(byte_block)
|
|
|
|
return sha256_hash.hexdigest()
|
|
|
|
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
def replace_line_startingwith(file: str, startingwith: str, new_line: str):
|
|
|
|
""""""
|
2021-02-07 20:20:07 +01:00
|
|
|
for line in fileinput.input(file, inplace=1):
|
|
|
|
if line.startswith(startingwith):
|
|
|
|
line = new_line + '\n'
|
|
|
|
sys.stdout.write(line)
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
|
|
|
|
def update_source_file(srcfile: str, url: str):
|
2021-02-07 20:20:07 +01:00
|
|
|
filename = url.rsplit('/', 1)[1]
|
|
|
|
urllib.request.urlretrieve(url, filename)
|
2021-10-08 18:49:20 +02:00
|
|
|
hashsum = sha256sum(filename)
|
2021-02-07 20:20:07 +01:00
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
replace_line_startingwith(srcfile, 'SOURCE_URL=', f'SOURCE_URL={url}')
|
|
|
|
replace_line_startingwith(srcfile, 'SOURCE_SUM=', f'SOURCE_SUM={hashsum}')
|
2021-02-07 20:20:07 +01:00
|
|
|
replace_line_startingwith(srcfile, 'SOURCE_SUM_PRG=', 'SOURCE_SUM_PRG=sha256sum')
|
|
|
|
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
def get_required_extensions(extensions: List[str]):
|
2021-02-07 20:20:07 +01:00
|
|
|
ext_files = [
|
2021-10-08 18:49:20 +02:00
|
|
|
{'name': 'LDAPAuthentication2', 'file': 'conf/ldap_authentication2.src', },
|
|
|
|
{'name': 'LDAPAuthorization', 'file': 'conf/ldap_authorization.src', },
|
|
|
|
# {'name': 'Auth_remoteuser', 'file': 'conf/ldap_auth_remoteuser.src', },
|
|
|
|
{'name': 'LDAPGroups', 'file': 'conf/ldap_groups.src', },
|
|
|
|
{'name': 'LDAPProvider', 'file': 'conf/ldap_provider.src', },
|
|
|
|
{'name': 'LDAPUserInfo', 'file': 'conf/ldap_userinfo.src', },
|
|
|
|
{'name': 'PluggableAuth', 'file': 'conf/pluggable_auth.src', },
|
2021-02-07 20:20:07 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
for ext in ext_files:
|
|
|
|
file = ext['file']
|
|
|
|
name = ext['name']
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
echo_var = f'source {file} ; echo ${{}}'
|
|
|
|
current_url = subprocess.check_output(
|
|
|
|
echo_var.format('SOURCE_URL'), shell=True
|
|
|
|
).decode('utf-8').strip()
|
2021-02-07 20:20:07 +01:00
|
|
|
|
|
|
|
# Search for corresponding in extensions
|
2021-10-08 18:49:20 +02:00
|
|
|
matching_extension_urls = [url for url in extensions if name in url]
|
2021-02-07 20:20:07 +01:00
|
|
|
|
|
|
|
if len(matching_extension_urls) != 1:
|
2021-10-08 18:49:20 +02:00
|
|
|
print(f'ERROR: Could not find an upstream link for extension {name}')
|
2021-02-07 20:20:07 +01:00
|
|
|
continue
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
new_url = EXTENSIONS_HOST_URL + matching_extension_urls[0]
|
2021-02-07 20:20:07 +01:00
|
|
|
|
|
|
|
if current_url == new_url:
|
2021-10-08 18:49:20 +02:00
|
|
|
print(f'OK: url is up to date for {name}')
|
2021-02-07 20:20:07 +01:00
|
|
|
continue
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
print(f'Updating source file for {name}')
|
|
|
|
update_source_file(file, new_url)
|
2021-02-07 20:20:07 +01:00
|
|
|
|
|
|
|
|
2021-10-08 18:49:20 +02:00
|
|
|
def main():
|
|
|
|
"""Main function."""
|
2021-02-07 20:20:07 +01:00
|
|
|
mediawiki_ext_version = get_mediawiki_ext_version()
|
|
|
|
extensions = get_all_extensions()
|
|
|
|
extensions = get_extensions_for_version(extensions, mediawiki_ext_version)
|
|
|
|
get_required_extensions(extensions)
|
2021-10-08 18:49:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|