Merge pull request #128215 from primeos/tdesktop-update-script

tdesktop: Add an update script

authored by

Michael Weiss and committed by
GitHub
eb1b4730 f5bba23a

+70
+70
pkgs/applications/networking/instant-messengers/telegram/tdesktop/update.py
··· 1 + #! /usr/bin/env nix-shell 2 + #! nix-shell -i python3 -p python3 nix 3 + 4 + import fileinput 5 + import json 6 + import os 7 + import re 8 + import subprocess 9 + 10 + from datetime import datetime 11 + from urllib.request import urlopen, Request 12 + 13 + 14 + DIR = os.path.dirname(os.path.abspath(__file__)) 15 + HEADERS = {'Accept': 'application/vnd.github.v3+json'} 16 + 17 + 18 + def github_api_request(endpoint): 19 + base_url = 'https://api.github.com/' 20 + request = Request(base_url + endpoint, headers=HEADERS) 21 + with urlopen(request) as http_response: 22 + return json.loads(http_response.read().decode('utf-8')) 23 + 24 + 25 + def get_commit_date(repo, sha): 26 + url = f'https://api.github.com/repos/{repo}/commits/{sha}' 27 + request = Request(url, headers=HEADERS) 28 + with urlopen(request) as http_response: 29 + commit = json.loads(http_response.read().decode()) 30 + date = commit['commit']['committer']['date'].rstrip('Z') 31 + date = datetime.fromisoformat(date).date().isoformat() 32 + return 'unstable-' + date 33 + 34 + 35 + def nix_prefetch_url(url, unpack=False): 36 + """Prefetches the content of the given URL.""" 37 + print(f'nix-prefetch-url {url}') 38 + options = ['--type', 'sha256'] 39 + if unpack: 40 + options += ['--unpack'] 41 + out = subprocess.check_output(['nix-prefetch-url'] + options + [url]) 42 + return out.decode('utf-8').rstrip() 43 + 44 + 45 + def update_file(relpath, version, sha256, rev=None): 46 + file_path = os.path.join(DIR, relpath) 47 + with fileinput.FileInput(file_path, inplace=True) as f: 48 + for line in f: 49 + result = line 50 + result = re.sub(r'^ version = ".+";', f' version = "{version}";', result) 51 + result = re.sub(r'^ sha256 = ".+";', f' sha256 = "{sha256}";', result) 52 + if rev: 53 + result = re.sub(r'^ rev = ".*";', f' rev = "{rev}";', result) 54 + print(result, end='') 55 + 56 + 57 + if __name__ == "__main__": 58 + tdesktop_tag = github_api_request('repos/telegramdesktop/tdesktop/releases/latest')['tag_name'] 59 + tdesktop_version = tdesktop_tag.lstrip('v') 60 + tdesktop_hash = nix_prefetch_url(f'https://github.com/telegramdesktop/tdesktop/releases/download/{tdesktop_tag}/tdesktop-{tdesktop_version}-full.tar.gz') 61 + update_file('default.nix', tdesktop_version, tdesktop_hash) 62 + tg_owt_ref = github_api_request('repos/desktop-app/tg_owt/commits/master')['sha'] 63 + tg_owt_version = get_commit_date('desktop-app/tg_owt', tg_owt_ref) 64 + tg_owt_hash = 'TODO' 65 + update_file('tg_owt.nix', tg_owt_version, tg_owt_hash, tg_owt_ref) 66 + tg_owt_ref = github_api_request('repos/desktop-app/tg_owt/commits/master')['sha'] 67 + libtgvoip_ref = github_api_request(f'repos/telegramdesktop/tdesktop/contents/Telegram/ThirdParty/libtgvoip?ref={tdesktop_tag}')['sha'] 68 + libtgvoip_version = get_commit_date('telegramdesktop/libtgvoip', libtgvoip_ref) 69 + libtgvoip_hash = nix_prefetch_url(f'https://github.com/telegramdesktop/libtgvoip/archive/{libtgvoip_ref}.tar.gz', unpack=True) 70 + update_file('../../../../../development/libraries/libtgvoip/default.nix', libtgvoip_version, libtgvoip_hash, libtgvoip_ref)