|
| 1 | +#!/usr/bin/env -S v run |
| 2 | + |
| 3 | +import os |
| 4 | +import net.http |
| 5 | +import compress.szip |
| 6 | +import crypto.sha3 |
| 7 | + |
| 8 | +fn should_be_ok(http_status_code int, msg string) { |
| 9 | + assert http_status_code == 200, '${msg}. Check your internet connection and try again later.' |
| 10 | +} |
| 11 | + |
| 12 | +fn main() { |
| 13 | + unbuffer_stdout() |
| 14 | + os.chdir(@VROOT)! |
| 15 | + |
| 16 | + download_page_url := 'https://sqlite.org/download.html' |
| 17 | + println('> Getting ${download_page_url} ...') |
| 18 | + download_page := http.get(download_page_url)! |
| 19 | + should_be_ok(download_page.status_code, 'The download page of SQLite is not available now.') |
| 20 | + |
| 21 | + dlines := download_page.body.split_into_lines() |
| 22 | + amalgamation_csv := dlines.filter(|line| line.starts_with('PRODUCT,') |
| 23 | + && line.contains('amalgamation'))[0].split(',') |
| 24 | + assert amalgamation_csv.len >= 4 |
| 25 | + |
| 26 | + version := amalgamation_csv[1] |
| 27 | + zip_name := os.file_name(amalgamation_csv[2]) |
| 28 | + zip_url := 'https://sqlite.org/${amalgamation_csv[2]}' |
| 29 | + url_size := amalgamation_csv[3].int() |
| 30 | + url_sha3 := amalgamation_csv[4] |
| 31 | + println('> Getting SQLite amalgamation version: ${version}') |
| 32 | + println('> from url: ${zip_url}') |
| 33 | + println('> expected size: ${url_size}') |
| 34 | + println('> expected SHA3: ${url_sha3} ...') |
| 35 | + amalgamation_name := zip_name.to_lower().replace('.zip', '') |
| 36 | + |
| 37 | + println('> Downloading from ${zip_url} ...') |
| 38 | + zip_content := http.get(zip_url)! |
| 39 | + should_be_ok(zip_content.status_code, 'The .zip file URL of SQLite is not available now.') |
| 40 | + |
| 41 | + assert zip_content.body.len == url_size |
| 42 | + println('> download size: ${zip_content.body.len} matches expected size: ${url_size} .') |
| 43 | + zip_shasum := sha3.sum256(zip_content.body.bytes()).hex() |
| 44 | + assert zip_shasum == url_sha3 |
| 45 | + println('> download sha3: ${zip_shasum} matches too.') |
| 46 | + |
| 47 | + os.write_file(zip_name, zip_content.body)! |
| 48 | + assert os.is_file(zip_name) |
| 49 | + assert szip.extract_zip_to_dir(zip_name, 'thirdparty')! |
| 50 | + os.rmdir_all('thirdparty/sqlite') or {} |
| 51 | + os.mv('thirdparty/${amalgamation_name}', 'thirdparty/sqlite')! |
| 52 | + os.rm('thirdparty/sqlite/shell.c')! |
| 53 | + files := os.walk_ext('thirdparty/sqlite', '') |
| 54 | + for f in files { |
| 55 | + println('> extracted file: ${f:-40s} | size: ${os.file_size(f):8}') |
| 56 | + } |
| 57 | + |
| 58 | + println('> removing ${zip_name} ...') |
| 59 | + os.rm(zip_name)! |
| 60 | + println('> done') |
| 61 | +} |
0 commit comments