mpsd.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #!/usr/bin/lua
  2. local mps_path=os.getenv("MPS_PATH")
  3. if not mps_path then mps_path="/usr/aylinux/mps" end
  4. --package.cpath = package.cpath .. ";"..mps_path.."/lua/?.so"
  5. -- genel lua kütüphanelerinden etkilenmemesi için önce mps yolunda olanlar kullanılacak.
  6. package.cpath = mps_path.."/lua/?.so" .. ";".. package.cpath
  7. package.cpath = mps_path.."/lua/ext/?.so" .. ";".. package.cpath
  8. package.path = mps_path.."/lua/?.lua" .. ";".. package.path
  9. package.path = mps_path.."/lua/ext/?.lua".. ";".. package.path
  10. package.path = mps_path.."/lang/?.lua" .. ";".. package.path
  11. package.path = mps_path.."/conf/?.lua" .. ";".. package.path
  12. local talimatci= require("talimat")
  13. local rules= require("rules")
  14. -- Variables
  15. local talimat_file="talimat"
  16. local noexec=false
  17. local talimat_dir=""
  18. local isd=""
  19. local talimat_path=""
  20. local thash=""
  21. local start_task_no=1
  22. local _signs={[0]="+",[1]="-",[2]="!",[8]="Server issued an error response.(wget)"}
  23. local task_queue={
  24. "talimat_meta",
  25. "dirs_create" ,
  26. "fetch_sources" ,
  27. "check_sources" ,
  28. "extract_sources" ,
  29. "build_package",
  30. "install_package",
  31. "strip_files",
  32. "compress_manpages",
  33. "delete_files",
  34. "copy_scripts",
  35. "generate_meta",
  36. "generate_content",
  37. "generate_package",
  38. "dirs_delete"
  39. }
  40. -- Helper functions
  41. function _error(msg)
  42. print(msg)
  43. os.exit()
  44. end
  45. function shell(command)
  46. local handle=io.popen(command)
  47. local result=handle:read('*all')
  48. handle:close()
  49. -- komut çıktısı sonu yeni satır karakterin silinmesi - en sondaki \n
  50. if result:sub(-1) == "\n" then
  51. --result=result:gsub("\n", "")
  52. result=result:sub(1,-2)
  53. end
  54. return result
  55. end
  56. --- Check if a file or directory exists in this path
  57. function exists(file)
  58. local ok, err, code = os.rename(file, file)
  59. if not ok then
  60. if code == 13 then
  61. -- Permission denied, but it exists
  62. return true
  63. end
  64. end
  65. return ok, err
  66. end
  67. function string:addline(line)
  68. return self..line.."\n"
  69. end
  70. function string:split(delimiter)
  71. local result = {}
  72. if delimiter == "." then
  73. for i in string.gmatch(self, "[^%.]+") do
  74. table.insert(result,i)
  75. end
  76. else
  77. local from = 1
  78. local delim_from, delim_to = string.find( self, delimiter, from )
  79. while delim_from do
  80. table.insert( result, string.sub( self, from , delim_from-1 ) )
  81. from = delim_to + 1
  82. delim_from, delim_to = string.find( self, delimiter, from )
  83. end
  84. table.insert( result, string.sub( self, from ) )
  85. end
  86. return result
  87. end
  88. -- check a variable in an array
  89. function table.has(tab,val)
  90. for index, value in ipairs(tab) do
  91. if value == val then return true end
  92. end
  93. return false
  94. end
  95. function create_talimat(tdir)
  96. local template=[===[
  97. [paket]
  98. tanim = %s paketi
  99. paketci = Cihan_Alkan
  100. grup = kütüphane
  101. url = https://aylinux.kripto.com.tr
  102. [gerek]
  103. derleme =
  104. calisma =
  105. [kaynak]
  106. 1 = kaynak_adresi
  107. [sha256]
  108. 1 =
  109. [derle]
  110. tip = gnu
  111. [pakur]
  112. tip = gnu
  113. ]===]
  114. local isim =tdir:split("#")[1]
  115. local surum =tdir:split("#")[2]:split("-")[1]
  116. local devir =tdir:split("#")[2]:split("-")[2]
  117. if isim and surum and devir then
  118. shell("mkdir -pv "..tdir)
  119. end
  120. local file = io.open(tdir.."/talimat", "w")
  121. io.output(file)
  122. io.write(template:format(isim))
  123. io.close(file)
  124. print(tdir.." için şablon talimat oluşturuldu")
  125. end
  126. -------------------------------------------------- ---
  127. -- talimat metalarını export olarak çıktı almak için
  128. function talimat_meta()
  129. local t=""
  130. t=t:addline("##### talimat vars #####")
  131. for _,cmd in ipairs(rules.export.talimat(talimat.paket)) do
  132. t=t:addline(cmd)
  133. end
  134. return t
  135. end
  136. function dirs_create()
  137. local t=""
  138. t=t:addline("##### create dirs #####")
  139. t=t:addline(rules.make_dirs.archive())
  140. t=t:addline(rules.make_dirs.pkg())
  141. t=t:addline(rules.make_dirs.src())
  142. t=t:addline(rules.make_dirs.pkg_meta())
  143. return t
  144. end
  145. function prepare_sources()
  146. -- create srcobj, address store_name
  147. local t={}
  148. local key, address, store, place
  149. for _,val in ipairs(talimat.kaynak) do
  150. key=val:split("@@")[1]
  151. val=val:split("@@")[2]
  152. if rules.source[key] ~= nil then
  153. --apply rule
  154. val=rules.source[key](talimat,val)
  155. end
  156. --parse applied source string
  157. address=val:split("::")[1]
  158. store=val:split("::")[2]
  159. if store == nil then
  160. store=val:split("/")[#val:split("/")]
  161. end
  162. place="url"
  163. extract=true
  164. if key == "git" then place="git" end
  165. if key == "svn" then place="svn" end
  166. if key == "dosya" or key == "file" then place="file" end
  167. if key == "dizin" or key == "dir" then place="dir" end
  168. if place == "url" or place == "git" or place == "svn"
  169. then store=rules.dirs.archive..store end
  170. if place == "file" then
  171. address=talimat_dir.."/"..address
  172. store=rules.dirs.src.."/"..store
  173. end
  174. if place == "dir" then
  175. store=rules.dirs.src.."/"..store
  176. end
  177. -- check if it has no extract option
  178. if store:sub(-1) == "!" then
  179. extract=false
  180. store=store:split("!")[1]
  181. address=address:split("!")[1]
  182. end
  183. -- add sources table
  184. table.insert(t,{fetch=place,address=address,store=store,extract=extract})
  185. end
  186. return t
  187. -- print(srcobj.fetch,srcobj.address,srcobj.store)
  188. end
  189. function fetch_sources()
  190. local t=""
  191. local sources=prepare_sources()
  192. t=t:addline("##### fetch sources #####")
  193. t=t:addline(talimat_meta())
  194. t=t:addline(rules.export.source_aliases)
  195. if sources == {} then
  196. _error("source list is not ready")
  197. end
  198. local afetch
  199. for _,srcobj in ipairs(sources) do
  200. --if srcobj.fetch == "url" then srcobj.store=rules.dirs.archive..srcobj.store end
  201. --if srcobj.fetch == "file" then
  202. -- srcobj.address=talimat_dir.."/"..srcobj.address
  203. -- srcobj.store=rules.dirs.src.."/"..srcobj.store
  204. --end
  205. if rules.fetch[srcobj.fetch] ~= nil then
  206. --t=t:addline(rules.fetch.check(srcobj))
  207. --apply rule
  208. afetch=rules.fetch[srcobj.fetch](srcobj)
  209. else
  210. _error("unknown fetch way:"..srcobj.fetch)
  211. end
  212. t=t:addline(afetch)
  213. end
  214. return t
  215. end
  216. function check_sources()
  217. local t=""
  218. -- apply talimat variables
  219. t=t:addline(talimat_meta())
  220. local sources=prepare_sources()
  221. t=t:addline("##### check sources #####")
  222. if sources == {} then
  223. _error("source list is not ready")
  224. end
  225. local achk
  226. local hashes={"sha256","sha512"}
  227. for _,hasht in ipairs(hashes) do
  228. if talimat[hasht] ~= nil then
  229. for index,_hash in ipairs(talimat[hasht]) do
  230. if sources[index] ~= nil then
  231. --if sources[index]["fetch"] == "file" then sources[index]["store"]=rules.dirs.src.."/"..sources[index]["store"] end
  232. --if sources[index]["fetch"] == "url" then sources[index]["store"]=rules.dirs.archive..sources[index]["store"] end
  233. achk=rules.hash[hasht](sources[index]["store"],_hash)
  234. t=t:addline(achk)
  235. end
  236. end
  237. end
  238. end
  239. return t
  240. end
  241. function extract_sources()
  242. local t=""
  243. -- apply talimat variables
  244. t=t:addline(talimat_meta())
  245. local sources=prepare_sources()
  246. t=t:addline("##### extract sources #####")
  247. if sources == {} then
  248. _error("source list is not ready")
  249. end
  250. local aext, suffix
  251. local archive_suffix={"gz","xz","lz","bz2","tgz","zip","tar"}
  252. -- archive file type only todo!!!
  253. for _,srcobj in ipairs(sources) do
  254. -- detect file type from suffix (todo!!! magic, mime type)
  255. if srcobj.extract then
  256. suffix=srcobj.store:split(".")[#srcobj.store:split(".")]
  257. -- extract dir, store name
  258. if table.has(archive_suffix,suffix) then
  259. aext=rules.extract.bsdtar(rules.dirs.src,srcobj.store)
  260. t=t:addline(aext)
  261. end
  262. else
  263. t=t:addline('echo "'..srcobj.store..' : no extract"')
  264. end
  265. end
  266. return t
  267. end
  268. function build_package()
  269. local t=""
  270. t=t:addline("##### build sources #####")
  271. -- apply global env
  272. -- need(export_dir-make_dir)
  273. -- need(talimat_export)
  274. t=t:addline("set -x")
  275. for _,cmd in ipairs(rules.export.dirs()) do
  276. t=t:addline(cmd)
  277. end
  278. -- apply talimat variables
  279. t=t:addline(talimat_meta())
  280. -- apply build environments
  281. for key,_ in pairs(rules.build_env) do
  282. --print("----",export)
  283. t=t:addline(rules.build_env[key]())
  284. end
  285. -- default enter source directory
  286. t=t:addline(rules.change.dir(rules.dirs.src))
  287. if talimat.paket.arsiv then
  288. t=t:addline(rules.export.me("ARCHIVE_DIR",rules.dirs.src..talimat.paket.arsiv))
  289. t=t:addline(rules.change.dir(rules.dirs.src..talimat.paket.arsiv))
  290. else
  291. t=t:addline(rules.change.dir(rules.package.archive))
  292. end
  293. -- create srcobj, address store_name
  294. local key
  295. -- traverse orderly
  296. for _,val in ipairs(talimat.derle) do
  297. key=val:split("@@")[1]
  298. val=val:split("@@")[2]
  299. if rules.build[key] ~= nil then
  300. --apply rule
  301. val=rules.build[key](talimat,val)
  302. end
  303. if rules.build_env[key] ~= nil then
  304. --apply rule
  305. val=rules.build_env[key](val)
  306. end
  307. t=t:addline(val)
  308. end
  309. --t=t:addline("set +x")
  310. return t
  311. end
  312. function install_package()
  313. local t=""
  314. t=t:addline("##### install package #####")
  315. t=t:addline("set -x")
  316. -- apply global env
  317. for _,cmd in ipairs(rules.export.dirs()) do
  318. t=t:addline(cmd)
  319. end
  320. -- apply talimat variables
  321. t=t:addline(talimat_meta())
  322. -- apply build environments
  323. for key,_ in pairs(rules.build_env) do
  324. --print("----",export)
  325. t=t:addline(rules.build_env[key]())
  326. end
  327. -- default enter source directory
  328. t=t:addline(rules.change.dir(rules.dirs.src))
  329. if talimat.paket.arsiv then
  330. t=t:addline(rules.export.me("ARCHIVE_DIR",rules.dirs.src..talimat.paket.arsiv))
  331. t=t:addline(rules.change.dir(rules.dirs.src..talimat.paket.arsiv))
  332. else
  333. t=t:addline(rules.change.dir(rules.package.archive))
  334. end
  335. -- create srcobj, address store_name
  336. local key
  337. for _,val in ipairs(talimat.pakur) do
  338. key=val:split("@@")[1]
  339. val=val:split("@@")[2]
  340. if rules.install[key] ~= nil then
  341. --apply rule
  342. val=rules.install[key](talimat,val)
  343. end
  344. -- strip, nostrip return anything
  345. if val ~= nil then
  346. t=t:addline(val)
  347. end
  348. end
  349. --t=t:addline("set +x")
  350. return t
  351. end
  352. function strip_files()
  353. local t=""
  354. t=t:addline("##### strip files #####")
  355. if rules.strip.status then
  356. t=t:addline(rules.strip.files(rules.dirs.pkg,rules.strip.blacklist))
  357. end
  358. return t
  359. end
  360. function compress_manpages()
  361. local t=""
  362. t=t:addline("##### compress manpages #####")
  363. t=t:addline(rules.compress.man(rules.dirs.pkg))
  364. return t
  365. end
  366. function delete_files()
  367. local t=""
  368. t=t:addline("##### delete files #####")
  369. t=t:addline(rules.delete.files_un(rules.dirs.pkg))
  370. return t
  371. end
  372. function copy_scripts()
  373. local t=""
  374. t=t:addline("##### copy scripts #####")
  375. t=t:addline(rules.copy.scripts(talimat.dir,rules.dirs.pkg,rules.dirs.pkg_meta))
  376. return t
  377. end
  378. function generate_meta()
  379. local t=""
  380. t=t:addline("##### generate meta #####")
  381. t=t:addline(talimat_meta())
  382. t=t:addline(rules.find.libdepends(rules.dirs.pkg,rules.dirs.pkg_meta))
  383. t=t:addline(rules.find.pkglibs())
  384. local size=rules.calculate.size(rules.dirs.pkg)
  385. t=t:addline(rules.generate.meta_info(rules.dirs.pkg,rules.dirs.pkg_meta,size,thash))
  386. return t
  387. end
  388. function generate_content()
  389. local t=""
  390. t=t:addline("##### generate content #####")
  391. t=t:addline(rules.generate.content_info(rules.dirs.pkg))
  392. return t
  393. end
  394. function generate_package()
  395. local t=""
  396. t=t:addline("##### generate package #####")
  397. t=t:addline(talimat_meta())
  398. t=t:addline(rules.generate.package(rules.dirs.pkg,rules.dirs.pkg_meta))
  399. return t
  400. end
  401. function dirs_delete()
  402. local t=""
  403. t=t:addline("##### delete dirs #####")
  404. t=t:addline(rules.delete.dir(rules.dirs.pkg))
  405. t=t:addline(rules.delete.dir(rules.dirs.src))
  406. return t
  407. end
  408. function create_yur()
  409. local file = io.open(("/tmp/%s.yur"):format(isd), "w")
  410. io.output(file)
  411. local operf="Operation file generated by MPSD 2.0 [%s]\n"
  412. operf=operf:format(os.date())
  413. for _,task in ipairs(task_queue) do
  414. operf=operf.."\n".._G[task]()
  415. end
  416. io.write(operf)
  417. io.close(file)
  418. end
  419. function run()
  420. --create yururluk file
  421. create_yur()
  422. local totalsec=0
  423. for taskno,task in ipairs(task_queue) do
  424. local task_cmd=""
  425. task_cmd=_G[task]()
  426. if start_task_no <= taskno then
  427. if noexec then
  428. print(task_cmd)
  429. print("##### ----------- #####")
  430. else
  431. local stime=os.time()
  432. print("--------------")
  433. print(task:upper())
  434. print("---------------------------------------------------")
  435. print("start time",os.date())
  436. print()
  437. local a,b,c,exno=pcall(os.execute,task_cmd)
  438. --print(a,b,c,exno)
  439. print()
  440. print("finish time",os.date())
  441. totalsec=totalsec+(os.time()-stime)
  442. print("STATUS:",_signs[exno],exno,"sn: "..tostring(os.time()-stime))
  443. print("---------------------------------------------------")
  444. if exno ~= 0 then
  445. break
  446. end
  447. end
  448. print()
  449. end
  450. end
  451. print("Total min:",math.floor(totalsec/60*100)/100)
  452. end
  453. -- logging mpsd.lua talimatdir | tee /tmp/package.log
  454. -------------- main start ---------------
  455. talimat_dir=arg[1]
  456. if talimat_dir == nil then _error("talimat dizini belirtilmedi!") end
  457. if #arg > 1 then
  458. for i = 2,#arg do
  459. if arg[i] == "--print" or arg[i] == "-p" then
  460. noexec=true
  461. elseif arg[i] == "--create" or arg[i] == "-c" then
  462. create_talimat(talimat_dir)
  463. os.exit()
  464. elseif arg[i] == "--generate" or arg[i] == "-g" then
  465. -- resume since instal_package task
  466. start_task_no=7
  467. end
  468. end
  469. end
  470. if exists(talimat_dir) == nil then _error("geçersiz talimat dizini!") end
  471. talimat_dir=shell("readlink -f "..talimat_dir)
  472. isd=shell("basename "..talimat_dir)
  473. talimat_path=talimat_dir.."/"..talimat_file
  474. thash=shell("sha256sum "..talimat_path.." | awk '{print $1}'")
  475. if exists(talimat_path) == nil then _error("talimat dosyası bulunmadı!") end
  476. talimat=talimatci.load(talimat_path,{"derle","pakur","kaynak"})
  477. talimat.dir=talimat_dir
  478. talimat.paket.isim=isd:split("#")[1]
  479. talimat.paket.surum=isd:split("#")[2]:split("-")[1]
  480. talimat.paket.devir=isd:split("#")[2]:split("-")[2]
  481. -- start to process tasks
  482. run()