mps_helper.lua 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/env lua
  2. -- mps için yardımcı işlevler
  3. --importlar
  4. local color = require ("ansicolors")
  5. local helper={}
  6. helper.renkli=true
  7. helper.shell_log="/tmp/mps_helper-shell.log"
  8. -- yaz = farklı tipte çıktıları ekrana renkli yazan işlev
  9. -- codes: 0=error, 1=success, 2=info 3=warning
  10. getmetatable("").__index.yaz = function(msg,code)
  11. --print("*",msg,code,color)
  12. tip={'%{red}','%{green}','%{blue}','%{lred}'}
  13. if code == nil then code=2 end
  14. if helper.renkli then
  15. --print(tip[code+1](msg))
  16. print(color(('%s%s'):format(tip[code+1],msg)))
  17. else
  18. print(msg)
  19. end
  20. if code == 0 then
  21. os.exit()
  22. end
  23. end
  24. --split string
  25. function string:split(delimiter)
  26. local result = {}
  27. if delimiter == "." then
  28. for i in string.gmatch(self, "[^%.]+") do
  29. table.insert(result,i)
  30. end
  31. else
  32. local from = 1
  33. local delim_from, delim_to = string.find( self, delimiter, from )
  34. while delim_from do
  35. table.insert( result, string.sub( self, from , delim_from-1 ) )
  36. from = delim_to + 1
  37. delim_from, delim_to = string.find( self, delimiter, from )
  38. end
  39. table.insert( result, string.sub( self, from ) )
  40. end
  41. return result
  42. end
  43. -- myassert, bir parametrenin nil, "", false olma durumlarını kontrol eder.
  44. function helper._assert(param,msg)
  45. if msg == nil then
  46. msg = "error:"
  47. end
  48. if param == nil or param == false then
  49. msg:yaz(0)
  50. else
  51. return param
  52. end
  53. end
  54. -- check a variable in an array
  55. -- todo!!! has_value kullanilan islevler find ile revize edilecek
  56. function helper.has_value (tab, val)
  57. for _, value in ipairs(tab) do
  58. if value == val then
  59. return true
  60. end
  61. end
  62. return false
  63. end
  64. -- get the index of an item in a table
  65. function helper.find(tab, val)
  66. for index, value in pairs(tab) do
  67. if value == val then
  68. return index
  69. end
  70. end
  71. return -1
  72. end
  73. -- print elements of a table-array
  74. function helper.tprint(tablo)
  75. for _, value in ipairs(tablo) do
  76. print (value)
  77. end
  78. end
  79. -- bir dizin/dosya yolunun varlığını kontrol eder
  80. function helper.path_exists(path)
  81. return lfs.attributes(path, "mode") ~= nil
  82. end
  83. -- bir dizin/dosya boyut
  84. function helper.get_size(path)
  85. return lfs.attributes(path, "size")
  86. end
  87. function helper.shell(command)
  88. -- Open log file in append mode
  89. local logger = io.open(helper.shell_log, "a")
  90. logger:write(command.."\n");
  91. logger:close();
  92. local handle=io.popen(command)
  93. local result=handle:read('*all')
  94. handle:close()
  95. -- komut çıktısı sonu yeni satır karakterin silinmesi - en sondaki \n
  96. if result:sub(-1) == "\n" then
  97. --result=result:gsub("\n", "")
  98. result=result:sub(1,-2)
  99. end
  100. return result
  101. end
  102. function helper.get_abspath(path)
  103. local ret=helper.shell("readlink -f " .. path)
  104. return ret
  105. end
  106. function helper.get_basename(path)
  107. return helper.shell("basename " .. path)
  108. end
  109. function helper.get_dirname(path)
  110. return helper.shell("dirname " .. path)
  111. end
  112. -- get content of a file
  113. function helper.get_content(filename)
  114. assert(helper.path_exists(filename),"helper.get_content : invalid path: "..filename)
  115. local f = assert(io.open(filename, "r"))
  116. local t = f:read('*all')
  117. f:close()
  118. return t
  119. end
  120. -- check a file has a line
  121. function helper.has_line(filename,line)
  122. assert(helper.path_exists(filename),"helper.has_line : invalid path: "..filename)
  123. local cnt=helper.get_content(filename)
  124. for linein in cnt:gmatch("[^\r\n]+") do
  125. if linein == line then
  126. return true
  127. end
  128. end
  129. return false
  130. end
  131. -- check sha256sum of a file
  132. function helper.hash_check(filepath,hash_value)
  133. assert(helper.path_exists(filepath),"helper invalid path: "..filepath)
  134. local komut='echo "%s %s" | sha256sum --check ;[ $? -eq 1 ] && printf "err@r"'
  135. local ret=helper.shell(komut:format(hash_value,filepath))
  136. if ret:match("err@r") then
  137. return false
  138. end
  139. return true
  140. end
  141. -- get dirs of a directory
  142. function helper.get_dirs(directory)
  143. dirs={}
  144. for obj in lfs.dir(directory) do
  145. if lfs.attributes(directory..obj,"mode") == "directory" then
  146. if obj ~="." and obj~=".." then
  147. table.insert(dirs,obj)
  148. end
  149. end
  150. end
  151. return dirs
  152. end
  153. -- convert byte to kilobyte,megabyte
  154. function helper.byte_convert(value)
  155. --test
  156. if value == "@test" then
  157. assert(helper.byte_convert(1023) == "1023 B")
  158. assert(helper.byte_convert(1025) == "1.0 KB")
  159. assert(helper.byte_convert(1024*1025) == "1.00 MB")
  160. assert(helper.byte_convert(1024*1025*1024) == "1.001 GB")
  161. return 1
  162. end
  163. local _kb=1024
  164. local _mb=1024*_kb
  165. local _gb=1024*_mb
  166. local result=""
  167. if type(value) == "string" then value=tonumber(value) end
  168. if value > _gb then result=("%.3f GB"):format(value/_gb)
  169. elseif value > _mb then result=("%.2f MB"):format(value/_mb)
  170. elseif value > _kb then result=("%.1f KB"):format(value/_kb)
  171. else result=("%.0f B"):format(value) end
  172. return result
  173. end
  174. return helper