Skip to content

Instantly share code, notes, and snippets.

Created July 1, 2012 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3028598 to your computer and use it in GitHub Desktop.
Save anonymous/3028598 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.
python modul_control.pyWelchen Befehl möchten Sie für die
Installation von Paketen verwenden?
1: apt-get
2: aptitude
Nummer: 1
Sie haben apt-get gewählt!
Das Paket python-tk wird für das Starten
des Python-Modul lbestellorder.py benötigt.
Das Paket python-tk ist nicht installiert ist!
Soll das Paket python-tk jetzt installiert werden?
< ja / nein >: ja
Geben Sie Ihr Passwort ein!
[sudo] password for whtb:
Paketlisten werden gelesen... Fertig
Abhängigkeitsbaum wird aufgebaut
Statusinformationen werden eingelesen... Fertig
Vorgeschlagene Pakete:
tix python-tk-dbg
Die folgenden NEUEN Pakete werden installiert:
python-tk
0 aktualisiert, 1 neu installiert, 0 zu entfernen und 0 nicht aktualisiert.
Es müssen noch 0 B von 28,3 kB an Archiven heruntergeladen werden.
Nach dieser Operation werden 118 kB Plattenplatz zusätzlich benutzt.
Vormals nicht ausgewähltes Paket python-tk wird gewählt.
(Lese Datenbank ... 222454 Dateien und Verzeichnisse sind derzeit installiert.)
Entpacken von python-tk (aus .../python-tk_2.7.3-1ubuntu1_amd64.deb) ...
python-tk (2.7.3-1ubuntu1) wird eingerichtet ...
('ex: ', 'versand_email.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'gui_dialog.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'prog_param.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'file_pool.py', NameError("global name 'psutil' is not defined",))
/media/daten/Scripte/officeplanet/lieferantenbestellung
['ftp_abholliste', 'rueckstand', 'gui_start.pyc', 'lbestellorder.py', 'versand_email.py', 'gui_dialog.py', 'c.pyc', 'offen', 'a.pyc', 'fehler', 'beispiel.log', '.directory', 'vendor_basis.txt~', 'prog_param.py', 'gui_break.pyc', 'file_pool.py', 'file_pool.pyc', 'send_mails', 'modul_control.py', 'c.py', 'prog_control.pyc', 'write_modul.pyc', 'export', 'prog_control.py', 'lspezifikation.pyc', '__pycache__', 'versand_email.pyc', 'versand_ftp.pyc', 'b.py', 'a.py', 'auftrag', 'lbestellorder.pyc', 'write_modul.py', 'prog_param.pyc', 'sicherung', 'versand_ftp.py', 'vendor_basis.txt', 'gui_dialog.pyc', 'gui_start.py', 'lspezifikation.py', 'b.pyc', 'modul_control.pyc', 'gui_break.py']
('ex: ', 'c.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'prog_control.py', NameError("global name 'psutil' is not defined",))
Alles ok!
('ex: ', 'b.py', SystemExit(1,))
/media/daten/Scripte/officeplanet/lieferantenbestellung
('ex: ', 'a.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'write_modul.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'versand_ftp.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'gui_start.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'lspezifikation.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'gui_break.py', NameError("global name 'psutil' is not defined",))
Überprüfung ist beendet!
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, os, shlex, subprocess, imp
PROG = 'modul_control.py'
PATH = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0])))
FILE_LIST = os.listdir(PATH)
DATEI = 'gui_start.py'
MODUL = DATEI.split('.')[0]
APT_GET = 'sudo apt-get install -y'
APTITUDE = 'sudo aptitude install -y'
COMMAND_QUESTION = '''Welchen Befehl möchten Sie für die
Installation von Paketen verwenden?
1: %s
2: %s'''
IMPORT_INFO = '''Das Paket %s wird für das Starten
des Python-Modul %s benötigt.
Das Paket %s ist nicht installiert ist!
Soll das Paket %s jetzt installiert werden?'''
def prog_kill(progname):
for prog in psutil.process_iter():
for line in prog.cmdline:
if progname in line:
psutil.Process(prog.pid).terminate()
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]))
number = raw_input('Nummer: ')
counter += 1
if number == '1':
install_command.add(APT_GET)
yes = True
elif number == '2':
install_command.add(APTITUDE)
yes = True
else:
if counter <= 3:
print ('Nummer %s existiert nicht!' % number)
print ('')
else:
print ('Eingabefehler, Abbruch!')
yes = True
sys.exit()
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)
prog_kill(datei)
print ('Modul %s ist ok!' % datei)
except ImportError, e:
counter += 1
if counter == 1:
command()
print ('')
for output in install_command:
print ('Sie haben %s gewählt!' % output.split(None, 2)[1])
print ('')
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':
for output in install_command:
command_install = output
command_line = '%s %s' % (command_install, paket.lstrip('-'))
args = shlex.split(command_line)
print ('')
print ('Geben Sie Ihr Passwort ein!')
subprocess.call(args, shell=False)
elif answer == 'nein':
print ('')
print ('Installation von %s wurde abgebrochen!' % paket)
else:
print ('')
print ('Fehlerhafte Eingabe, Installation von %s wurde abgebrochen!' % paket)
sys.exit(1)
except BaseException, e:
print ('')
print ('ex: ', datei, e)
print ''
print 'Überprüfung ist beendet!'
sys.exit()
if __name__ == "__main__":
modul_check()
python modul_control.py
('ex: ', 'lbestellorder.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'versand_email.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'gui_dialog.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'prog_param.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'file_pool.py', NameError("global name 'psutil' is not defined",))
/media/daten/Scripte/officeplanet/lieferantenbestellung
['ftp_abholliste', 'rueckstand', 'gui_start.pyc', 'lbestellorder.py', 'versand_email.py', 'gui_dialog.py', 'c.pyc', 'offen', 'a.pyc', 'fehler', 'beispiel.log', '.directory', 'vendor_basis.txt~', 'prog_param.py', 'gui_break.pyc', 'file_pool.py', 'file_pool.pyc', 'send_mails', 'modul_control.py', 'c.py', 'prog_control.pyc', 'write_modul.pyc', 'export', 'prog_control.py', 'lspezifikation.pyc', '__pycache__', 'versand_email.pyc', 'versand_ftp.pyc', 'b.py', 'a.py', 'auftrag', 'lbestellorder.pyc', 'write_modul.py', 'prog_param.pyc', 'sicherung', 'versand_ftp.py', 'vendor_basis.txt', 'gui_dialog.pyc', 'gui_start.py', 'lspezifikation.py', 'b.pyc', 'modul_control.pyc', 'gui_break.py']
('ex: ', 'c.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'prog_control.py', NameError("global name 'psutil' is not defined",))
Alles ok!
('ex: ', 'b.py', SystemExit(1,))
/media/daten/Scripte/officeplanet/lieferantenbestellung
('ex: ', 'a.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'write_modul.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'versand_ftp.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'gui_start.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'lspezifikation.py', NameError("global name 'psutil' is not defined",))
('ex: ', 'gui_break.py', NameError("global name 'psutil' is not defined",))
Überprüfung ist beendet!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment