Created
July 9, 2012 09:57
-
-
Save anonymous/3075514 to your computer and use it in GitHub Desktop.
Überprüfung von Modulen in einem Ordner, auf fehlende Pakete mit der Option diese zu installieren und Ausgabe weiterer Fehlermeldungen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
try: | |
import sys, os, time, shlex, subprocess, imp, pip | |
from subprocess import PIPE | |
check = True | |
except ImportError: | |
import sys, os, time, shlex, subprocess, imp | |
from subprocess import PIPE | |
check = False | |
PROG = 'modul_control.py' | |
# Programm | |
def info(): | |
print (''' | |
Dieses Modul ist für Linux konzipiert und auf DEBIAN / UBUNTU getestet. | |
Es überprüft die Lauffähigkeit der Module mit der Endung py. | |
Fehlermeldungen werden in folgender Reihenfolge ausgewertet: | |
ImportError, NameErrror und BaseException. | |
Fehlende Pakete für die Funktionsfähigkeit der Module, werden zur | |
Installation angeboten. Für Fehler, die nicht behoben werden können, | |
erfolgt eine Fehlerausgabe am Ende. | |
Die Anwendung von modul_control.py ist recht einfach. | |
Sie stellen modul_control.py in den Ordner, in dem Sie Ihre Module | |
überprüfen möchten und starten es aus der Konsole heraus. | |
''') | |
print ('\nWählen Sie die Nummer \n1: Programmstart \n2: Abbruch: ') | |
decision = raw_input('Nummer: ') | |
if decision == '1': | |
print ('\n%s wird gestartet!\n\n' % PROG) | |
elif decision == '2': | |
print ('\n%s wird beendet!' % PROG) | |
sys.exit(0) | |
# Funktion zur Auflistung | |
# aller Dateien des aktuellen Ordners. | |
file_list = set() | |
def filelist(): | |
PATH = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) | |
LIST = os.listdir(PATH) | |
for files in LIST: | |
if os.path.isfile(files): | |
file_list.add(files) | |
# Funktion zur Überprüfung des Betriebssystem. | |
# Bei Windows erfolgt Abbruch! | |
if os.name != 'posix': | |
print ('Sorry, Sie haben kein Linux! \nOb mudul_control auch auf Windows-Systemen läuft, \nwurde nicht getestet!') | |
sys.exit(0) | |
# Funktion zur Überprüfung | |
# ob Name von e_name in Paketliste enthalten ist. | |
paketline = set() | |
def name_check(e_name): | |
if not 'python' in e_name: | |
modul = 'python-%s' % e_name | |
command_line = 'apt-cache search %s' % (modul.lstrip('-')) | |
args = shlex.split(command_line) | |
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE) | |
process.wait() | |
for line in process.stdout.readlines(): | |
if line: | |
paketline.add(line) | |
# Funktion zur Überprüfung | |
# ob ausgewähltes Paket schon installiert ist. | |
installinfo = set() | |
def install_check(paket): | |
# Überprüfe ob Paket nicht installiert ist. | |
install_test = 'apt-cache policy %s' % paket | |
args = shlex.split(install_test) | |
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE) | |
process.wait() | |
for line in process.stdout.readlines(): | |
if 'Installiert:' in line and not 'Installiert: (keine)' in line: | |
print ('\nPaket %s ist installiert!\nÜberprüfen Sie bitte Ihr Modul auf Fehler!\n' % (paket, paket)) | |
else: | |
installinfo.add(line) | |
print ('\nPaket %s ist nicht installiert!\nSie können %s jetzt installieren!\n' % (paket, paket)) | |
break | |
# Funktion zur Auswahl des Installation-Befehl | |
# PIP ist momentan noch nicht einsetzbar und | |
# muß dafür noch erweitert werden! | |
APT_GET = 'sudo apt-get install -y' | |
APTITUDE = 'sudo aptitude install -y' | |
PIP = 'pip install' | |
install_command = set() | |
def command(e_name): | |
counter = 0 | |
yes = False | |
while yes != True: | |
print ('\nWelchen Befehl möchten Sie für die\nInstallation von Paketen verwenden?:\n1: {}\n2: {}\n3: {}'.format(APT_GET.split(None, 2)[1], APTITUDE.split(None, 2)[1], PIP.split(None, 2)[0])) | |
number = raw_input('Nummer: ') | |
counter += 1 | |
if number == '1': | |
install_command.add(APT_GET) | |
print ('\nSie haben %s gewählt!\n' % APT_GET.split(None, 2)[1]) | |
print ('Geben Sie Ihr Passwort ein!') | |
yes = True | |
elif number == '2': | |
install_command.add(APTITUDE) | |
print ('\nSie haben %s gewählt!\n' % APTITUDE.split(None, 2)[1]) | |
print ('Geben Sie Ihr Passwort ein!') | |
yes = True | |
elif number == '3': | |
if e_name == 'pip' and check != False: | |
no_name(e_name) | |
install_command.add(PIP) | |
elif e_name == 'pip' and check != True: | |
print ('\nSie haben %s gewählt!\n' % PIP.split(None, 2)[0]) | |
yes = True | |
elif e_name != 'pip' and check != False: | |
print ('\nNummer %s ist nicht installiert, \nbitte wählen Sie eine andere Option!\n' % number) | |
else: | |
if counter == 4: | |
print ('Eingabefehler, Abbruch!') | |
yes = True | |
sys.exit() | |
else: | |
print ('\nEingabefehler, es gibt keine Nummer %s, \nbitte wählen Sie erneut eine Option!\n' % number) | |
# Funktion zur Ausführung der Installation von Paketen. | |
def installation(paket): | |
for output in install_command: | |
command_install = output | |
command_line = '%s %s' % (command_install, paket.lstrip('-')) | |
args = shlex.split(command_line) | |
installation = False | |
counter = 0 | |
while not installation: | |
counter += 1 | |
try: | |
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE) | |
for line in process.communicate(): | |
print (line) | |
installation = True | |
except BaseException, e: | |
print ('Installationsfehler: %s' %e) | |
installation = False | |
if installation == False and counter < 3: | |
print ('\nEs gab einen Fehler, die Installation wird wiederholt!') | |
elif installation == False and counter == 3: | |
print ('\nDie Installation ist fehlerhaft und wird daher beendet!') | |
if not installation: | |
print ('\nEs ist ein Fehler bei der Installation von %s aufgetreten, \nbitte starten Sie das Programm jetzt erneut!' % paket) | |
sys.exit(1) | |
# Funktion zu ImportError für, | |
# Module die kein Paket für die Installation angeboten bekommen, | |
# sondern nur den Namen des Moduls. | |
# Hier werden Pakete aus der apt-Paketliste angeboten. | |
# Die Auswahl des zu installierenden Paketes, | |
# muß manuell vorgenommen werden. | |
error_print = set() | |
def no_name(e_name): | |
if not 'python' in e_name: | |
modul = 'python-%s' % e_name | |
question = ('Möchten Sie das Paket %s \naus der Paketliste aussuchen und installieren?\n< ja / nein >: ' % e_name) | |
answer = raw_input(question) | |
if answer == 'ja': | |
name_check(e_name) | |
if paketline: | |
print ('\nFolgende Pakete wurden gefunden:\n') | |
for line in paketline: | |
print line | |
if answer == 'ja': | |
text = ('\nGeben Sie das zu installierende Paket für %s ein!: ' % e_name) | |
paket = raw_input(text) | |
install_check(paket) | |
if installinfo: | |
if modul == 'pip': | |
install_command.remove | |
if install_command: | |
installation(paket) | |
print ('') | |
else: | |
command(modul) | |
print ('') | |
installation(paket) | |
print ('') | |
paketline.remove | |
installinfo.remove | |
elif answer == 'nein': | |
if check != False: | |
print ('\nInstallation von %s wurde abgebrochen!\n' % e_name) | |
error_print.add('y') | |
paketline.remove | |
else: | |
print ('\nInstallation von %s wurde abgebrochen!' % e_name) | |
sys.exit(1) | |
else: | |
print ('\nFehlerhafte Eingabe, Installation von %s wurde abgebrochen!' % e_name) | |
sys.exit(1) | |
else: | |
print ('\nEs existieren keine Pakete für %s in der Paketliste! \nVielleicht ist der Namen des Moduls falsch geschrieben \noder es handelt sich um ein Fremdpaket, das Sie manuell \ninstallieren müssen! \nEine Installation ist daher nicht möglich!\n' % e_name) | |
error_print.add('y') | |
time.sleep(3) | |
paketline.remove | |
# Funktion zur Überprüfung von Fehlermeldungen. | |
# Die Reihenfolge ist: | |
# ImportError und NameError | |
# BaseException gibt alle restlichen Fehlermeldungen aus. | |
BaseExceptionInfo = set() | |
NameErrorInfo = set() | |
ImportErrorInfo = set() | |
def modul_check(): | |
for prog in file_list: | |
if prog[(len(prog) - 3):] == '.py' and prog != PROG: | |
datei = prog | |
modul = prog[:(len(prog) - 3)] | |
try: | |
imp.load_source(modul, datei) | |
print ('Modul %s ist ok!' % datei) | |
except ImportError, e: | |
if not 'please install the' in str(e): | |
e_name = e.message.rsplit(None, 2)[-1] | |
name_check(e_name) | |
if paketline: | |
no_name(e_name) | |
print ('\nFehler in Datei %s mit Modul %s\n' % (datei, e_name)) | |
else: | |
ImportErrorInfo.add(e) | |
else: | |
e_name = e.message.rsplit(None, 2)[-2] | |
print ('\nFehler in Datei %s mit Modul %s\n' % (datei, e_name)) | |
if 'please install the' in str(e): | |
paket = e.message.rsplit(None, 2)[1] | |
install_check(paket) | |
if installinfo: | |
print ('Das Paket {} wird für das Starten\ndes Python-Modul {} benötigt.\nDas Paket {} ist nicht installiert!\n\nSoll das Paket {} jetzt installiert werden?'.format(paket, datei, paket, paket)) | |
text = '< ja / nein >: \n' | |
answer = raw_input(text) | |
if answer == 'ja': | |
if not install_command: | |
command(e_name) | |
installation(paket) | |
elif answer == 'nein': | |
print ('\nInstallation von %s wurde abgebrochen!' % paket) | |
else: | |
print ('\nFehlerhafte Eingabe, Installation von %s wurde abgebrochen!' % paket) | |
sys.exit(1) | |
except NameError, e: | |
e_name = e.message.split('\'')[1] | |
name_check(e_name) | |
if paketline: | |
print ('\nFehler in Datei %s mit Modul %s\n' % (datei, e_name)) | |
no_name(e_name) | |
else: | |
error_print.add('y') | |
for line in error_print: | |
if line == 'y': | |
with open(datei, 'r') as infile: | |
for find in infile: | |
if modul in find: | |
result = (datei, e) | |
NameErrorInfo.add(result) | |
break | |
error_print.remove | |
installinfo.remove | |
except BaseException, e: | |
result = (datei, e) | |
BaseExceptionInfo.add(result) | |
# Funktion zur Auswertung von Fehlermeldungen, | |
# die nicht ImportError entsprechen. | |
def error_info(): | |
if ImportErrorInfo or NameErrorInfo or BaseExceptionInfo: | |
print ('\n\nÜberprüfung ist beendet, \nfolgende Fehler müssen manuell von Ihnen behoben werden!\n') | |
for info in ImportErrorInfo: | |
if info: | |
print ('ImportError:') | |
print ('{}\n'.format(info)) | |
for info in NameErrorInfo: | |
if info: | |
print ('NameError:') | |
print ('{}\n'.format(info)) | |
for info in BaseExceptionInfo: | |
if info: | |
print ('BaseException:') | |
print ('{}\n'.format(info)) | |
sys.exit() | |
else: | |
print ('\nÜberprüfung ist beendet, \nes wurden keine Fehler festgestellt!') | |
sys.exit() | |
def expiration(): | |
info() | |
filelist() | |
modul_check() | |
error_info() | |
if __name__ == "__main__": | |
expiration() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment