Created
July 4, 2012 17:57
-
-
Save anonymous/3048599 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, shlex, subprocess, imp, psutil | |
from subprocess import PIPE | |
check = True | |
except ImportError: | |
import sys, os, shlex, subprocess, imp | |
from subprocess import PIPE | |
check = False | |
PROG = 'modul_control.py' | |
# Alle Dateien des Ordners in FILE_LIST speichern. | |
FILE_LIST = set() | |
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) | |
APT_GET = 'sudo apt-get install -y' | |
APTITUDE = 'sudo aptitude install -y' | |
PIP = 'pip install' | |
COMMAND_QUESTION = 'Welchen Befehl möchten Sie für die\nInstallation \ | |
von Paketen verwenden?\n1: %s\n2: %s\n3: %s' | |
IMPORT_INFO = 'Das Paket %s wird für das Starten\ndes Python-Modul \ | |
%s benötigt.\nDas Paket %s ist nicht installiert ist!\n\nSoll das \ | |
Paket %s jetzt installiert werden?' | |
# Programm | |
# Funktion zur 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) | |
if 'sudo' in args: | |
print ('Geben Sie Ihr Passwort ein!') | |
installation = False | |
counter = 0 | |
while installation != True: | |
counter += 1 | |
try: | |
process = subprocess.Popen(args, shell=False, stdout=subprocess.PIPE) | |
print ('') | |
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 installation == True: | |
if 'psutil' in paket: | |
print ('\nDie Installation von %s war erfolgreich,\nbitte starten Sie das Programm jetzt erneut!') % paket | |
sys.exit(0) | |
else: | |
print ('\nEs ist ein Fehler bei der Installation von %s aufgetreten, \nbitte starten Sie das Programm jetzt erneut!') % paket | |
sys.exit(1) | |
# Funktion zur Auswahl des Installation-Befehl | |
install_command = set() | |
def command(e_name): | |
counter = 0 | |
yes = False | |
while yes != True: | |
print (COMMAND_QUESTION % (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]) | |
yes = True | |
elif number == '2': | |
install_command.add(APTITUDE) | |
print ('\nSie haben %s gewählt!\n' % APTITUDE.split(None, 2)[1]) | |
yes = True | |
elif number == '3' and e_name != 'pip' and check != False: | |
no_pip() | |
install_command.add(PIP) | |
print ('\nSie haben %s gewählt!\n' % PIP.split(None, 2)[0]) | |
yes = True | |
else: | |
if counter <= 3: | |
print ('') | |
if e_name != 'pip': | |
print ('Nummer %s existiert nicht!\n' % number) | |
else: | |
print ('Nummer %s ist nicht installiert, \nbitte wählen Sie eine ander Option!' % number) | |
print ('') | |
else: | |
print ('Eingabefehler, Abbruch!') | |
yes = True | |
sys.exit() | |
# 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 = False | |
def no_name(e_name): | |
if 'psutil' == e_name: | |
print ('Das Modul %s wird zur Funktionsfähigkeit \ndieses Moduls benötigt!\n') % e_name | |
if not 'python' in e_name: | |
modul = 'python-%s' % e_name | |
command_line = 'apt-cache search %s' % (modul.lstrip('-')) | |
question = ('Möchten Sie das Paket %s installieren?\n< ja / nein >: ') % e_name | |
answer = raw_input(question) | |
print ('') | |
args = shlex.split(command_line) | |
process = subprocess.call(args, shell=False,) | |
paket = '' | |
if answer == 'ja': | |
text = ('\nGeben Sie das zu installierende Paket für %s ein!: ') % e_name | |
paket = raw_input(text) | |
print ('') | |
elif answer == 'nein': | |
if check != False: | |
print ('\nInstallation von %s wurde abgebrochen!\n' % e_name) | |
global error_print | |
error_print = True | |
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) | |
if paket != '': | |
if modul == 'pip': | |
install_command.remove | |
command(modul) | |
command(modul) | |
installation(paket) | |
# Funktion zur Überprüfung ob Modul geladen werden kann | |
def no_pip(): | |
try: | |
import pip | |
check = True | |
except ImportError, e: | |
check = False | |
e_name = e.message.split(' ')[-1] | |
no_name(e_name) | |
def no_mod(): | |
try: | |
import psutil | |
check = True | |
except ImportError, e: | |
check = False | |
e_name = e.message.split(' ')[-1] | |
no_name(e_name) | |
no_mod() | |
# Funktion zur Überprüfung von Fehlermeldungen | |
BaseExceptionInfo = set() | |
NameErrorInfo = set() | |
def modul_check(): | |
counter = 0 | |
for prog in FILE_LIST: | |
if '.py' in prog and not '.pyc' in prog and not PROG in prog: | |
datei = prog | |
modul = prog.split('.py')[0] | |
try: | |
if counter == 0: | |
no_mod() | |
imp.load_source(modul, datei) | |
counter = 1 | |
else: | |
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] | |
print '\nFehler in Datei %s mit Modul %s\n' % (datei, e_name) | |
counter += 1 | |
if counter == 1: | |
command(e_name) | |
print ('') | |
no_name(e_name) | |
print ('') | |
else: | |
e_name = e.message.rsplit(None, 2)[-2] | |
print '\nFehler in Datei %s mit Modul %s\n' % (datei, e_name) | |
counter += 1 | |
if counter == 1: | |
command(e_name) | |
if 'please install the' in str(e): | |
paket = e.message.rsplit(None, 2)[1] | |
if paket: | |
print (IMPORT_INFO % (paket, datei, paket, paket)) | |
answer = raw_input('< ja / nein >: ') | |
if answer == 'ja': | |
installation(paket) | |
print ('') | |
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] | |
print '\nFehler in Datei %s mit Modul %s' % (datei, e_name) | |
no_name(e_name) | |
if error_print == True: | |
with open(datei, 'r') as infile: | |
for find in infile: | |
if modul in find: | |
result = (datei, e) | |
NameErrorInfo.add(result) | |
break | |
except BaseException, e: | |
result = (datei, e) | |
BaseExceptionInfo.add(result) | |
finally: | |
if datei: | |
#print 'yes' | |
for prog in psutil.process_iter(): | |
#if prog.cmdline: | |
#print prog.cmdline | |
if 'c.py' in prog.name or 'c.py' in prog.cmdline: | |
print '2' | |
psutil.Process(prog.pid).terminate() | |
# Funktion zur Auswertung von Fehlermeldungen, | |
# die nicht ImportError entsprechen. | |
def error_info(): | |
print ('') | |
for info in NameErrorInfo: | |
if info: | |
print ('NameError:') | |
print (info) | |
print ('') | |
print ('') | |
for info in BaseExceptionInfo: | |
if info: | |
print ('BaseException:') | |
print (info) | |
print ('') | |
print '\nÜberprüfung ist beendet!' | |
sys.exit() | |
if __name__ == "__main__": | |
modul_check() | |
error_info() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment