milis/bin/talimat.py

521 lines
16 KiB
Python
Raw Permalink Normal View History

2017-04-18 06:36:44 +02:00
#!/usr/bin/python2
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Mahmut Şamil Avar - milisarge
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
"""
Milis Linux Python Talimat Kütüphanesi - talimat.py
"""
import sys
import re
import os
import shlex
2017-04-18 14:37:54 +02:00
import urllib2
2017-04-18 15:00:29 +02:00
from requests.exceptions import HTTPError
2017-04-18 06:36:44 +02:00
2017-10-30 14:02:00 +01:00
if os.path.exists("/usr/lib/python2.7/site-packages/requests") is False:
print "request kurulacak"
os.system("pip2 install requests")
2017-04-18 06:36:44 +02:00
# Milis linux talimat sınıfı
class Talimat():
2017-04-18 07:05:09 +02:00
talimatname="/sources/milis.git/talimatname/"
2017-04-18 06:36:44 +02:00
def __init__(self):
self.tanim=""
self.url=""
self.paketci=""
self.gerekler=[]
2017-11-28 14:11:39 +01:00
self.gruplar=[]
2017-04-18 06:36:44 +02:00
self.isim=""
2017-04-18 14:37:54 +02:00
self._isim=""
2017-04-18 06:36:44 +02:00
self.surum=""
self.devir=""
self.kaynaklar=[]
2017-10-28 19:09:49 +02:00
self.derleme="derle() {"+"\n"
2017-04-18 06:36:44 +02:00
def ice_aktar(self,dosya,tip):
if tip=="arch":
2017-11-02 20:47:16 +01:00
d_isim=dosya.split("_")[0]
2017-04-18 06:36:44 +02:00
pkgbuild=PKGBUILD(dosya)
2017-11-02 20:47:16 +01:00
if hasattr(pkgbuild, "description"):
self.tanim=pkgbuild.description
else:
self.tanim=""
if hasattr(pkgbuild, "url"):
self.url=pkgbuild.url
else:
self.url=""
2017-04-18 06:36:44 +02:00
self.paketci="milisarge"
2017-04-18 14:37:54 +02:00
if hasattr(pkgbuild, 'makedepends'):
for mgerek in pkgbuild.makedepends:
if mgerek not in self.gerekler:
self.gerekler.append(mgerek)
if hasattr(pkgbuild, 'depends'):
for gerek in pkgbuild.depends:
if gerek not in self.gerekler:
self.gerekler.append(gerek)
2017-11-28 14:11:39 +01:00
if hasattr(pkgbuild, 'groups'):
for grup in pkgbuild.groups:
if grup not in self.gruplar:
self.gruplar.append(grup)
2017-11-02 20:47:16 +01:00
if isinstance(pkgbuild.name, list):
self.isim=d_isim
else:
self.isim=pkgbuild.name
2017-04-18 14:37:54 +02:00
if hasattr(pkgbuild, '_name'):
self._isim=pkgbuild._name
2017-04-18 06:36:44 +02:00
self.surum=pkgbuild.version
2017-10-28 23:04:20 +02:00
self.devir=int(pkgbuild.release)
2017-04-18 06:36:44 +02:00
self.kaynaklar=pkgbuild.sources
self._ice_aktar_bloklar(dosya,tip)
return "tanımlar için gecersiz tip!"
def _gerekler(self):
gerekstr=""
for gerek in self.gerekler:
2017-11-02 20:47:16 +01:00
if os.path.exists(self.talimatname+"onsistem/"+gerek) is False and os.path.exists(self.talimatname+"temel/"+gerek) is False:
2017-04-18 07:05:09 +02:00
gerekstr+=gerek+" "
2017-10-28 19:09:49 +02:00
if os.path.exists(self.talimatname+"genel/"+gerek[0:1]+"/"+gerek) is False:
2017-04-18 07:05:09 +02:00
print renk.uyari+gerek+" talimatı yapılmalı!"+renk.son
2017-04-18 06:36:44 +02:00
return gerekstr
2017-11-28 14:11:39 +01:00
def _gruplar(self):
grupstr=""
for grup in self.gruplar:
grupstr+=grup+" "
return grupstr
2017-04-18 06:36:44 +02:00
def _kaynaklar(self):
kaynakstr=""
for kaynak in self.kaynaklar:
kaynakstr+=kaynak+"\n"
return kaynakstr
def _ice_aktar_bloklar(self,dosya,tip):
if tip=="arch":
2017-04-18 06:40:50 +02:00
#bu sekilde de satirlar cekilebilir.
#lines = [line.rstrip('\n') for line in open(dosya)]
2017-04-18 06:36:44 +02:00
with open(dosya) as f:
satirlar = f.readlines()
blok=False
onblok=False
for satir in satirlar:
2017-12-07 12:29:36 +01:00
if "md5sums=(" in satir or "sha256sums=('" in satir or "sha1sums=('" in satir or "sha512sums=('" in satir:
2017-04-18 06:36:44 +02:00
onblok=True
if onblok is True and "')" in satir:
blok=True
continue
if blok and satir.rstrip()!="" and satir.rstrip()!="}":
2017-04-19 02:30:40 +02:00
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):
2017-04-18 06:36:44 +02:00
satir=satir.replace("pkgdir","PKG")
satir=satir.replace("srcdir","SRC")
2017-10-28 19:09:49 +02:00
satir=satir.replace("pkgname","isim")
satir=satir.replace("pkgver","surum")
satir=satir.replace("pkgrel","devir")
2017-04-18 06:36:44 +02:00
self.derleme+=satir+"\n"
else:
return "blok için gecersiz tip!"
def olustur(self):
if self.isim:
2017-04-18 07:05:09 +02:00
print renk.tamamb+self.isim+" talimatı hazırlanıyor..."+renk.son
os.system("mkdir -p "+self.isim)
2017-04-18 06:36:44 +02:00
open(self.isim+"/talimat","w").write(self.icerik())
def icerik(self):
icerikstr=""
2017-10-28 19:09:49 +02:00
icerikstr+="# Tanım: "+self.tanim+"\n"
2017-04-18 06:36:44 +02:00
icerikstr+="# URL: "+self.url+"\n"
2017-10-28 19:09:49 +02:00
icerikstr+="# Paketçi: "+self.paketci+"\n"
2017-11-28 14:11:39 +01:00
icerikstr+="# Gerekler: "+self._gerekler()+"\n"
icerikstr+="# Grup: "+self._gruplar()
2017-04-18 06:36:44 +02:00
icerikstr+="\n"+"\n"
2017-10-28 19:09:49 +02:00
icerikstr+="isim="+self.isim+"\n"
2017-04-18 14:37:54 +02:00
if self._isim !="":
2017-10-28 19:09:49 +02:00
icerikstr+="_isim="+self._isim+"\n"
icerikstr+="surum="+str(self.surum)+"\n"
icerikstr+="devir="+str(self.devir)+"\n"
icerikstr+="kaynak=("+self._kaynaklar()+")"
2017-04-18 06:36:44 +02:00
icerikstr+="\n"+"\n"
2017-10-28 23:04:20 +02:00
# boş satırların temizlenmesi
d_icerik = "".join([s for s in self.derleme.splitlines(True) if s.strip("\r\n")])
icerikstr+=d_icerik
2017-04-18 06:36:44 +02:00
icerikstr+="}"
return icerikstr
def cevir(self,dosya,tip="arch"):
self.ice_aktar(dosya,tip)
self.olustur()
2017-04-18 14:37:54 +02:00
print renk.tamamy+talimat.isim+" talimatı hazır."+renk.son
2017-04-18 06:36:44 +02:00
# archlinux pkgbuild sınıfı
#Copyright (c) 2009 Sebastian Nowicki (parched.py)
class PKGBUILD():
_symbol_regex = re.compile(r"\$(?P<name>{[\w\d_]+}|[\w\d]+)")
def __init__(self, name=None, fileobj=None):
self.install = ""
self.checksums = {
'md5': [],
'sha1': [],
'sha256': [],
'sha384': [],
'sha512': [],
}
self.noextract = []
self.sources = []
self.makedepends = []
# Symbol lookup table
self._var_map = {
'pkgname': 'name',
2017-04-18 14:37:54 +02:00
'_pkgname': '_name',
2017-04-18 06:36:44 +02:00
'pkgver': 'version',
'pkgdesc': 'description',
'pkgrel': 'release',
'source': 'sources',
'arch': 'architectures',
'license': 'licenses',
}
self._checksum_fields = (
'md5sums',
'sha1sums',
'sha256sums',
'sha384sums',
'sha512sums',
)
# Symbol table
self._symbols = {}
if not name and not fileobj:
raise ValueError("nothing to open")
should_close = False
if not fileobj:
fileobj = open(name, "r")
should_close = True
self._parse(fileobj)
if should_close:
fileobj.close()
def _handle_assign(self, token):
var, equals, value = token.strip().partition('=')
# Is it an array?
2017-10-28 23:04:20 +02:00
if value!="":
if value[0] == '(' and value[-1] == ')':
self._symbols[var] = self._clean_array(value)
else:
self._symbols[var] = self._clean(value)
2017-04-18 06:36:44 +02:00
else:
self._symbols[var] = self._clean(value)
def _parse(self, fileobj):
"""Parse PKGBUILD"""
if hasattr(fileobj, "seek"):
fileobj.seek(0)
parser = shlex.shlex(fileobj, posix=True)
parser.whitespace_split = True
in_function = False
while 1:
token = parser.get_token()
if token is None or token == '':
break
# Skip escaped newlines and functions
if token == '\n' or in_function:
continue
# Special case:
# Array elements are dispersed among tokens, we have to join
# them first
if token.find("=(") >= 0 and not token.rfind(")") >= 0:
in_array = True
elements = []
while in_array:
_token = parser.get_token()
if _token == '\n':
continue
if _token[-1] == ')':
_token = '"%s")' % _token.strip(')')
token = token.replace('=(', '=("', 1) + '"'
token = " ".join((token, " ".join(elements), _token))
in_array = False
else:
elements.append('"%s"' % _token.strip())
# Assignment
if re.match(r"^[\w\d_]+=", token):
self._handle_assign(token)
# Function definitions
elif token == '{':
in_function = True
elif token == '}' and in_function:
in_function = False
self._substitute()
self._assign_local()
if self.release:
self.release = float(self.release)
def _clean(self, value):
"""Pythonize a bash string"""
return " ".join(shlex.split(value))
def _clean_array(self, value):
"""Pythonize a bash array"""
return shlex.split(value.strip('()'))
def _replace_symbol(self, matchobj):
"""Replace a regex-matched variable with its value"""
symbol = matchobj.group('name').strip("{}")
# If the symbol isn't found fallback to an empty string, like bash
try:
value = self._symbols[symbol]
except KeyError:
value = ''
# BUG: Might result in an infinite loop, oops!
return self._symbol_regex.sub(self._replace_symbol, value)
def _substitute(self):
"""Substitute all bash variables within values with their values"""
for symbol in self._symbols:
value = self._symbols[symbol]
# FIXME: This is icky
if isinstance(value, str):
result = self._symbol_regex.sub(self._replace_symbol, value)
else:
result = [self._symbol_regex.sub(self._replace_symbol, x)
for x in value]
self._symbols[symbol] = result
def _assign_local(self):
"""Assign values from _symbols to PKGBUILD variables"""
for var in self._symbols:
value = self._symbols[var]
if var in self._checksum_fields:
key = var.replace('sums', '')
self.checksums[key] = value
else:
if var in self._var_map:
var = self._var_map[var]
setattr(self, var, value)
class renk:
baslik = '\033[95m'
tamamb = '\033[94m'
tamamy = '\033[92m'
uyari = '\033[93m'
hata = '\033[91m'
son = '\033[0m'
kalin = '\033[1m'
altcizgili = '\033[4m'
2017-04-18 14:37:54 +02:00
class Arge:
2017-11-07 04:24:53 +01:00
# arch pkgbuild linki indirme
2017-04-18 14:37:54 +02:00
def indir(self,link):
2017-10-13 04:33:40 +02:00
paket=link.split("/")[-1]
if paket == "":
paket=link.split("/")[-2]
if "=" in paket:
paket=paket.split("=")[1]
2017-04-18 14:37:54 +02:00
print renk.tamamb+paket+" indiriliyor..."+renk.son
2017-04-18 15:00:29 +02:00
try:
veri = urllib2.urlopen(link)
open(paket+"_pkgbuild","w").write(veri.read())
return paket+"_pkgbuild"
except urllib2.HTTPError, e:
if e.code == 404:
print renk.hata+link+" bulunamadı!"+renk.son
return None
2017-11-07 04:24:53 +01:00
2017-04-18 14:37:54 +02:00
def aur_link(self,paket):
link="https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h="+paket
return link
2017-04-19 02:30:40 +02:00
def arch2_link(self,paket):
2017-04-19 00:52:27 +02:00
link="https://git.archlinux.org/svntogit/community.git/plain/trunk/PKGBUILD?h=packages/"+paket
return link
2017-04-19 02:30:40 +02:00
def arch_link(self,paket):
link="https://git.archlinux.org/svntogit/packages.git/plain/trunk/PKGBUILD?h=packages/"+paket
return link
2017-11-07 04:24:53 +01:00
# alpine apkbuild linki indirme
def indir_alp(self,paket,tip="isim"):
if tip=="isim":
repolar=["main","community","testing"]
for repo in repolar:
link="https://git.alpinelinux.org/cgit/aports/plain/"+repo+"/"+paket+"/APKBUILD"
try:
veri = urllib2.urlopen(link)
print renk.tamamb+repo+" reposunda bulundu"+renk.son
open(paket+"_apkbuild","w").write(veri.read())
return paket+"_apkbuild"
except urllib2.HTTPError, e:
if e.code == 404:
print renk.hata+link+" bulunamadı!"+renk.son
elif tip=="link":
paket=link.split("/")[-2]
print renk.tamamb+paket+" indiriliyor..."+renk.son
try:
veri = urllib2.urlopen(link)
open(paket+"_apkbuild","w").write(veri.read())
return paket+"_apkbuild"
except urllib2.HTTPError, e:
if e.code == 404:
print renk.hata+link+" bulunamadı!"+renk.son
return None
return None
2017-04-19 02:30:40 +02:00
2017-11-28 14:11:39 +01:00
# kaosx github linki indirme
def indir_kaos(self,paket,tip="isim"):
if tip=="isim":
repolar=["main","apps","core"]
for repo in repolar:
link="https://raw.githubusercontent.com/KaOSx/"+repo+"/master/"+paket+"/PKGBUILD"
try:
veri = urllib2.urlopen(link)
print renk.tamamb+repo+" reposunda bulundu"+renk.son
open(paket+"_pkgbuild","w").write(veri.read())
return paket+"_pkgbuild"
except urllib2.HTTPError, e:
if e.code == 404:
print renk.hata+link+" bulunamadı!"+renk.son
elif tip=="link":
paket=link.split("/")[-2]
print renk.tamamb+paket+" indiriliyor..."+renk.son
try:
veri = urllib2.urlopen(link)
open(paket+"_pkgbuild","w").write(veri.read())
return paket+"_pkgbuild"
except urllib2.HTTPError, e:
if e.code == 404:
print renk.hata+link+" bulunamadı!"+renk.son
return None
return None
# blackarch github linki indirme
def indir_blackarch(self,paket,tip="isim"):
if tip=="isim":
repolar=["packages"]
for repo in repolar:
link="https://raw.githubusercontent.com/BlackArch/blackarch/master/"+repo+"/"+paket+"/PKGBUILD"
try:
veri = urllib2.urlopen(link)
print renk.tamamb+repo+" reposunda bulundu"+renk.son
open(paket+"_pkgbuild","w").write(veri.read())
return paket+"_pkgbuild"
except urllib2.HTTPError, e:
if e.code == 404:
print renk.hata+link+" bulunamadı!"+renk.son
elif tip=="link":
paket=link.split("/")[-2]
print renk.tamamb+paket+" indiriliyor..."+renk.son
try:
veri = urllib2.urlopen(link)
open(paket+"_pkgbuild","w").write(veri.read())
return paket+"_pkgbuild"
except urllib2.HTTPError, e:
if e.code == 404:
print renk.hata+link+" bulunamadı!"+renk.son
return None
return None
def yardim(self):
print renk.tamamb+"talimat.py -a (archlinux) paket_ismi | url "+renk.son
print renk.tamamb+"talimat.py -k (kaosx) paket_ismi | url "+renk.son
print renk.tamamb+"talimat.py -b (blackarch) paket_ismi | url "+renk.son
2017-04-18 06:36:44 +02:00
if __name__ == '__main__':
2017-11-28 14:11:39 +01:00
arge=Arge()
2017-04-18 06:36:44 +02:00
if len(sys.argv) > 1:
dosya=sys.argv[1]
2017-04-18 14:37:54 +02:00
talimat=Talimat()
2017-04-18 06:36:44 +02:00
if os.path.exists(dosya):
talimat.cevir(dosya)
2017-11-07 04:24:53 +01:00
elif len(sys.argv) > 2:
if dosya == "-a":
dosya=sys.argv[2]
if "https" in dosya or "http" in dosya:
Pdosya=arge.indir(dosya)
talimat.cevir(Pdosya)
else :
paket=sys.argv[2]
paket=str(paket)
link=arge.aur_link(paket)
2017-04-19 00:52:27 +02:00
dosya=arge.indir(link)
2017-04-19 02:30:40 +02:00
if dosya is None:
2017-11-07 04:24:53 +01:00
link=arge.arch_link(paket)
2017-04-19 02:30:40 +02:00
dosya=arge.indir(link)
2017-11-07 04:24:53 +01:00
if dosya is None:
link=arge.arch2_link(paket)
dosya=arge.indir(link)
if link and dosya:
talimat.cevir(dosya)
2017-11-28 14:11:39 +01:00
elif dosya == "-k":
link=sys.argv[2]
if "https" in link or "http" in link:
Pdosya=arge.indir_kaos(link,"link")
talimat.cevir(Pdosya)
else :
paket=sys.argv[2]
paket=str(paket)
dosya=arge.indir_kaos(paket,"isim")
if dosya:
talimat.cevir(dosya)
else:
renk.hata+str(dosya)+" repolarda bulunamadı!"+renk.son
elif dosya == "-b":
link=sys.argv[2]
if "https" in link or "http" in link:
Pdosya=arge.indir_blackarch(link,"link")
talimat.cevir(Pdosya)
else :
paket=sys.argv[2]
paket=str(paket)
dosya=arge.indir_blackarch(paket,"isim")
if dosya:
talimat.cevir(dosya)
else:
renk.hata+str(dosya)+" repolarda bulunamadı!"+renk.son
2017-11-07 04:24:53 +01:00
elif dosya == "-al":
link=sys.argv[2]
if "https" in link or "http" in link:
Pdosya=arge.indir_alp(link,"link")
print Pdosya
#talimat.cevir(Pdosya)
else :
paket=sys.argv[2]
paket=str(paket)
dosya=arge.indir_alp(paket,"isim")
if dosya:
print dosya
#talimat.cevir(dosya)
else:
renk.hata+str(dosya)+" repolarda bulunamadı!"+renk.son
2017-04-18 06:36:44 +02:00
else:
2017-04-18 14:37:54 +02:00
print renk.hata+dosya+" paremetre bulunamadı!"+renk.son
2017-11-28 14:11:39 +01:00
else:
arge.yardim()