From f6f8170a60fb2dffb93b8eda44a5a6e76f6fae5a Mon Sep 17 00:00:00 2001 From: Alyssa Ross Date: Thu, 11 Feb 2021 16:43:36 +0000 Subject: [PATCH] dictdDBs.wiktionary: add updateScript --- pkgs/servers/dict/wiktionary/default.nix | 2 + .../servers/dict/wiktionary/latest_version.py | 42 +++++++++++++++++++ pkgs/servers/dict/wiktionary/update.sh | 7 ++++ 3 files changed, 51 insertions(+) create mode 100644 pkgs/servers/dict/wiktionary/latest_version.py create mode 100755 pkgs/servers/dict/wiktionary/update.sh diff --git a/pkgs/servers/dict/wiktionary/default.nix b/pkgs/servers/dict/wiktionary/default.nix index 3c18056821d8..cd0c9c6c24bc 100644 --- a/pkgs/servers/dict/wiktionary/default.nix +++ b/pkgs/servers/dict/wiktionary/default.nix @@ -13,6 +13,8 @@ stdenv.mkDerivation rec { buildInputs = [ python dict glibcLocales ]; builder = ./builder.sh; + passthru.updateScript = ./update.sh; + meta = with lib; { description = "DICT version of English Wiktionary"; homepage = "http://en.wiktionary.org/"; diff --git a/pkgs/servers/dict/wiktionary/latest_version.py b/pkgs/servers/dict/wiktionary/latest_version.py new file mode 100644 index 000000000000..2833a1e05b03 --- /dev/null +++ b/pkgs/servers/dict/wiktionary/latest_version.py @@ -0,0 +1,42 @@ +import subprocess + +from html.parser import HTMLParser +from os.path import abspath, dirname +from urllib.request import urlopen + +class WiktionaryLatestVersionParser(HTMLParser): + def __init__(self, current_version, *args, **kwargs): + self.latest_version = current_version + super().__init__(*args, **kwargs) + + + def handle_starttag(self, tag, attrs): + if tag != 'a': + return + + href = dict(attrs)['href'][0:-1] + if href == 'latest': + return + + self.latest_version = max(self.latest_version, href) + + +def nix_prefetch_url(url, algo='sha256'): + """Prefetches the content of the given URL.""" + print(f'nix-prefetch-url {url}') + out = subprocess.check_output(['nix-prefetch-url', '--type', algo, url]) + return out.decode('utf-8').rstrip() + + +current_version = subprocess.check_output([ + 'nix', 'eval', '--raw', + '-f', dirname(abspath(__file__)) + '/../../../..', + 'dictdDBs.wiktionary.version', +]).decode('utf-8') + +parser = WiktionaryLatestVersionParser(current_version) + +with urlopen('https://dumps.wikimedia.org/enwiktionary/') as resp: + parser.feed(resp.read().decode('utf-8')) + +print(parser.latest_version) diff --git a/pkgs/servers/dict/wiktionary/update.sh b/pkgs/servers/dict/wiktionary/update.sh new file mode 100755 index 000000000000..ff5e0fc3551a --- /dev/null +++ b/pkgs/servers/dict/wiktionary/update.sh @@ -0,0 +1,7 @@ +#! /usr/bin/env nix-shell +#! nix-shell -i bash -p common-updater-scripts python3 + +set -ueo pipefail + +version="$(python "$(dirname "${BASH_SOURCE[0]}")"/latest_version.py)" +update-source-version dictdDBs.wiktionary "$version"