bliss

KISS in Lua
git clone git://bvnf.space/bliss.git
Log | Files | Refs | README | LICENSE

commit cd6f67772e58915ef46c15dcd80d740dd45bd6b7
parent 913d8433b4c2f45838b0dce608699b9af2b5e7cc
Author: phoebos <ben@bvnf.space>
Date:   Thu, 29 Jun 2023 20:36:05 +0100

utils.run: take an array instead of a string

Also take an optional table of environ variables which will be *added*
to the child processes environment (so no need to include variables like
HOME etc).
run now takes an array - think of execvpe(2).

run_shell exists with the original functionality - running a string in a
shell, mainly for easy pipes; but ideally this won't be needed in the
future.

Diffstat:
Mbliss/archive.lua | 4++--
Mbliss/build.lua | 13+++++++------
Mbliss/download.lua | 15++++++++-------
Mbliss/utils.lua | 35++++++++++++++++++++++++++++++++---
4 files changed, 49 insertions(+), 18 deletions(-)

diff --git a/bliss/archive.lua b/bliss/archive.lua @@ -5,7 +5,7 @@ local unistd = require 'posix.unistd' -- extracts tarball to PWD local function tar_extract(tarball) - if not utils.run("tar xf '" .. tarball .. "'") then + if not utils.run("tar", {"xf", tarball}) then utils.die("failed to extract "..tarball) end @@ -41,7 +41,7 @@ local function tar_create(env, p) xz = "xz -z", zst = "zstd -z", } - if not utils.run("tar cf - . | " .. compress_map[env.COMPRESS] .. " > " .. _tar_file) then os.exit(false) end + if not utils.run_shell("tar cf - . | " .. compress_map[env.COMPRESS] .. " > " .. _tar_file) then os.exit(false) end -- TODO: cd $OLDPWD? diff --git a/bliss/build.lua b/bliss/build.lua @@ -26,7 +26,7 @@ local function build_extract(env, p) utils.mkcd(env.mak_dir..'/'..p.pkg..'/'..(p.sources[k][2] or '')) local r = p.sources[k][1] if r:match("^git%+") then - utils.run("cp -PRf '" .. v .. "/.' .") + utils.run("cp", {"-PRf", v.."/.", "."}) elseif r:match("%.tar$") or r:match("%.tar%...$") or r:match("%.tar%....$") @@ -34,25 +34,26 @@ local function build_extract(env, p) or r:match("%.t.z") then archive.tar_extract(v) else - utils.run("cp -PRf '" .. v .. "' .") + utils.run("cp", {"-PRf", v, "."}) end end end local function build_build(env, p) - utils.mkcd(env.mak_dir..'/'..p.pkg, env.pkg_dir..'/'..p.pkg..'/'..env.pkg_db) + local destdir = env.pkg_dir .. '/' .. p.pkg + utils.mkcd(env.mak_dir..'/'..p.pkg, destdir..'/'..env.pkg_db) utils.log(p.pkg, "Starting build") -- TODO: tee log local f = p.repo_dir .. '/build' - -- TODO: env (execp?) - if not utils.run(f .. " " ..env.pkg_dir..'/'..p.pkg .. " " .. p.ver) then + -- TODO: env + if not utils.run(f, {destdir, p.ver}) then utils.die(p.pkg, "Build failed") end -- copy repository files to the package directory. - if not utils.run("cp -LRf \"" .. p.repo_dir .. "\" \"" .. env.pkg_dir .. "/" .. p.pkg .. "/" .. env.pkg_db .. "/\"") then os.exit(false) end + if not utils.run("cp", {"-LRf", p.repo_dir, destdir .. "/" .. env.pkg_db .. "/"}) then os.exit(false) end utils.log(p.pkg, "Successfully built package") end diff --git a/bliss/download.lua b/bliss/download.lua @@ -15,17 +15,18 @@ local function http(env, p, source, dest) -- TODO: use a library? if not env.GET then utils.die("No http download utility available") end local args_map = { - aria2c = ' -d / -o ', - axel = ' -o ', - curl = ' -fLo ', - wget = ' -O ', - wget2 = ' -O ', + aria2c = {'-d', '/', '-o'}, + axel = {'-o'}, + curl = {'-fLo'}, + wget = {'-O'}, + wget2 = {'-O'}, } local args = args_map[libgen.basename(env.GET)] or utils.die("'"..env.GET.."' is unsupported as KISS_GET") --TODO: tmp file - local cmd = env.GET .. args .. dest .. ' ' .. source - if not utils.run(cmd) then + table.insert(args, dest) + table.insert(args, source) + if not utils.run(env.GET, args) then utils.die(p, "Failed to download " .. source) end end diff --git a/bliss/utils.lua b/bliss/utils.lua @@ -1,10 +1,12 @@ -- Copyright 2023 phoebos local sys_stat = require 'posix.sys.stat' +local sys_wait = require 'posix.sys.wait' local unistd = require 'posix.unistd' +local stdlib = require 'posix.stdlib' local signal = require 'posix.signal' local colors = {"", "", ""} -local setup, setup_colors, check_execute, get_available, get_pkg_clean, trap_on, trap_off, split, mkdirp, mkcd, rm_rf, log, warn, die, prompt, run, capture, shallowcopy +local setup, setup_colors, check_execute, get_available, get_pkg_clean, trap_on, trap_off, split, mkdirp, mkcd, rm_rf, log, warn, die, prompt, run_shell, run, capture, shallowcopy function setup() colors = setup_colors() @@ -182,12 +184,38 @@ function prompt(env, msg) end end -function run(cmd) +-- cmd is a string. +function run_shell(cmd) io.stderr:write(cmd.."\n") - -- faster to use fork + posix.unistd.execp? return os.execute(cmd) end +-- path is a string, cmd is an array, env is a table +-- Despite the variable name, path doesn't have to be absolute because execp is used. +function run(path, cmd, env) + io.stderr:write(path .. ' ' .. table.concat(cmd, ' ', 1) .. '\n') + + local pid = unistd.fork() + + if not pid then + die("fork failed") + elseif pid == 0 then + if env then + for k,v in pairs(env) do + stdlib.setenv(k, v) + end + end + + unistd.execp(path, cmd) + else + local _, msg, code = sys_wait.wait(pid) + if msg ~= "exited" then + die("run failed: " .. msg) + end + return code == 0 + end +end + -- Returns an array of lines printed by cmd function capture(cmd) local p = io.popen(cmd, 'r') @@ -216,6 +244,7 @@ local M = { warn = warn, die = die, prompt = prompt, + run_shell = run_shell, run = run, capture = capture, shallowcopy = shallowcopy,