Skip to content

Commit

Permalink
kpkg: build: simplify error handling by moving checks internally
Browse files Browse the repository at this point in the history
  • Loading branch information
kreatoo committed Jan 28, 2024
1 parent b8a0ac1 commit d91dabb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 30 deletions.
43 changes: 15 additions & 28 deletions kpkg/commands/buildcmd.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ proc cleanUp() {.noconv.} =


proc fakerootWrap(srcdir: string, path: string, root: string, input: string,
autocd = "", tests = false, isTest = false, existsTest = 1, target = "default"): int =
autocd = "", tests = false, isTest = false, existsTest = 1, target = "default", typ: string): int =
## Wraps command with fakeroot and executes it.

if (isTest and not tests) or (tests and existsTest != 0):
return 0

if not isEmptyOrWhitespace(autocd):
return execCmdKpkg("fakeroot -- /bin/sh -c '. "&path&"/run && export DESTDIR="&root&" && export ROOT=$DESTDIR && cd "&autocd&" && "&input&"'")
return execCmdKpkg("fakeroot -- /bin/sh -c '. "&path&"/run && export DESTDIR="&root&" && export ROOT=$DESTDIR && cd "&autocd&" && "&input&"'", typ)

return execCmdKpkg("fakeroot -- /bin/sh -c '. "&path&"/run && export DESTDIR="&root&" && export ROOT=$DESTDIR && cd '"&srcdir&"' && "&input&"'")
return execCmdKpkg("fakeroot -- /bin/sh -c '. "&path&"/run && export DESTDIR="&root&" && export ROOT=$DESTDIR && cd '"&srcdir&"' && "&input&"'", typ)

proc builder*(package: string, destdir: string,
root = "/opt/kpkg/build", srcdir = "/opt/kpkg/srcdir", offline = false,
Expand Down Expand Up @@ -275,10 +275,6 @@ proc builder*(package: string, destdir: string,
if execCmdKpkg("su -s /bin/sh _kpkg -c '. "&path&"/run"&" && prepare'") != 0:
err("prepare failed", true)

var cmd: int
var cmd2: int
var cmd3: int

# Run ldconfig beforehand for any errors
discard execProcess("ldconfig")

Expand Down Expand Up @@ -350,31 +346,22 @@ proc builder*(package: string, destdir: string,
if pkg.sources.split(" ").len == 1:
if existsPrepare == 0:
debug "prepare() exist, autocd will not run"
cmd = execCmdKpkg(sboxWrap(cmdStr))
cmd2 = fakerootWrap(srcdir, path, root, "check", tests = tests,
isTest = true, existsTest = existsTest)
cmd3 = fakerootWrap(srcdir, path, root, cmd3Str)
discard execCmdKpkg(sboxWrap(cmdStr), "build")
discard fakerootWrap(srcdir, path, root, "check", tests = tests,
isTest = true, existsTest = existsTest, typ = "Tests")
discard fakerootWrap(srcdir, path, root, cmd3Str, typ = "Installation")
else:
debug "prepare() doesn't exist, autocd will run"
cmd = execCmdKpkg(sboxWrap("cd "&folder&" && "&cmdStr))
cmd2 = fakerootWrap(srcdir, path, root, "check", folder,
tests = tests, isTest = true, existsTest = existsTest)
cmd3 = fakerootWrap(srcdir, path, root, cmd3Str, folder)
discard execCmdKpkg(sboxWrap("cd "&folder&" && "&cmdStr), "build")
discard fakerootWrap(srcdir, path, root, "check", folder,
tests = tests, isTest = true, existsTest = existsTest, typ = "Tests")
discard fakerootWrap(srcdir, path, root, cmd3Str, folder, typ = "Installation")

else:
cmd = execCmdKpkg(sboxWrap(cmdStr))
cmd2 = fakerootWrap(srcdir, path, root, "check", tests = tests,
isTest = true, existsTest = existsTest)
cmd3 = fakerootWrap(srcdir, path, root, cmd3Str)

if cmd != 0:
err("build failed")

if cmd2 != 0:
err("Tests failed")

if cmd3 != 0:
err("Installation failed")
discard execCmdKpkg(sboxWrap(cmdStr), "build")
discard fakerootWrap(srcdir, path, root, "check", tests = tests,
isTest = true, existsTest = existsTest, typ = "Tests")
discard fakerootWrap(srcdir, path, root, cmd3Str, typ = "Installation")

var tarball = "/var/cache/kpkg/archives/arch/"

Expand Down
9 changes: 7 additions & 2 deletions kpkg/modules/processes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import logger
import osproc
import streams

proc execCmdKpkg*(command: string): int =
proc execCmdKpkg*(command: string, error = "none"): int =
# Like execCmdEx, but with outputs.
let process = startProcess(command, options = {poEvalCommand, poStdErrToStdOut})
let outp = outputStream(process)
Expand All @@ -12,7 +12,12 @@ proc execCmdKpkg*(command: string): int =
while outp.readLine(line):
echo line

return waitForExit(process)
let res = waitForExit(process)

if error != "none" and res != 0:
err error&" failed"

return res

proc getPidKpkg(): string =
# Gets current pid from /proc/self.
Expand Down

0 comments on commit d91dabb

Please sign in to comment.