talimat.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. #!/usr/bin/python2
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (c) 2017 Mahmut Şamil Avar - milisarge
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a copy
  7. # of this software and associated documentation files (the "Software"), to
  8. # deal in the Software without restriction, including without limitation the
  9. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. # sell copies of the Software, and to permit persons to whom the Software is
  11. # furnished to do so, subject to the following conditions:
  12. #
  13. # The above copyright notice and this permission notice shall be included in
  14. # all copies or substantial portions of the Software.
  15. #
  16. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. # IN THE SOFTWARE.
  23. """
  24. Milis Linux Python Talimat Kütüphanesi - talimat.py
  25. """
  26. import sys
  27. import re
  28. import os
  29. import shlex
  30. import urllib2
  31. from requests.exceptions import HTTPError
  32. if os.path.exists("/usr/lib/python2.7/site-packages/requests") is False:
  33. print "request kurulacak"
  34. os.system("pip2 install requests")
  35. # Milis linux talimat sınıfı
  36. class Talimat():
  37. talimatname="/sources/milis.git/talimatname/"
  38. def __init__(self):
  39. self.tanim=""
  40. self.url=""
  41. self.paketci=""
  42. self.gerekler=[]
  43. self.gruplar=[]
  44. self.isim=""
  45. self._isim=""
  46. self.surum=""
  47. self.devir=""
  48. self.kaynaklar=[]
  49. self.derleme="derle() {"+"\n"
  50. def ice_aktar(self,dosya,tip):
  51. if tip=="arch":
  52. d_isim=dosya.split("_")[0]
  53. pkgbuild=PKGBUILD(dosya)
  54. if hasattr(pkgbuild, "description"):
  55. self.tanim=pkgbuild.description
  56. else:
  57. self.tanim=""
  58. if hasattr(pkgbuild, "url"):
  59. self.url=pkgbuild.url
  60. else:
  61. self.url=""
  62. self.paketci="milisarge"
  63. if hasattr(pkgbuild, 'makedepends'):
  64. for mgerek in pkgbuild.makedepends:
  65. if mgerek not in self.gerekler:
  66. self.gerekler.append(mgerek)
  67. if hasattr(pkgbuild, 'depends'):
  68. for gerek in pkgbuild.depends:
  69. if gerek not in self.gerekler:
  70. self.gerekler.append(gerek)
  71. if hasattr(pkgbuild, 'groups'):
  72. for grup in pkgbuild.groups:
  73. if grup not in self.gruplar:
  74. self.gruplar.append(grup)
  75. if isinstance(pkgbuild.name, list):
  76. self.isim=d_isim
  77. else:
  78. self.isim=pkgbuild.name
  79. if hasattr(pkgbuild, '_name'):
  80. self._isim=pkgbuild._name
  81. self.surum=pkgbuild.version
  82. self.devir=int(pkgbuild.release)
  83. self.kaynaklar=pkgbuild.sources
  84. self._ice_aktar_bloklar(dosya,tip)
  85. return "tanımlar için gecersiz tip!"
  86. def _gerekler(self):
  87. gerekstr=""
  88. for gerek in self.gerekler:
  89. if os.path.exists(self.talimatname+"onsistem/"+gerek) is False and os.path.exists(self.talimatname+"temel/"+gerek) is False:
  90. gerekstr+=gerek+" "
  91. if os.path.exists(self.talimatname+"genel/"+gerek[0:1]+"/"+gerek) is False:
  92. print renk.uyari+gerek+" talimatı yapılmalı!"+renk.son
  93. return gerekstr
  94. def _gruplar(self):
  95. grupstr=""
  96. for grup in self.gruplar:
  97. grupstr+=grup+" "
  98. return grupstr
  99. def _kaynaklar(self):
  100. kaynakstr=""
  101. for kaynak in self.kaynaklar:
  102. kaynakstr+=kaynak+"\n"
  103. return kaynakstr
  104. def _ice_aktar_bloklar(self,dosya,tip):
  105. if tip=="arch":
  106. #bu sekilde de satirlar cekilebilir.
  107. #lines = [line.rstrip('\n') for line in open(dosya)]
  108. with open(dosya) as f:
  109. satirlar = f.readlines()
  110. blok=False
  111. onblok=False
  112. for satir in satirlar:
  113. if "md5sums=(" in satir or "sha256sums=('" in satir or "sha1sums=('" in satir or "sha512sums=('" in satir:
  114. onblok=True
  115. if onblok is True and "')" in satir:
  116. blok=True
  117. continue
  118. if blok and satir.rstrip()!="" and satir.rstrip()!="}":
  119. if (satir not in self.derleme) and ("pkgver()" not in satir) and ("prepare()" not in satir) and ("build()" not in satir) and ("package()" not in satir) and ("check()" not in satir):
  120. satir=satir.replace("pkgdir","PKG")
  121. satir=satir.replace("srcdir","SRC")
  122. satir=satir.replace("pkgname","isim")
  123. satir=satir.replace("pkgver","surum")
  124. satir=satir.replace("pkgrel","devir")
  125. self.derleme+=satir+"\n"
  126. else:
  127. return "blok için gecersiz tip!"
  128. def olustur(self):
  129. if self.isim:
  130. print renk.tamamb+self.isim+" talimatı hazırlanıyor..."+renk.son
  131. os.system("mkdir -p "+self.isim)
  132. open(self.isim+"/talimat","w").write(self.icerik())
  133. def icerik(self):
  134. icerikstr=""
  135. icerikstr+="# Tanım: "+self.tanim+"\n"
  136. icerikstr+="# URL: "+self.url+"\n"
  137. icerikstr+="# Paketçi: "+self.paketci+"\n"
  138. icerikstr+="# Gerekler: "+self._gerekler()+"\n"
  139. icerikstr+="# Grup: "+self._gruplar()
  140. icerikstr+="\n"+"\n"
  141. icerikstr+="isim="+self.isim+"\n"
  142. if self._isim !="":
  143. icerikstr+="_isim="+self._isim+"\n"
  144. icerikstr+="surum="+str(self.surum)+"\n"
  145. icerikstr+="devir="+str(self.devir)+"\n"
  146. icerikstr+="kaynak=("+self._kaynaklar()+")"
  147. icerikstr+="\n"+"\n"
  148. # boş satırların temizlenmesi
  149. d_icerik = "".join([s for s in self.derleme.splitlines(True) if s.strip("\r\n")])
  150. icerikstr+=d_icerik
  151. icerikstr+="}"
  152. return icerikstr
  153. def cevir(self,dosya,tip="arch"):
  154. self.ice_aktar(dosya,tip)
  155. self.olustur()
  156. print renk.tamamy+talimat.isim+" talimatı hazır."+renk.son
  157. # archlinux pkgbuild sınıfı
  158. #Copyright (c) 2009 Sebastian Nowicki (parched.py)
  159. class PKGBUILD():
  160. _symbol_regex = re.compile(r"\$(?P<name>{[\w\d_]+}|[\w\d]+)")
  161. def __init__(self, name=None, fileobj=None):
  162. self.install = ""
  163. self.checksums = {
  164. 'md5': [],
  165. 'sha1': [],
  166. 'sha256': [],
  167. 'sha384': [],
  168. 'sha512': [],
  169. }
  170. self.noextract = []
  171. self.sources = []
  172. self.makedepends = []
  173. # Symbol lookup table
  174. self._var_map = {
  175. 'pkgname': 'name',
  176. '_pkgname': '_name',
  177. 'pkgver': 'version',
  178. 'pkgdesc': 'description',
  179. 'pkgrel': 'release',
  180. 'source': 'sources',
  181. 'arch': 'architectures',
  182. 'license': 'licenses',
  183. }
  184. self._checksum_fields = (
  185. 'md5sums',
  186. 'sha1sums',
  187. 'sha256sums',
  188. 'sha384sums',
  189. 'sha512sums',
  190. )
  191. # Symbol table
  192. self._symbols = {}
  193. if not name and not fileobj:
  194. raise ValueError("nothing to open")
  195. should_close = False
  196. if not fileobj:
  197. fileobj = open(name, "r")
  198. should_close = True
  199. self._parse(fileobj)
  200. if should_close:
  201. fileobj.close()
  202. def _handle_assign(self, token):
  203. var, equals, value = token.strip().partition('=')
  204. # Is it an array?
  205. if value!="":
  206. if value[0] == '(' and value[-1] == ')':
  207. self._symbols[var] = self._clean_array(value)
  208. else:
  209. self._symbols[var] = self._clean(value)
  210. else:
  211. self._symbols[var] = self._clean(value)
  212. def _parse(self, fileobj):
  213. """Parse PKGBUILD"""
  214. if hasattr(fileobj, "seek"):
  215. fileobj.seek(0)
  216. parser = shlex.shlex(fileobj, posix=True)
  217. parser.whitespace_split = True
  218. in_function = False
  219. while 1:
  220. token = parser.get_token()
  221. if token is None or token == '':
  222. break
  223. # Skip escaped newlines and functions
  224. if token == '\n' or in_function:
  225. continue
  226. # Special case:
  227. # Array elements are dispersed among tokens, we have to join
  228. # them first
  229. if token.find("=(") >= 0 and not token.rfind(")") >= 0:
  230. in_array = True
  231. elements = []
  232. while in_array:
  233. _token = parser.get_token()
  234. if _token == '\n':
  235. continue
  236. if _token[-1] == ')':
  237. _token = '"%s")' % _token.strip(')')
  238. token = token.replace('=(', '=("', 1) + '"'
  239. token = " ".join((token, " ".join(elements), _token))
  240. in_array = False
  241. else:
  242. elements.append('"%s"' % _token.strip())
  243. # Assignment
  244. if re.match(r"^[\w\d_]+=", token):
  245. self._handle_assign(token)
  246. # Function definitions
  247. elif token == '{':
  248. in_function = True
  249. elif token == '}' and in_function:
  250. in_function = False
  251. self._substitute()
  252. self._assign_local()
  253. if self.release:
  254. self.release = float(self.release)
  255. def _clean(self, value):
  256. """Pythonize a bash string"""
  257. return " ".join(shlex.split(value))
  258. def _clean_array(self, value):
  259. """Pythonize a bash array"""
  260. return shlex.split(value.strip('()'))
  261. def _replace_symbol(self, matchobj):
  262. """Replace a regex-matched variable with its value"""
  263. symbol = matchobj.group('name').strip("{}")
  264. # If the symbol isn't found fallback to an empty string, like bash
  265. try:
  266. value = self._symbols[symbol]
  267. except KeyError:
  268. value = ''
  269. # BUG: Might result in an infinite loop, oops!
  270. return self._symbol_regex.sub(self._replace_symbol, value)
  271. def _substitute(self):
  272. """Substitute all bash variables within values with their values"""
  273. for symbol in self._symbols:
  274. value = self._symbols[symbol]
  275. # FIXME: This is icky
  276. if isinstance(value, str):
  277. result = self._symbol_regex.sub(self._replace_symbol, value)
  278. else:
  279. result = [self._symbol_regex.sub(self._replace_symbol, x)
  280. for x in value]
  281. self._symbols[symbol] = result
  282. def _assign_local(self):
  283. """Assign values from _symbols to PKGBUILD variables"""
  284. for var in self._symbols:
  285. value = self._symbols[var]
  286. if var in self._checksum_fields:
  287. key = var.replace('sums', '')
  288. self.checksums[key] = value
  289. else:
  290. if var in self._var_map:
  291. var = self._var_map[var]
  292. setattr(self, var, value)
  293. class renk:
  294. baslik = '\033[95m'
  295. tamamb = '\033[94m'
  296. tamamy = '\033[92m'
  297. uyari = '\033[93m'
  298. hata = '\033[91m'
  299. son = '\033[0m'
  300. kalin = '\033[1m'
  301. altcizgili = '\033[4m'
  302. class Arge:
  303. # arch pkgbuild linki indirme
  304. def indir(self,link):
  305. paket=link.split("/")[-1]
  306. if paket == "":
  307. paket=link.split("/")[-2]
  308. if "=" in paket:
  309. paket=paket.split("=")[1]
  310. print renk.tamamb+paket+" indiriliyor..."+renk.son
  311. try:
  312. veri = urllib2.urlopen(link)
  313. open(paket+"_pkgbuild","w").write(veri.read())
  314. return paket+"_pkgbuild"
  315. except urllib2.HTTPError, e:
  316. if e.code == 404:
  317. print renk.hata+link+" bulunamadı!"+renk.son
  318. return None
  319. def aur_link(self,paket):
  320. link="https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h="+paket
  321. return link
  322. def arch2_link(self,paket):
  323. link="https://git.archlinux.org/svntogit/community.git/plain/trunk/PKGBUILD?h=packages/"+paket
  324. return link
  325. def arch_link(self,paket):
  326. link="https://git.archlinux.org/svntogit/packages.git/plain/trunk/PKGBUILD?h=packages/"+paket
  327. return link
  328. # alpine apkbuild linki indirme
  329. def indir_alp(self,paket,tip="isim"):
  330. if tip=="isim":
  331. repolar=["main","community","testing"]
  332. for repo in repolar:
  333. link="https://git.alpinelinux.org/cgit/aports/plain/"+repo+"/"+paket+"/APKBUILD"
  334. try:
  335. veri = urllib2.urlopen(link)
  336. print renk.tamamb+repo+" reposunda bulundu"+renk.son
  337. open(paket+"_apkbuild","w").write(veri.read())
  338. return paket+"_apkbuild"
  339. except urllib2.HTTPError, e:
  340. if e.code == 404:
  341. print renk.hata+link+" bulunamadı!"+renk.son
  342. elif tip=="link":
  343. paket=link.split("/")[-2]
  344. print renk.tamamb+paket+" indiriliyor..."+renk.son
  345. try:
  346. veri = urllib2.urlopen(link)
  347. open(paket+"_apkbuild","w").write(veri.read())
  348. return paket+"_apkbuild"
  349. except urllib2.HTTPError, e:
  350. if e.code == 404:
  351. print renk.hata+link+" bulunamadı!"+renk.son
  352. return None
  353. return None
  354. # kaosx github linki indirme
  355. def indir_kaos(self,paket,tip="isim"):
  356. if tip=="isim":
  357. repolar=["main","apps","core"]
  358. for repo in repolar:
  359. link="https://raw.githubusercontent.com/KaOSx/"+repo+"/master/"+paket+"/PKGBUILD"
  360. try:
  361. veri = urllib2.urlopen(link)
  362. print renk.tamamb+repo+" reposunda bulundu"+renk.son
  363. open(paket+"_pkgbuild","w").write(veri.read())
  364. return paket+"_pkgbuild"
  365. except urllib2.HTTPError, e:
  366. if e.code == 404:
  367. print renk.hata+link+" bulunamadı!"+renk.son
  368. elif tip=="link":
  369. paket=link.split("/")[-2]
  370. print renk.tamamb+paket+" indiriliyor..."+renk.son
  371. try:
  372. veri = urllib2.urlopen(link)
  373. open(paket+"_pkgbuild","w").write(veri.read())
  374. return paket+"_pkgbuild"
  375. except urllib2.HTTPError, e:
  376. if e.code == 404:
  377. print renk.hata+link+" bulunamadı!"+renk.son
  378. return None
  379. return None
  380. # blackarch github linki indirme
  381. def indir_blackarch(self,paket,tip="isim"):
  382. if tip=="isim":
  383. repolar=["packages"]
  384. for repo in repolar:
  385. link="https://raw.githubusercontent.com/BlackArch/blackarch/master/"+repo+"/"+paket+"/PKGBUILD"
  386. try:
  387. veri = urllib2.urlopen(link)
  388. print renk.tamamb+repo+" reposunda bulundu"+renk.son
  389. open(paket+"_pkgbuild","w").write(veri.read())
  390. return paket+"_pkgbuild"
  391. except urllib2.HTTPError, e:
  392. if e.code == 404:
  393. print renk.hata+link+" bulunamadı!"+renk.son
  394. elif tip=="link":
  395. paket=link.split("/")[-2]
  396. print renk.tamamb+paket+" indiriliyor..."+renk.son
  397. try:
  398. veri = urllib2.urlopen(link)
  399. open(paket+"_pkgbuild","w").write(veri.read())
  400. return paket+"_pkgbuild"
  401. except urllib2.HTTPError, e:
  402. if e.code == 404:
  403. print renk.hata+link+" bulunamadı!"+renk.son
  404. return None
  405. return None
  406. def yardim(self):
  407. print renk.tamamb+"talimat.py -a (archlinux) paket_ismi | url "+renk.son
  408. print renk.tamamb+"talimat.py -k (kaosx) paket_ismi | url "+renk.son
  409. print renk.tamamb+"talimat.py -b (blackarch) paket_ismi | url "+renk.son
  410. if __name__ == '__main__':
  411. arge=Arge()
  412. if len(sys.argv) > 1:
  413. dosya=sys.argv[1]
  414. talimat=Talimat()
  415. if os.path.exists(dosya):
  416. talimat.cevir(dosya)
  417. elif len(sys.argv) > 2:
  418. if dosya == "-a":
  419. dosya=sys.argv[2]
  420. if "https" in dosya or "http" in dosya:
  421. Pdosya=arge.indir(dosya)
  422. talimat.cevir(Pdosya)
  423. else :
  424. paket=sys.argv[2]
  425. paket=str(paket)
  426. link=arge.aur_link(paket)
  427. dosya=arge.indir(link)
  428. if dosya is None:
  429. link=arge.arch_link(paket)
  430. dosya=arge.indir(link)
  431. if dosya is None:
  432. link=arge.arch2_link(paket)
  433. dosya=arge.indir(link)
  434. if link and dosya:
  435. talimat.cevir(dosya)
  436. elif dosya == "-k":
  437. link=sys.argv[2]
  438. if "https" in link or "http" in link:
  439. Pdosya=arge.indir_kaos(link,"link")
  440. talimat.cevir(Pdosya)
  441. else :
  442. paket=sys.argv[2]
  443. paket=str(paket)
  444. dosya=arge.indir_kaos(paket,"isim")
  445. if dosya:
  446. talimat.cevir(dosya)
  447. else:
  448. renk.hata+str(dosya)+" repolarda bulunamadı!"+renk.son
  449. elif dosya == "-b":
  450. link=sys.argv[2]
  451. if "https" in link or "http" in link:
  452. Pdosya=arge.indir_blackarch(link,"link")
  453. talimat.cevir(Pdosya)
  454. else :
  455. paket=sys.argv[2]
  456. paket=str(paket)
  457. dosya=arge.indir_blackarch(paket,"isim")
  458. if dosya:
  459. talimat.cevir(dosya)
  460. else:
  461. renk.hata+str(dosya)+" repolarda bulunamadı!"+renk.son
  462. elif dosya == "-al":
  463. link=sys.argv[2]
  464. if "https" in link or "http" in link:
  465. Pdosya=arge.indir_alp(link,"link")
  466. print Pdosya
  467. #talimat.cevir(Pdosya)
  468. else :
  469. paket=sys.argv[2]
  470. paket=str(paket)
  471. dosya=arge.indir_alp(paket,"isim")
  472. if dosya:
  473. print dosya
  474. #talimat.cevir(dosya)
  475. else:
  476. renk.hata+str(dosya)+" repolarda bulunamadı!"+renk.son
  477. else:
  478. print renk.hata+dosya+" paremetre bulunamadı!"+renk.son
  479. else:
  480. arge.yardim()