diff options
-rw-r--r-- | config.lua | 14 | ||||
-rwxr-xr-x | generate-html.lua | 11 | ||||
-rwxr-xr-x | generate-json.lua | 26 | ||||
-rw-r--r-- | utils.lua | 22 |
4 files changed, 44 insertions, 29 deletions
diff --git a/config.lua b/config.lua new file mode 100644 index 0000000..62a87a9 --- /dev/null +++ b/config.lua @@ -0,0 +1,14 @@ +local conf = {} + +conf.version = "v0.0.1" +conf.apkindex_list = "apkindex.list" +conf.mirrors_yaml = "https://git.alpinelinux.org/cgit/aports/plain/main/alpine-mirrors/mirrors.yaml" +conf.master_url = "http://rsync.alpinelinux.org/alpine/" +conf.outdir = "_out" +conf.mirrors_html = "index.html" +conf.mirrors_json = "mirror-status.json" +conf.http_timeout = 3 +conf.archs = { "x86", "x86_64", "armhf", "aarch64", "ppc64le", "s390x" } +conf.repos = { "main", "community", "testing", "backports" } + +return conf diff --git a/generate-html.lua b/generate-html.lua index 7b4772c..25d4ddd 100755 --- a/generate-html.lua +++ b/generate-html.lua @@ -4,10 +4,7 @@ local json = require("cjson") local inspect = require("inspect") local lustache = require("lustache") local utils = require("utils") - -local outdir = "_out" -local mirrors_html = "index.html" -local mirrors_json = "mirror-status.json" +local conf = require("config") function get_branches(indexes) local res = {} @@ -150,9 +147,11 @@ function build_tables(indexes) return res end -local indexes = json.decode(utils.read_file(outdir.."/"..mirrors_json)) +local out_json = ("%s/%s"):format(conf.outdir, conf.mirrors_json) +local indexes = json.decode(utils.read_file(out_json)) local thead = get_branches(indexes) table.insert(thead, 1, "branch/release") local view = { lupdate = os.date("%c", indexes.date), mirrors = build_tables(indexes), thead = thead } local tpl = utils.read_file("index.tpl") -utils.write_file(outdir.."/"..mirrors_html, lustache:render(tpl, view)) +local out_html = ("%s/%s"):format(conf.outdir, conf.mirrors_html) +utils.write_file(out_html, lustache:render(tpl, view)) diff --git a/generate-json.lua b/generate-json.lua index 1af51cb..d2e7148 100755 --- a/generate-json.lua +++ b/generate-json.lua @@ -5,20 +5,14 @@ local request = require("http.request") local yaml = require("yaml") local json = require("cjson") local utils = require("utils") - -local app_version = "v0.0.1" -local apkindex_list = "apkindex.list" -local mirrors_yaml = "https://git.alpinelinux.org/cgit/aports/plain/main/alpine-mirrors/mirrors.yaml" -local master = "http://rsync.alpinelinux.org/alpine/" -local output = "_out/mirror-status.json" -local http_timeout = 3 +local conf = require("config") ---- -- convert apkindex list to a table function get_apkindexes() local res = {} local qty = 0 - for line in io.lines(apkindex_list) do + for line in io.lines(conf.apkindex_list) do branch, repo, arch = line:match("^alpine/(.*)/(.*)/(.*)/APKINDEX.tar.gz") if type(res[branch]) == "nil" then res[branch] = {} end if type(res[branch][repo]) == "nil" then res[branch][repo] = {} end @@ -62,7 +56,7 @@ end function get_index_status(uri) local res = {} local status, modified - local headers = request.new_from_uri(uri):go(http_timeout) + local headers = request.new_from_uri(uri):go(conf.http_timeout) if headers then status = headers:get(":status") else @@ -77,6 +71,7 @@ end --- write results to json file on disk function write_json(t) + local output = ("%s/%s"):format(conf.outdir, conf.mirrors_json) local f = assert(io.open(output, "w")) local json = assert(json.encode(t)) f:write(json) @@ -96,12 +91,13 @@ function check_apkindexes(mirror) local branches = {} local qty = 0 local cnt = 0 + local allowed_archs = utils.to_list(conf.archs) for branch in utils.kpairs(indexes, utils.sort_branch) do local repos = {} for repo in utils.kpairs(indexes[branch], utils.sort_repo) do local archs = {} for arch in utils.kpairs(indexes[branch][repo], utils.sort_arch) do - if type(utils.allowed.archs[arch]) == "number" then + if type(allowed_archs[arch]) == "number" then local uri = ("%s/%s/%s/%s/APKINDEX.tar.gz"):format(mirror, branch, repo, arch) status, modified = get_index_status(uri) table.insert(archs, {name=arch, status=status, modified=modified}) @@ -119,7 +115,7 @@ end function process_mirrors() local res = {} - local mirrors = get_mirrors(mirrors_yaml) + local mirrors = get_mirrors(conf.mirrors_yaml) for idx,mirror in ipairs(mirrors) do local start_time = os.time() res[idx] = {} @@ -133,10 +129,10 @@ function process_mirrors() end function process_master() - print(("Getting indexes from master: %s"):format(master)) + print(("Getting indexes from master: %s"):format(conf.master_url)) local res = {} - res.url = master - res.branch = check_apkindexes(master) + res.url = conf.master_url + res.branch = check_apkindexes(conf.master_url) return res end @@ -145,6 +141,6 @@ write_json( master = process_master(), mirrors = process_mirrors(), date = os.time(), - version = app_version + version = conf.version } ) @@ -1,10 +1,6 @@ -local M = {} - +local conf = require("config") -M.allowed = { - archs = { x86=1, x86_64=2, armhf=3, aarch64=4, ppc64le=5, s390x=6 }, - repos = { main=1, community=2, testing=3, backports=4 } -} +local M = {} function M.in_array(t, value) for k,v in ipairs(t) do @@ -28,6 +24,16 @@ function M.write_file(file, string) end ---- +-- convert array to list +function M.to_list(t) + local res = {} + for k,v in ipairs(t) do + res[v] = k + end + return res +end + +---- -- table iterator which sorts on keys function M.kpairs(t, f) local keys = {} @@ -51,7 +57,7 @@ end ---- -- repo sort function for kpairs function M.sort_repo(a,b) - local repos = M.allowed.repos + local repos = M.to_list(conf.repos) if type(repos[a]) == "number" and type(repos[b]) == "number" then if repos[a] < repos[b] then return true end end @@ -60,7 +66,7 @@ end ---- -- arch sort function for kpairs function M.sort_arch(a,b) - local archs = M.allowed.archs + local archs = M.to_list(conf.archs) if type(archs[a]) == "number" and type(archs[b]) == "number" then if archs[a] < archs[b] then return true end end |