summaryrefslogtreecommitdiffstats
path: root/aports.lua
blob: cd16817638729f520316d86d1cf516bcfa5d2da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env luajit

-- lua turbo application

local turbo = require "turbo"
local inspect = require "inspect"

function string.begins(str, prefix)
    return str:sub(1,#prefix)==prefix
end

local tpl = turbo.web.Mustache.TemplateHelper("./tpl")

local ContentsRenderer = class("ContentsRenderer", turbo.web.RequestHandler)

function ContentsRenderer:get()
    local args = {
        filename = self:get_argument("filename","", true),
        arch = self:get_argument("arch", "x86", true),
    }
    local table = { [args.arch] = true }
    if args.filename ~= "" then
        local result = QueryContents(args)
        if next(result) ~= nil then
            table.rows = result
        end
    end
    table.contents = true
    table.filename = args.filename
    table.header = tpl:render("header.tpl", table)
    table.footer = tpl:render("footer.tpl", table)
    local page = tpl:render(self.options, table)
    self:write(page)
end

local PackagesRenderer = class("PackagesRenderer", turbo.web.RequestHandler)

function PackagesRenderer:get()
    local args = {
        package = self:get_argument("package","", true),
        arch = self:get_argument("arch", "x86", true),
    }
    local table = { [args.arch] = true }
    if args.package == "" then
        args.package = "%"
    end
    local result = QueryPackages(args)
    if next(result) ~= nil then
        table.rows = result
    end
    table.packages = true
    table.header = tpl:render("header.tpl", table)
    table.footer = tpl:render("footer.tpl", table)
    local page = tpl:render(self.options, table)
    self:write(page)
end

local PackageRenderer = class("PackageRenderer", turbo.web.RequestHandler)

function PackageRenderer:get(arch, name)
    local table = QueryPackage(name, arch)
    if table ~= nil then
        table.deps = QueryDeps(table.deps)
        table.reqbys = QueryRequiredBy(table.provides)
        table.subpkgs = QuerySubPackages(table.origin, table.name)
        table.maintainer = string.gsub(table.maintainer, '<.*>', '')
        for k in pairs (table) do
            if table[k] == "" then
                table[k] = nil
            end
        end
    else
        table = {}
    end
    table.header = tpl:render("header.tpl")
    table.footer = tpl:render("footer.tpl")
    local page = tpl:render(self.options, table)
    self:write(page)
end

function QueryContents(terms)
    require('DBI')
    local dbh = assert(DBI.Connect('SQLite3', 'db/filelist.db'))
    local sth = assert(dbh:prepare('select * from filelist where file like ? and arch like ? limit 100'))
    sth:execute(terms.filename, terms.arch)
    local r = {}
    for row in sth:rows(true) do
        r[#r + 1] = {
            file = "/" .. row.path .. "/" .. row.file,
            pkgname = row.pkgname,
            repo = row.repo,
            arch = row.arch,
        }
    end
    sth:close()
    return r
end

function QueryPackages(terms)
    require('DBI')
    local dbh = assert(DBI.Connect('SQLite3', 'db/apkindex.db'))
    local sth = assert(dbh:prepare('select name, version, url, lic, desc, arch, maintainer, datetime(build_time, \'unixepoch\') as build_time from apkindex where name like ? ORDER BY build_time DESC limit 100'))
    sth:execute(terms.package)
    local r = {}
    for row in sth:rows(true) do
        r[#r + 1] = {
            package = row.name,
            version = row.version,
            project = row.url,
            license = row.lic,
            desc = row.desc,
            arch = row.arch,
            repo = "unk",
            maintainer = string.gsub(row.maintainer, '<.*>', ''),
            bdate = row.build_time
        }
    end
    sth:close()
    return r
end

function QueryPackage(name, arch)
    require('DBI')
    local dbh = assert(DBI.Connect('SQLite3', 'db/apkindex.db'))
    local sth = assert(dbh:prepare('select *, datetime(build_time, \'unixepoch\') as build_time from apkindex where name like ? and arch like ? limit 1'))
    sth:execute(name, arch)
    local r = sth:fetch(true)
    sth:close()
    return r
end

function QueryDeps(deps)
    require('DBI')
    local names = {}
    local dbh = assert(DBI.Connect('SQLite3', 'db/apkindex.db'))
    local sth = assert(dbh:prepare('select name from apkindex where provides like ?'))
    for _,k in pairs (deps:split(" ")) do
        if k:begins('so:') then
            sth:execute("%"..k.."%")
            local l = sth:fetch(true)
            if l ~= nil then
                names[l.name] = l.name
            end
        else
            names[k] = k
        end
    end
    sth:close()
    local r = {}
    for _,name in pairs (names) do
        r[#r+1] = {dep=name}
    end
    if next(r) ~= nil then
        return r
    end
end

function QueryRequiredBy(provides)
    require('DBI')
    local names = {}
    local dbh = assert(DBI.Connect('SQLite3', 'db/apkindex.db'))
    local sth = assert(dbh:prepare('select name from apkindex where deps like ?'))
    for _,d in pairs (provides:split(" ")) do
        if d:begins('so:') then
            d = string.gsub(d, '=.*', '')
            sth:execute("%"..d.."%")
            for row in sth:rows(true) do
                if row ~= nil then
                    names[row.name] = row.name
                end
            end
        end
    end
    sth:close()
    local r = {}
    for _,name in pairs (names) do
        r[#r+1] = {reqby=name}
    end
    if next(r) ~= nil then
        return r
    end
end

function QuerySubPackages(origin, name)
    require('DBI')
    local names = {}
    local dbh = assert(DBI.Connect('SQLite3', 'db/apkindex.db'))
    local sth = assert(dbh:prepare('select name from apkindex where origin like ?'))
    sth:execute(origin)
    local r = {}
    for row in sth:rows(true) do
        if row.name ~= name then
            r[#r+1] = {subpkg=row.name}
        end
    end
    sth:close()
    if next(r) ~= nil then
        return r
    end
end

turbo.web.Application({
    {"^/$", turbo.web.RedirectHandler, "/packages"},
    {"^/contents$", ContentsRenderer, "contents.tpl"},
    {"^/packages$", PackagesRenderer, "packages.tpl"},
    {"^/package/(.*)/(.*)$", PackageRenderer, "package.tpl"},
    {"/assets/(.*)$", turbo.web.StaticFileHandler, "assets/"},
}):listen(8888)
turbo.ioloop.instance():start()