ressuscite.py: Premiere version

... qui ne marche pas encore tres tres bien ...

darcs-hash:20080923231839-ddb99-69e05dc6e87f55ab7fdc09fd05af4d7cd2f1d728.gz
This commit is contained in:
Michel Blockelet 2008-09-24 01:18:39 +02:00
parent f91b920755
commit dee715c3db

190
gestion/ressuscite.py Executable file
View file

@ -0,0 +1,190 @@
#!/usr/bin/env python
# -*- mode: python; coding: utf-8 -*-
#
# ressuscite.py
# -------------
#
# Copyright (C) 2008 Michel Blockelet <blockelet@crans.org>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
u"""Outil pour ressusciter des machines du cimetière."""
import os, sys
import cPickle
sys.path.append('/usr/scripts/gestion')
from ldap_crans import crans_ldap, MachineFixe, MachineWifi
from gest_crans import select, set_machine
from whos import aff
import affich_tools
import user_tests
db = crans_ldap()
isadm = user_tests.isadm()
def dialog(arg):
return affich_tools.dialog(u'Ressusciter une machine', arg)
def menu_principal():
while 1:
arg = u'--title "Ressusciter" '
arg += u'--help-button --item-help --cancel-label "Quitter" '
arg += u'--default-item "" '
arg += u'--menu "Que souhaitez vous faire ?" 0 55 13 '
arg += u'"A" "Ressusciter à partir d\'un adhérent" "" '
annul, result = dialog(arg)
if annul: break
choix = result[0]
if choix == 'A':
# Ressusciter à partir d'un adhérent
# On sélectionne l'adhérent
adh = select(db, u'Adhérent auquel rechercher les anciennes machines a', mde='ro')
if not adh: continue
# On sélectionne le type de machine
arg = u'--title "Recherche de machine" '
arg += u'--menu "Type de machine ?" 0 0 0 '
arg += u'"Fixe" "Machine fixe" '
arg += u'"Wifi" "Machine wireless" '
arg += u'"Tous" "Rechercher les deux types" '
annul, result = dialog(arg)
if annul: continue
choix = result[0]
# On fait la liste des machines
cimetiere = []
if choix == 'Fixe' or choix == 'Tous':
cimetiere = map(lambda s: "/home/cimetiere/MachineFixe/" + s,
os.listdir("/home/cimetiere/MachineFixe"))
if choix == 'Wifi' or choix == 'Tous':
cimetiere += map(lambda s: "/home/cimetiere/MachineWifi/" + s,
os.listdir("/home/cimetiere/MachineWifi"))
valid = []
for fichier in cimetiere:
try:
machine = cPickle.load(open(fichier, 'r'))
# Est-ce que c'est bien une machine de l'adhérent ?
if (int(machine.dn.split(',', 3)[1].split('=', 2)[1])
== int(adh.id())
and str(machine.proprio) == ("%s %s"
% (adh.prenom(), adh.nom()))):
valid.append((machine, fichier[28:44]))
except:
pass
# Pas de machines !
if not valid:
arg = u'--title "Recherche" '
arg += u'--msgbox "Aucun résultat.\n\n\n" 0 0'
dialog(arg)
continue
if len(valid) == 1:
# Une seule réponse
choix = valid[0]
else:
# Il faut choisir
while 1:
os.system('clear')
choix = None
print "Plusieurs réponses correspondant à votre requête ont été trouvées :"
data = []
for (machine, date) in valid:
data.append([machine.id(), str(machine.__class__)[-4:], machine.nom().split('.', 1)[0], date])
print affich_tools.tableau(data,
titre=[u'mid', u'Type', u'Nom de machine', u'Date destruction'],
largeur=[5, 4, '*', 16],
alignement=['d', 'c', 'c', 'c'])
i = affich_tools.prompt(u'Votre choix ? (0 pour annuler) mid =')
if i == '0': break
for (m, d) in valid:
if m.id() == i:
choix = (m, d)
break
if not choix:
# Redemande le choix
print 'Choix invalide'
continue
if choix: break
if not choix:
# Retour à l'interface de recherche
continue
(oldmachine, date) = choix
if str(machine.__class__)[-4:] == 'Fixe':
machine = MachineFixe(adh)
elif str(machine.__class__)[-4:] == 'Wifi':
machine = MachineWifi(adh)
else:
# On va quand même créer une machine ...
machine = MachineFixe(adh)
err = ""
# On remet le nom
try: machine.nom(str(oldmachine.nom()))
except ValueError, c: err += c.args[0] + '\n'
except EnvironmentError, c: err += c.args[0] + '\n'
# On remet la MAC
try: machine.mac(str(oldmachine.mac()))
except ValueError, c:
if len(c.args)>1 and c.args[1] == 1 and isadm:
# Mac en double
arg = u'--title "Adresse MAC" '
arg += u'--yesno "L\'adresse MAC existe déjà, continuer ? \n" 0 0'
no, res = dialog(arg)
if not no:
try: machine.mac(str(oldmachine.mac()), 1)
except ValueError, c: err += c.args[0] + '\n'
except EnvironmentError, c: err += c.args[0] + '\n'
elif len(c.args)>1 and c.args[1] == 3 and isadm:
# Mac douteuse
arg = u'--title "Adresse MAC" '
arg += u'--yesno "L\'adresse MAC ne correspond à aucun constructeur, continuer ? \n" 0 0'
no, res = dialog(arg)
if not no:
try: machine.mac(str(oldmachine.mac()), 1)
except ValueError, c: err += c.args[0] + '\n'
except EnvironmentError, c: err += c.args[0] + '\n'
else:
err += c.args[0] + '\n'
except EnvironmentError, c: err += c.args[0] + '\n'
# On met une IP automatique
try: machine.ip('<automatique>')
except ValueError, c: err += c.args[0] + '\n'
except EnvironmentError, c: err += c.args[0] + '\n'
except RuntimeError, c: err += c.args[0] + '\n'
# Des erreurs ?
if err:
arg = u'--title "Paramètres machine" '
arg += u'--msgbox "%s\n\n" 0 0' % err
dialog(arg)
set_machine(machine)
continue
if __name__ == '__main__':
if len(sys.argv) < 2:
menu_principal()