lix-installer/set_version.py

84 lines
2.4 KiB
Python
Executable file

#!/usr/bin/env python
from pathlib import Path
import textwrap
import dataclasses
import requests
SYSTEMS = ['x86_64-linux', 'x86_64-darwin', 'aarch64-linux', 'aarch64-darwin']
@dataclasses.dataclass
class Package:
system: str
version: str
def url(self):
return f"https://releases.lix.systems/lix/lix-{self.version}/lix-{self.version}-{self.system}.tar.xz";
def variable_name(self):
system = self.system.replace('-', '_').upper()
return f'LIX_{system}_URL'
def make_urls_section(packages: list[Package]):
def one_item(package: Package):
return textwrap.dedent("""\
/// Default [`nix_package_url`](CommonSettings::nix_package_url) for {system}.
pub const {variable_name}: &str =
"{url}";
""").format(system=package.system, variable_name=package.variable_name(), url=package.url())
return '\n'.join(one_item(package) for package in packages)
def replace_section(old: str, section: str) -> str:
lines = []
eat = False
for line in old.splitlines():
next_eat = eat
if 'BEGIN GENERATE-URLS' in line.strip():
next_eat = True
elif 'END GENERATE-URLS' in line.strip():
lines.append(section)
eat = False
next_eat = False
if not eat:
lines.append(line)
eat = next_eat
return '\n'.join(lines)
def replace_in_file(file: Path, section: str):
new_file = replace_section(file.read_text(), section)
file.write_text(new_file)
def make_flake_url_section(version: str) -> str:
return f' url = "https://git.lix.systems/lix-project/lix/archive/{version}.tar.gz";'
def main():
import argparse
ap = argparse.ArgumentParser(description='Update the version of lix-installer')
ap.add_argument('new_version', help='The new version')
args = ap.parse_args()
settings_rs = Path('src/settings.rs')
packages = [Package(system, args.new_version) for system in SYSTEMS]
for package in packages:
resp = requests.head(package.url())
if resp.status_code != 200:
print(f'Warning: broken URL {package.url()} returns HTTP {resp.status_code}')
replace_in_file(settings_rs, make_urls_section(packages))
flake_nix = Path('flake.nix')
replace_in_file(flake_nix, make_flake_url_section(args.new_version))
if __name__ == '__main__':
main()