OpenSDE Packages Database (without history before r20070)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
5.2 KiB

  1. #!/usr/bin/lua
  2. -- --- T2-COPYRIGHT-NOTE-BEGIN ---
  3. -- This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  4. --
  5. -- T2 SDE: package/.../sam/sam.lua
  6. -- Copyright (C) 2006 The T2 SDE Project
  7. --
  8. -- More information can be found in the files COPYING and README.
  9. --
  10. -- This program is free software; you can redistribute it and/or modify
  11. -- it under the terms of the GNU General Public License as published by
  12. -- the Free Software Foundation; version 2 of the License. A copy of the
  13. -- GNU General Public License can be found in the file COPYING.
  14. -- --- T2-COPYRIGHT-NOTE-END ---
  15. -- identification
  16. local _NAME = "SAM"
  17. local _VERSION = "0.0-devel"
  18. local _COPYRIGHT = "Copyright (C) 2006 The T2 SDE Project"
  19. local _DESCRIPTION = "System Administration Manager for systems based on T2"
  20. -- SAM namespace
  21. sam = sam or {
  22. command = {}
  23. }
  24. require "sam.log"
  25. -- default options
  26. sam.opt = sam.opt or {
  27. loglevel = sam.log.DEBUG, -- sam.log.WARN
  28. }
  29. --[[ DESCRIPTION ] ----------------------------------------------------------
  30. Provided functions:
  31. * sam.command["command-name"](args...)
  32. * sam.command["command-name"].main(args...)
  33. Execute a command (extended by modulues) with given arguments.
  34. The only built-in command currently is "help".
  35. --]] ------------------------------------------------------------------------
  36. -- fprintf alike helper function
  37. local function fprintf(stream, ...)
  38. stream:write( string.format(unpack(arg)) )
  39. end
  40. -- MODULES ------------------------------------------------------------------
  41. -- load_module(name)
  42. -- Load the previously detected module.
  43. local function load_module(name)
  44. sam.info(_NAME, "Loading module %s (from %s)\n", name, sam.command[name]._module._FILE)
  45. -- sanity check for module info
  46. if not sam.command[name] or not sam.command[name]._module then
  47. sam.error(_NAME, "No such command module '%s', giving up.\n", name)
  48. return
  49. end
  50. -- load and execute the module
  51. local module, emsg = loadfile(sam.command[name]._module._FILE)
  52. if not module then
  53. print(emsg)
  54. os.exit(-1)
  55. end
  56. module = module()
  57. -- module sanity check
  58. if not module.main or not module._NAME then
  59. sam.error(_NAME, "Command module '%s' is probably not a SAM module.\n", name)
  60. return
  61. end
  62. -- copy module data
  63. sam.command[name]._NAME = module._NAME
  64. sam.command[name]._DESCRIPTION = module._DESCRIPTION
  65. sam.command[name]._USAGE = module._USAGE
  66. sam.command[name]._module.main = module.main
  67. sam.command[name]._load = nil
  68. -- set real methods
  69. sam.command[name].main = function(self,...) return self._module.main(unpack(arg)) end
  70. -- set correct metatable
  71. setmetatable(sam.command[name], {
  72. __call = function(self, ...) return self._module.main(unpack(arg)) end,
  73. })
  74. end
  75. -- detect_modules()
  76. -- Detect all SAM modules
  77. local function detect_modules()
  78. local lfs = require("lfs")
  79. local moddir = os.getenv("SAM_MODULES") or "/usr/share/sam"
  80. for file in lfs.dir( moddir ) do
  81. local name
  82. local path
  83. _,_,name = string.find(file, "^sam_([%a][_%w%a]*).lua")
  84. path = moddir .. "/" .. file
  85. if name and lfs.attributes(path).mode == "file" and "sam_" .. name .. ".lua" == file then
  86. sam.dbg(_NAME, "Found '%s' (%s)\n", name, path)
  87. -- preset the module structure of the detected module
  88. -- for auto-loading
  89. sam.command[name] = {
  90. _module = {
  91. _NAME = name,
  92. _FILE = path,
  93. },
  94. _load = function(self,...) load_module(self._module._NAME) end,
  95. _NAME = name,
  96. _DESCRIPTION = "",
  97. _USAGE = "",
  98. main = function(self,...)
  99. load_module(self._module._NAME)
  100. return self:main(unpack(arg))
  101. end,
  102. }
  103. -- add a metatable so the commands can be used, however,
  104. -- it is anly a intermediate metatable, as the module is not
  105. -- loaded yet. The module gets loaded (dynamic linker alike)
  106. -- once it is called
  107. setmetatable(sam.command[name], {
  108. __call = function(self, ...)
  109. load_module(self._module._NAME)
  110. return self:main(unpack(arg))
  111. end,
  112. })
  113. end
  114. end
  115. end
  116. -- COMMANDS -----------------------------------------------------------------
  117. local function usage(cmd)
  118. fprintf(io.stdout, "%s v%s %s\n\n", _NAME, _VERSION, _COPYRIGHT)
  119. if cmd then
  120. if sam.command[cmd]._load then sam.command[cmd]:_load() end
  121. fprintf(io.stdout, "Usage: sam %s\n", sam.command[cmd]._USAGE)
  122. else
  123. fprintf(io.stdout, "Usage: sam <command> [command options]\n\n%s\n",
  124. [[Commands:
  125. help Show command overview (this)
  126. help <command> Show command specific usage information]])
  127. for k,_ in pairs(sam.command) do
  128. if sam.command[k]._load then sam.command[k]:_load() end
  129. fprintf(io.stdout, " %16s %s\n", k, sam.command[k]._DESCRIPTION)
  130. end
  131. end
  132. end
  133. -- --------------------------------------------------------------------------
  134. -- INITIALIZE SAM
  135. -- --------------------------------------------------------------------------
  136. detect_modules()
  137. -- --------------------------------------------------------------------------
  138. -- MAIN
  139. -- --------------------------------------------------------------------------
  140. if arg[1] then
  141. -- help
  142. if arg[1] == "help" then
  143. usage(arg[2])
  144. elseif arg[2] == "help" then
  145. usage(arg[1])
  146. else
  147. -- split command and command arguments
  148. local cmd = arg[1]
  149. local args = arg ; table.remove(args, 1)
  150. sam.command[cmd](unpack(args or {}))
  151. end
  152. end