1#!/usr/bin/env python3
 2
 3import urllib.request
 4
 5vendor = {
 6    "https://github.com/nlohmann/json/releases/latest/download/json.hpp":     "vendor/nlohmann/json.hpp",
 7    "https://github.com/nlohmann/json/releases/latest/download/json_fwd.hpp": "vendor/nlohmann/json_fwd.hpp",
 8
 9    "https://raw.githubusercontent.com/nothings/stb/refs/heads/master/stb_image.h": "vendor/stb/stb_image.h",
10
11    # not using latest tag to avoid this issue: https://github.com/ggml-org/llama.cpp/pull/17179#discussion_r2515877926
12    # "https://github.com/mackron/miniaudio/raw/refs/tags/0.11.23/miniaudio.h": "vendor/miniaudio/miniaudio.h",
13    "https://github.com/mackron/miniaudio/raw/669ed3e844524fcd883231b13095baee9f6de304/miniaudio.h": "vendor/miniaudio/miniaudio.h",
14
15    "https://raw.githubusercontent.com/yhirose/cpp-httplib/refs/tags/v0.30.2/httplib.h": "vendor/cpp-httplib/httplib.h",
16    "https://raw.githubusercontent.com/yhirose/cpp-httplib/refs/tags/v0.30.2/LICENSE":   "vendor/cpp-httplib/LICENSE",
17
18    "https://raw.githubusercontent.com/sheredom/subprocess.h/b49c56e9fe214488493021017bf3954b91c7c1f5/subprocess.h": "vendor/sheredom/subprocess.h",
19}
20
21for url, filename in vendor.items():
22    print(f"downloading {url} to {filename}") # noqa: NP100
23    urllib.request.urlretrieve(url, filename)
24
25    # split cpp/h files for httplib
26    # see: https://github.com/yhirose/cpp-httplib/blob/master/split.py
27    if 'httplib.h' in filename:
28        border = '// ----------------------------------------------------------------------------'
29        with open(filename, 'r') as f:
30            content = f.read()
31        header, implementation, footer = content.split(border, 2)
32        fname_cpp = filename.replace('.h', '.cpp')
33        with open(filename, 'w') as fh:
34            fh.write(header)
35            fh.write(footer)
36        with open(fname_cpp, 'w') as fc:
37            fc.write('#include "httplib.h"\n')
38            fc.write('namespace httplib {\n')
39            fc.write(implementation.replace('\ninline ', '\n'))
40            fc.write('} // namespace httplib\n')