From d2421dd4f69d5a006fec9bb09ec306eedca9e0f3 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Thu, 22 Jun 2017 02:10:37 +0200 Subject: ap: add subcommand dump-json --- ap.lua | 10 +++++++- aports/dump.lua | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 aports/dump.lua diff --git a/ap.lua b/ap.lua index 692c33b..09afda5 100755 --- a/ap.lua +++ b/ap.lua @@ -1,4 +1,4 @@ -#!/usr/bin/lua5.2 +#!/usr/bin/env lua local lfs = require('lfs') @@ -103,6 +103,14 @@ subcmd["apk-list"] = { end } +subcmd["dump-json"] = { + desc = "Dump all abuilds from aports tree to JSON", + run = function() + local dump = require "aports.dump" + print(dump.pkgs_to_json(db:each_aport())) + end +} + function print_usage() io.write("usage: ap -d SUBCOMMAND [options]\n\nSubcommands are:\n") local k,v diff --git a/aports/dump.lua b/aports/dump.lua new file mode 100644 index 0000000..a78b63e --- /dev/null +++ b/aports/dump.lua @@ -0,0 +1,77 @@ +--------- +-- Dump packages info into JSON. +---- +local cjson = require('cjson') +local pkgmod = require('aports.pkg') + +local get_maintainer = pkgmod.get_maintainer +local push = table.insert + + +-- Tables with this metatable are encoded as an array by our patched cjson. +local empty_array_mt = { __name = 'array' } + +local empty_array = setmetatable({}, empty_array_mt) + +--- Converts pkg's `arch` map into list of architectures. +-- Negated architecture is prefixed by "!". +-- +-- @tparam {[string]=bool,...} arch +-- @treturn {string,...} +local function convert_arch(arch) + local t = {} + for name, value in pairs(arch) do + push(t, value and name or '!'..name) + end + return t +end + +--- "Marks" the given table to be encoded as a JSON array. +local function array(tab) + return #tab == 0 + and empty_array + or setmetatable(tab, empty_array_mt) +end + +--- Converts the `pkg` into a simple table (map) that can be serialized. +-- +-- @tparam table pkg The package table (see @{db.split_apkbuild}). +-- @treturn table A simple map of values. +local function pkg_to_table(pkg) + return { + pkgname = pkg.pkgname, + pkgver = pkg.pkgver, + pkgrel = tonumber(pkg.pkgrel), + pkgdesc = pkg.pkgdesc, + url = pkg.url, + license = pkg.license, + arch = array(convert_arch(pkg.arch)), + depends = array(pkg.depends), + makedepends = array(pkg.makedepends), + checkdepends = array(pkg.checkdepends), + subpackages = array(pkg.subpackages), + source = array(pkg.source), + maintainer = get_maintainer(pkg), + } +end + + +local M = {} + +--- Dumps packages from the given iterator to a map indexed by package name. +function M.pkgs_to_map(iter, state) + local t = {} + for pkg in iter, state do + t[pkg.pkgname] = pkg_to_table(pkg) + end + return t +end + +--- Dumps packages from the given iterator to JSON. +-- @see pkgs_to_map +function M.pkgs_to_json(iter, state) + cjson.encode_sort_keys = true + return cjson.encode(M.pkgs_to_map(iter, state)) +end + +return M -- cgit v1.2.3