From: Steven Barth Date: Sun, 27 Jul 2008 17:38:05 +0000 (+0000) Subject: libs/lpk: Implemented register/stack/state machine X-Git-Tag: 0.8.0~563 X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fluci.git;a=commitdiff_plain;h=3b29503f1302ae66a5c2b590eda86334a550b57b libs/lpk: Implemented register/stack/state machine --- diff --git a/libs/lpk/luasrc/lpk.lua b/libs/lpk/luasrc/lpk.lua index fc2235458..ae6aefd6b 100644 --- a/libs/lpk/luasrc/lpk.lua +++ b/libs/lpk/luasrc/lpk.lua @@ -1,38 +1,4 @@ module("luci.lpk", package.seeall) -function getopt( arg, options ) - local tab = {} - local args = {} - for k, v in ipairs(arg) do - if v:sub(1, 2) == "--" then - local x = v:find( "=", 1, true ) - if x then - tab[ v:sub( 3, x-1 ) ] = v:sub( x+1 ) - else - tab[ v:sub( 3 ) ] = true - end - elseif v:sub( 1, 1 ) == "-" then - local y = 2 - local l = #v - local jopt - while ( y <= l ) do - jopt = v:sub( y, y ) - if options:find( jopt, 1, true ) then - if y < l then - tab[ jopt ] = v:sub( y+1 ) - y = l - else - tab[ jopt ] = arg[ k + 1 ] - end - else - tab[ jopt ] = true - end - y = y + 1 - end - else - table.insert(args, v) - end - end - return tab, args -end + diff --git a/libs/lpk/luasrc/lpk/core.lua b/libs/lpk/luasrc/lpk/core.lua new file mode 100644 index 000000000..2ba5bfd25 --- /dev/null +++ b/libs/lpk/luasrc/lpk/core.lua @@ -0,0 +1,103 @@ +module("luci.lpk.core", package.seeall) +require("luci.util") + +Task = luci.util.class() + +function Task.__init__(self, machine, register, start) + self.machine = machine + + -- The queue that has to be processed + self.work = {start} + + -- The queue that has to be processed in case of rollback + self.done = {} + + -- The Task register + self.register = register +end + +function Task.rollback(self) + if #self.done < 1 then + return false + end + + local state = table.remove(self.done) + local ret, err = pcall(state.rollback, state, self.register) + + if ret then + return true + else + return false, err + end +end + +function Task.step(self) + local state = table.remove(self.work) + local ret, next = pcall(state.process, state, self.register) + + if ret then + if next then + local nstate = self.machine:state(next) + if nstate then + table.insert(self.work, state) + table.insert(self.work, nstate) + else + self.register.error = "Unknown state: " .. next + return false + end + else + table.insert(self.done, state) + end + + return #self.work > 0 + else + self.register.error = next + return false + end +end + +function Task.perform(self) + while self:step() do + end + + if not self.register.error then + return true + else + local stat, err + repeat + stat, err = self:rollback() + until not stat + + assert(not err, "Machine broken!") + + return false, self.register.error + end +end + + +Machine = luci.util.class() + +function Machine.__init__(self, namespace) + self.namespace = namespace or _NAME +end + +function Machine.state(self, name) + local ret, state = pcall(require, self.namespace .. "." .. name) + return ret and state +end + +function Machine.task(self, name, ...) + local start = self:state(name) + + if not start or not start.entry then + error("No such command: " .. name) + end + + local register = {} + + if start:entry(register) then + return Task(self, register, start) + else + return nil, register.error + end +end diff --git a/libs/lpk/luasrc/lpk/core/download.lua b/libs/lpk/luasrc/lpk/core/download.lua new file mode 100644 index 000000000..e69de29bb diff --git a/libs/lpk/luasrc/lpk/core/install.lua b/libs/lpk/luasrc/lpk/core/install.lua new file mode 100644 index 000000000..e69de29bb diff --git a/libs/lpk/luasrc/lpk/core/resolve.lua b/libs/lpk/luasrc/lpk/core/resolve.lua new file mode 100644 index 000000000..e69de29bb diff --git a/libs/lpk/luasrc/lpk/core/retreive.lua b/libs/lpk/luasrc/lpk/core/retreive.lua new file mode 100644 index 000000000..e69de29bb diff --git a/libs/lpk/luasrc/lpk/core/unpack.lua b/libs/lpk/luasrc/lpk/core/unpack.lua new file mode 100644 index 000000000..e69de29bb diff --git a/libs/lpk/luasrc/lpk/state.lua b/libs/lpk/luasrc/lpk/state.lua deleted file mode 100644 index 29765de00..000000000 --- a/libs/lpk/luasrc/lpk/state.lua +++ /dev/null @@ -1,25 +0,0 @@ -module("luci.lpk.state", package.seeall) -require("luci.util") - -State = luci.util.class() - -function State.__init__() - self.poststates = {} - self.prestates = {} -end - -function State.add_poststate(state) - table.insert(self.poststates, state) -end - -function State.add_prestate(state) - table.insert(self.prestates, state) -end - -function State.process() - -end - -function State.handle() - -end diff --git a/libs/lpk/luasrc/lpk/state/install.lua b/libs/lpk/luasrc/lpk/state/install.lua deleted file mode 100644 index e69de29bb..000000000 diff --git a/libs/lpk/luasrc/lpk/state/resolve.lua b/libs/lpk/luasrc/lpk/state/resolve.lua deleted file mode 100644 index e69de29bb..000000000 diff --git a/libs/lpk/luasrc/lpk/state/retreive.lua b/libs/lpk/luasrc/lpk/state/retreive.lua deleted file mode 100644 index e69de29bb..000000000 diff --git a/libs/lpk/luasrc/lpk/util.lua b/libs/lpk/luasrc/lpk/util.lua new file mode 100644 index 000000000..d39809483 --- /dev/null +++ b/libs/lpk/luasrc/lpk/util.lua @@ -0,0 +1,37 @@ +module("luci.lpk.util", package.seeall) + +function getopt( arg, options ) + local tab = {} + local args = {} + for k, v in ipairs(arg) do + if v:sub(1, 2) == "--" then + local x = v:find( "=", 1, true ) + if x then + tab[ v:sub( 3, x-1 ) ] = v:sub( x+1 ) + else + tab[ v:sub( 3 ) ] = true + end + elseif v:sub( 1, 1 ) == "-" then + local y = 2 + local l = #v + local jopt + while ( y <= l ) do + jopt = v:sub( y, y ) + if options:find( jopt, 1, true ) then + if y < l then + tab[ jopt ] = v:sub( y+1 ) + y = l + else + tab[ jopt ] = arg[ k + 1 ] + end + else + tab[ jopt ] = true + end + y = y + 1 + end + else + table.insert(args, v) + end + end + return tab, args +end \ No newline at end of file