libs/lpk: Implemented register/stack/state machine
authorSteven Barth <steven@midlink.org>
Sun, 27 Jul 2008 17:38:05 +0000 (17:38 +0000)
committerSteven Barth <steven@midlink.org>
Sun, 27 Jul 2008 17:38:05 +0000 (17:38 +0000)
12 files changed:
libs/lpk/luasrc/lpk.lua
libs/lpk/luasrc/lpk/core.lua [new file with mode: 0644]
libs/lpk/luasrc/lpk/core/download.lua [new file with mode: 0644]
libs/lpk/luasrc/lpk/core/install.lua [new file with mode: 0644]
libs/lpk/luasrc/lpk/core/resolve.lua [new file with mode: 0644]
libs/lpk/luasrc/lpk/core/retreive.lua [new file with mode: 0644]
libs/lpk/luasrc/lpk/core/unpack.lua [new file with mode: 0644]
libs/lpk/luasrc/lpk/state.lua [deleted file]
libs/lpk/luasrc/lpk/state/install.lua [deleted file]
libs/lpk/luasrc/lpk/state/resolve.lua [deleted file]
libs/lpk/luasrc/lpk/state/retreive.lua [deleted file]
libs/lpk/luasrc/lpk/util.lua [new file with mode: 0644]

index fc22354..ae6aefd 100644 (file)
@@ -1,38 +1,4 @@
 module("luci.lpk", package.seeall)
 
 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 (file)
index 0000000..2ba5bfd
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
diff --git a/libs/lpk/luasrc/lpk/core/install.lua b/libs/lpk/luasrc/lpk/core/install.lua
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/libs/lpk/luasrc/lpk/core/resolve.lua b/libs/lpk/luasrc/lpk/core/resolve.lua
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/libs/lpk/luasrc/lpk/core/retreive.lua b/libs/lpk/luasrc/lpk/core/retreive.lua
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/libs/lpk/luasrc/lpk/core/unpack.lua b/libs/lpk/luasrc/lpk/core/unpack.lua
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/libs/lpk/luasrc/lpk/state.lua b/libs/lpk/luasrc/lpk/state.lua
deleted file mode 100644 (file)
index 29765de..0000000
+++ /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 (file)
index e69de29..0000000
diff --git a/libs/lpk/luasrc/lpk/state/resolve.lua b/libs/lpk/luasrc/lpk/state/resolve.lua
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/libs/lpk/luasrc/lpk/state/retreive.lua b/libs/lpk/luasrc/lpk/state/retreive.lua
deleted file mode 100644 (file)
index e69de29..0000000
diff --git a/libs/lpk/luasrc/lpk/util.lua b/libs/lpk/luasrc/lpk/util.lua
new file mode 100644 (file)
index 0000000..d398094
--- /dev/null
@@ -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