vpac / src / pkgbuild.v
113 lines · 103 sloc · 4.01 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1@[has_globals]
2module main
3
4import net.urllib
5import json2
6
7pub struct PackagePkgbuild {
8pub mut:
9 depends []string @[json: Depends]
10 make_depends []string @[json: MakeDepends]
11 check_depends []string @[json: CheckDepends]
12 description string @[json: Description]
13 first_submitted i64 @[json: FirstSubmitted]
14 id i64 @[json: ID]
15 last_modified i64 @[json: LastModified]
16 maintainer ?string @[json: Maintainer]
17 name string @[json: Name]
18 num_votes i64 @[json: NumVotes]
19 out_of_date ?i64 @[json: OutOfDate]
20 package_base string @[json: PackageBase]
21 package_base_id i64 @[json: PackageBaseID]
22 popularity f64 @[json: Popularity]
23 url string @[json: URL]
24 url_path string @[json: URLPath]
25 version string @[json: Version]
26}
27
28pub struct PackagesPkgbuild {
29pub mut:
30 count i64 @[json: resultcount]
31 results []PackagePkgbuild
32 type string
33 version i64
34 // the rpc's own failure, when it refuses the request; absent when it answers
35 error_text ?string @[json: error]
36}
37
38// pkgbuild_rpc_timeout is how long one rpc request may take, for the search
39// and the info lookups of the pkgbuild repos. a stall would otherwise hang
40// the search under a proxy's silent wall
41// (pkgbuild :timeout) sets it, in seconds
42__global pkgbuild_rpc_timeout = 10
43
44// pkgbuild_rpc fetches one url and reads its answer: timed, and demanding an
45// http answer the rpc actually gives. what it fetches is returned as text;
46// the callers decode their own answer out of it
47fn pkgbuild_rpc(url string, what string) !string {
48 return http_text(url, pkgbuild_rpc_timeout, what, 'the pkgbuild rpc')
49}
50
51// answer_start is the start of an rpc answer that did not decode, for the
52// error: at most 200 bytes, of an answer that may be shorter (a captive
53// portal's page, a proxy's error) — a plain slice would panic on it
54fn answer_start(body string) string {
55 return body#[..200]
56}
57
58// get_pkgbuild_packages searches one pkgbuild repo's site for a needle.
59// the needle is escaped: a query with '&', '#', ' ' or '=' must not break
60// the request or smuggle in extra rpc parameters
61fn get_pkgbuild_packages(base string, needle string) ![]PackagePkgbuild {
62 body := pkgbuild_rpc(
63 '${base}/rpc/v5/search/${urllib.query_escape(needle)}',
64 'search',
65 )!
66 data := json2.decode[PackagesPkgbuild](body) or {
67 return error('failed to decode search results: ${err.msg()} (the answer begins: ${answer_start(body)})')
68 }
69 if err := data.error_text {
70 return error('search failed: ${err}')
71 }
72 return data.results
73}
74
75// get_pkgbuild_info fetches the full record, dependencies included, of each
76// name, from one pkgbuild repo's site. the names are escaped likewise. no
77// names asks for nothing and gets it
78fn get_pkgbuild_info(base string, names []string) ![]PackagePkgbuild {
79 if names.len == 0 {
80 return []PackagePkgbuild{}
81 }
82 args := names.map('arg[]=${urllib.query_escape(it)}').join('&')
83 body := pkgbuild_rpc('${base}/rpc/v5/info?${args}', 'info lookup')!
84 data := json2.decode[PackagesPkgbuild](body) or {
85 return error('failed to decode package info: ${err.msg()} (the answer begins: ${answer_start(body)})')
86 }
87 if err := data.error_text {
88 return error('info lookup failed: ${err}')
89 }
90 return data.results
91}
92
93// pkgbuild_lookup finds name in the pkgbuild repos, tried in config order:
94// the first one that has the package is the one it builds from. it answers
95// the record, the repo's site with no trailing slash (so a clone url joins
96// with exactly one), and the name the config gives the repo; the site is
97// empty when no repo holds the name
98fn pkgbuild_lookup(name string) !(PackagePkgbuild, string, string) {
99 for repo in pkgbuild_repos() {
100 site := repo.servers[0].trim_right('/')
101 infos := get_pkgbuild_info(site, [name])!
102 if infos.len > 0 {
103 return infos[0], site, repo.name
104 }
105 }
106 return PackagePkgbuild{}, '', ''
107}
108
109// pkgbuild_has names whether any pkgbuild repo holds name
110fn pkgbuild_has(name string) !bool {
111 _, site, _ := pkgbuild_lookup(name)!
112 return site != ''
113}
114