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