vpac / src / fields.v
234 lines · 223 sloc · 6.38 KB · 841315c100c81d9e6b14c4b55a25d4e3d12db762
Raw
1@[has_globals]
2module main
3
4import nspkg
5import time
6import v.profiler
7
8// pkg_field_names are the fields info and search can show. both take the same
9// names through --fields, so a field means the same thing in either command
10const pkg_field_names = ['source', 'name', 'version', 'description', 'arch', 'url', 'licenses',
11 'groups', 'provides', 'depends', 'optdepends', 'required_by', 'optional_for', 'conflicts',
12 'replaces', 'size', 'packager', 'build_date', 'install_date', 'install_reason', 'install_script',
13 'validation']
14
15// search_default_fields are the columns of a search unless --fields says otherwise
16const search_default_fields = ['source', 'name', 'version', 'description']
17
18// cfg_search_fields are the columns the config asks for, (search #:fields):
19// search_default_fields unless it says otherwise. cfg_search_thold is the
20// fuzzy search's threshold when --thold names none, (search #:thold)
21__global cfg_search_fields = ['source', 'name', 'version', 'description']
22__global cfg_search_thold = 0.9
23// what a mode starts from when its flag names nothing: (clean #:keep), the
24// versions of a package the cache keeps, and (tree #:depth), how deep a tree
25// is drawn; 0 leaves each to the mode
26__global cfg_clean_keep = 0
27__global cfg_tree_depth = 0
28
29// parse_fields reads --fields: nothing gives the defaults, 'all' every field,
30// and anything else a comma separated list of field names
31fn parse_fields(spec string, defaults []string) ![]string {
32 if spec == '' {
33 return defaults
34 }
35 if spec == 'all' {
36 return pkg_field_names
37 }
38 mut out := []string{}
39 for f in spec.split(',') {
40 name := f.trim_space()
41 if name !in pkg_field_names {
42 return error('unknown field "${name}", the fields are: ${pkg_field_names.join(', ')}')
43 }
44 out << name
45 }
46 return out
47}
48
49// strs_text is a list of strings as one value: 'a, b'. the list arrives as a
50// borrowed pointer, and strs() walks it without writing to it — the mut
51// binding is only the mutable receiver the method spells, never a write to
52// the C list
53fn strs_text(list &nspkg.List) string {
54 mut l := unsafe { list }
55 return l.strs().join(', ')
56}
57
58// pkg_source is where an nspkg package comes from: 'local', or the sync repo
59fn pkg_source(pkg &nspkg.Package) string {
60 origin := pkg.get_origin()
61 if origin == .file {
62 return 'file'
63 }
64 if origin != .syncdb {
65 return 'local'
66 }
67 db := pkg.get_db() or { return 'remote' }
68 return db.get_name()
69}
70
71// pkg_field_items gives a field's value as separate items, for the fields that
72// wrap their lists across lines. none for the fields whose value is one piece
73// of text
74fn pkg_field_items(pkg &nspkg.Package, field string) ?[]string {
75 return match field {
76 'licenses' {
77 pkg.get_licenses().strs()
78 }
79 'groups' {
80 pkg.get_groups().strs()
81 }
82 'provides' {
83 pkg.get_provides().depends().map(|d| d.compute_string())
84 }
85 'depends' {
86 pkg.get_depends().depends().map(|d| d.compute_string())
87 }
88 // for a sync package libalpm looks through every sync db, as pacman -Sii
89 'required_by' {
90 pkg.compute_required_by()
91 }
92 'optional_for' {
93 pkg.compute_optional_for()
94 }
95 'conflicts' {
96 pkg.get_conflicts().depends().map(|d| d.compute_string())
97 }
98 'replaces' {
99 pkg.get_replaces().depends().map(|d| d.compute_string())
100 }
101 // every optional dependency of its own line: name, then its reason.
102 // an installed one carries pacman's [installed] mark
103 'optdepends' {
104 pkg.get_opt_depends().depends().map(|d| optdepend_text(d, pkg))
105 }
106 else {
107 none
108 }
109 }
110}
111
112// optdepend_text is one line of the optional deps: name, then its reason. an
113// installed package marks the deps the local db satisfies, as pacman's -Qi
114// does; a sync package's list stays bare, like pacman's -Si
115fn optdepend_text(d nspkg.Depend, pkg &nspkg.Package) string {
116 name := d.get_name()
117 desc := d.get_desc()
118 text := if desc == '' { name } else { '${name}: ${desc}' }
119 // the local cache hangs off the package's own db, no separate lookup
120 if pkg.get_origin() != .localdb {
121 return text
122 }
123 if db := pkg.get_db() {
124 if cache := db.get_pkgcache() {
125 if _ := cache.find_satisfier(d.compute_string()) {
126 return text + ' [installed]'
127 }
128 }
129 }
130 return text
131}
132
133// pkg_field is one field of an nspkg package. '' when the field does not apply,
134// such as the install fields of a package that is not installed
135fn pkg_field(pkg &nspkg.Package, source string, field string) string {
136 sync := pkg.get_origin() == .syncdb
137 return match field {
138 'source' {
139 source
140 }
141 'name' {
142 pkg.get_name()
143 }
144 'version' {
145 pkg.get_version()
146 }
147 'description' {
148 pkg.get_desc()
149 }
150 'arch' {
151 pkg.get_arch()
152 }
153 'url' {
154 pkg.get_url()
155 }
156 'licenses' {
157 strs_text(pkg.get_licenses())
158 }
159 'groups' {
160 strs_text(pkg.get_groups())
161 }
162 'provides' {
163 depends_text(pkg.get_provides())
164 }
165 'depends' {
166 depends_text(pkg.get_depends())
167 }
168 // the mark rides along here too, since info's one-value fields and
169 // the listing columns of search read the same sense from the field:
170 // an installed package marks the deps the local db satisfies, as
171 // pacman's -Qi and -Ss listings do
172 'optdepends' {
173 pkg.get_opt_depends().depends().map(|d| optdepend_text(d, pkg)).join(', ')
174 }
175 'required_by' {
176 pkg.compute_required_by().join(', ')
177 }
178 'optional_for' {
179 pkg.compute_optional_for().join(', ')
180 }
181 'conflicts' {
182 depends_text(pkg.get_conflicts())
183 }
184 'replaces' {
185 depends_text(pkg.get_replaces())
186 }
187 'size' {
188 if sync {
189 profiler.format_bytes(u64(pkg.download_size())) + ' to download'
190 } else {
191 profiler.format_bytes(u64(pkg.get_isize()))
192 }
193 }
194 'packager' {
195 pkg.get_packager()
196 }
197 'build_date' {
198 time.unix(pkg.get_build_date()).format_ss()
199 }
200 'install_date' {
201 if sync { '' } else { time.unix(pkg.get_install_date()).format_ss() }
202 }
203 'install_reason' {
204 if sync { '' } else { reason_to_str(pkg.get_reason()) }
205 }
206 'install_script' {
207 if sync {
208 ''
209 } else {
210 if pkg.has_scriptlet() { 'yes' } else { 'no' }
211 }
212 }
213 'validation' {
214 validation_to_str(pkg.get_validation())
215 }
216 else {
217 ''
218 }
219 }
220}
221
222// pkgbuild_field is one field of a pkgbuild result. the rpc only knows some of
223// them; the source is the repo's own name
224fn pkgbuild_field(info PackagePkgbuild, field string, source string) string {
225 return match field {
226 'source' { source }
227 'name' { info.name }
228 'version' { info.version }
229 'description' { info.description }
230 'url' { info.url }
231 'packager' { info.maintainer or { '' } }
232 else { '' }
233 }
234}
235