nixpkgs/pkgs/applications/networking/browsers/microsoft-edge/update.py
百地 希留耶 152922233a
microsoft-edge: Rewrite update script to use latest version (#219395)
Co-authored-by: Rik Huijzer <t.h.huijzer@rug.nl>
2023-03-27 01:48:23 +02:00

69 lines
2.1 KiB
Python
Executable File

#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3Packages.packaging python3Packages.debian
import base64
import gzip
import textwrap
from urllib import request
from debian.deb822 import Packages
from debian.debian_support import Version
def packages():
packages_url = 'https://packages.microsoft.com/repos/edge/dists/stable/main/binary-amd64/Packages'
handle = request.urlopen(packages_url)
return handle
def latest_packages(packages: bytes):
latest_packages: dict[str, Packages] = {}
for package in Packages.iter_paragraphs(packages, use_apt_pkg=False):
name: str = package['Package']
if not name.startswith('microsoft-edge-'):
continue
channel = name.replace('microsoft-edge-', '')
if channel not in latest_packages:
latest_packages[channel] = package
else:
old_package = latest_packages[channel]
if old_package.get_version() < package.get_version(): # type: ignore
latest_packages[channel] = package
return latest_packages
def nix_expressions(latest: dict[str, Packages]):
channel_strs: list[str] = []
for channel, package in latest.items():
print(f"Processing {channel} {package['Version']}")
match = Version.re_valid_version.match(package['Version'])
assert match is not None
version = match.group('upstream_version')
revision = match.group('debian_revision')
sri = 'sha256-' + \
base64.b64encode(bytes.fromhex(package['SHA256'])).decode('ascii')
channel_str = textwrap.dedent(
f'''\
{channel} = import ./browser.nix {{
channel = "{channel}";
version = "{version}";
revision = "{revision}";
sha256 = "{sri}";
}};'''
)
channel_strs.append(channel_str)
return channel_strs
def write_expression():
latest = latest_packages(packages())
channel_strs = nix_expressions(latest)
nix_expr = '{\n' + textwrap.indent('\n'.join(channel_strs), ' ') + '\n}'
with open('default.nix', 'w') as f:
f.write(nix_expr)
write_expression()