Skip to content

Instantly share code, notes, and snippets.

Created July 2, 2012 17:01
Show Gist options
  • Save anonymous/3034277 to your computer and use it in GitHub Desktop.
Save anonymous/3034277 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.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, os, shlex, subprocess, imp, psutil
PROG = 'modul_control.py'
PATH = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))
FILE_LIST = os.listdir(PATH)
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
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 ('\nGeben Sie Ihr Passwort ein!')
subprocess.call(args, shell=False)
def no_pip():
try:
import pip
except ImportError, e:
no_name(e)
def no_name(e):
if e:
modul = e.message.split(' ')[-1]
if not 'python' in modul:
modul = 'python-%s' % modul
command_line = 'apt-cache search %s' % (modul.lstrip('-'))
args = shlex.split(command_line)
process = subprocess.call(args, shell=False,)
if process == 0:
paket = raw_input('\nGeben Sie da zu installierende Paket ein!: ')
if paket != '':
if modul == 'pip':
install_command.remove
command()
installation(paket)
if modul == 'pip':
exit(0)
else:
print ('\nIhre Eingabe war falsch oder Sie haben keine Eingabe gemacht!\n%s wird beendet!' % PROG)
exit(0)
install_command = set()
def command():
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':
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 ('Nummer %s existiert nicht!\n' % number)
else:
print ('Eingabefehler, Abbruch!')
yes = True
sys.exit()
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:
imp.load_source(modul, datei)
for prog in psutil.process_iter():
if datei in prog.cmdline:
psutil.Process(prog.pid).terminate()
break
print ('Modul %s ist ok!' % datei)
except ImportError, e:
if not 'please install the' in str(e):
if PIP not in install_command:
counter += 1
print ('')
command()
no_name(e)
print ('')
else:
counter += 1
if counter == 1:
command()
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:
modul = e.message.split('\'')[1]
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)
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