vpac / src / testdb / testdb.v
60 lines · 55 sloc · 1.85 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module testdb
2
3import os
4
5// testdb writes libalpm local dbs for the tests: every test file is a
6// binary of its own, so a writer they share lives in a module they import
7// and vpac itself never does — it is in no build of the program.
8
9// Pkg is one installed package of a written db: what its desc says, and the
10// entries of its file list (directories with their trailing slash, as
11// libalpm keeps them)
12pub struct Pkg {
13pub:
14 name string
15 version string = '1.0-1'
16 reason int // 0 explicit, 1 a dependency
17 depends []string
18 optdepends []string
19 provides []string
20 conflicts []string
21 groups []string
22 files []string
23 // no_files leaves the file list out altogether, a broken entry
24 no_files bool
25}
26
27// desc is the text of a package's desc file: the sections that have
28// something to say, in libalpm's spelling
29pub fn (p Pkg) desc() string {
30 mut out := '%NAME%\n${p.name}\n\n%VERSION%\n${p.version}\n\n%ARCH%\nany\n\n%REASON%\n${p.reason}\n\n'
31 for key, items in {
32 'DEPENDS': p.depends
33 'OPTDEPENDS': p.optdepends
34 'PROVIDES': p.provides
35 'CONFLICTS': p.conflicts
36 'GROUPS': p.groups
37 } {
38 if items.len > 0 {
39 out += '%${key}%\n${items.join('\n')}\n\n'
40 }
41 }
42 return out
43}
44
45// write_local lays out a local db (version 9) under dbpath: the version
46// file, and one dir per package, named name-version, with its desc and its
47// file list. it adds to a db that is there; no packages write an empty one
48pub fn write_local(dbpath string, pkgs []Pkg) ! {
49 local := os.join_path(dbpath, 'local')
50 os.mkdir_all(local)!
51 os.write_file(os.join_path(local, 'ALPM_DB_VERSION'), '9\n')!
52 for p in pkgs {
53 dir := os.join_path(local, '${p.name}-${p.version}')
54 os.mkdir_all(dir)!
55 os.write_file(os.join_path(dir, 'desc'), p.desc())!
56 if !p.no_files {
57 os.write_file(os.join_path(dir, 'files'), '%FILES%\n${p.files.join('\n')}\n\n')!
58 }
59 }
60}
61