diff options
-rw-r--r-- | libs/lpk/luasrc/lpk.lua | 36 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/core.lua | 103 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/core/download.lua (renamed from libs/lpk/luasrc/lpk/state/install.lua) | 0 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/core/install.lua (renamed from libs/lpk/luasrc/lpk/state/resolve.lua) | 0 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/core/resolve.lua (renamed from libs/lpk/luasrc/lpk/state/retreive.lua) | 0 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/core/retreive.lua | 0 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/core/unpack.lua | 0 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/state.lua | 25 | ||||
-rw-r--r-- | libs/lpk/luasrc/lpk/util.lua | 37 |
9 files changed, 141 insertions, 60 deletions
diff --git a/libs/lpk/luasrc/lpk.lua b/libs/lpk/luasrc/lpk.lua index fc2235458f..ae6aefd6b6 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 0000000000..2ba5bfd250 --- /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/state/install.lua b/libs/lpk/luasrc/lpk/core/download.lua index e69de29bb2..e69de29bb2 100644 --- a/libs/lpk/luasrc/lpk/state/install.lua +++ b/libs/lpk/luasrc/lpk/core/download.lua diff --git a/libs/lpk/luasrc/lpk/state/resolve.lua b/libs/lpk/luasrc/lpk/core/install.lua index e69de29bb2..e69de29bb2 100644 --- a/libs/lpk/luasrc/lpk/state/resolve.lua +++ b/libs/lpk/luasrc/lpk/core/install.lua diff --git a/libs/lpk/luasrc/lpk/state/retreive.lua b/libs/lpk/luasrc/lpk/core/resolve.lua index e69de29bb2..e69de29bb2 100644 --- a/libs/lpk/luasrc/lpk/state/retreive.lua +++ b/libs/lpk/luasrc/lpk/core/resolve.lua diff --git a/libs/lpk/luasrc/lpk/core/retreive.lua b/libs/lpk/luasrc/lpk/core/retreive.lua new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/libs/lpk/luasrc/lpk/core/retreive.lua diff --git a/libs/lpk/luasrc/lpk/core/unpack.lua b/libs/lpk/luasrc/lpk/core/unpack.lua new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/libs/lpk/luasrc/lpk/core/unpack.lua diff --git a/libs/lpk/luasrc/lpk/state.lua b/libs/lpk/luasrc/lpk/state.lua deleted file mode 100644 index 29765de009..0000000000 --- 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/util.lua b/libs/lpk/luasrc/lpk/util.lua new file mode 100644 index 0000000000..d398094833 --- /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 |