From 40e59dee855fa7006bc9ce2e8ef7f4132bf8e4c4 Mon Sep 17 00:00:00 2001 From: Nicolas Dandrimont Date: Tue, 31 Aug 2010 08:52:43 +0200 Subject: [PATCH] [annuaires_pg.py] Import initial darcs-hash:20100831065243-ffbb2-82aea8357007fb25a1d373b54a0575a862edf911.gz --- gestion/annuaires_pg.py | 198 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100755 gestion/annuaires_pg.py diff --git a/gestion/annuaires_pg.py b/gestion/annuaires_pg.py new file mode 100755 index 00000000..8dfc100c --- /dev/null +++ b/gestion/annuaires_pg.py @@ -0,0 +1,198 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import psycopg2 + +conn = psycopg2.connect("user=crans dbname=switchs host=pgsql.adm.crans.org") + +# Population de la tâble avec les bâtiments +cur = conn.cursor() + +cur.execute("SELECT DISTINCT batiment FROM prises") +bat_switchs = [i[0] for i in cur.fetchall()] + +def chbre_prises(batiment, chambre = None): + """Correspondance chambre -> prise""" + batiment = batiment.lower() + if chambre: + chambre = chambre.lower() + cur = conn.cursor() + cur.execute("SELECT prise_crans FROM prises WHERE (batiment, chambre) = (%s, %s)", (batiment, chambre)) + try: + return "%03d" % cur.fetchone()[0] + except TypeError: + raise ValueError("Prise inexistante") + else: + cur = conn.cursor() + cur.execute("SELECT chambre, prise_crans FROM prises WHERE batiment = %s", batiment) + ret = {} + for chambre, prise_crans in cur.fetchall(): + ret[chambre] = "%03d" % prise_crans + if not ret: + raise ValueError("Batiment inexistant") + return ret + +def reverse(batiment, prise = None): + """Correspondance prise -> chambre""" + batiment = batiment.lower() + if prise: + cur = conn.cursor() + cur.execute("SELECT chambre FROM prises WHERE (batiment, prise_crans) = (%s, %s)", (batiment, int(prise))) + try: + return [chbre for (chbre,) in cur.fetchall()] + except TypeError: + raise ValueError("Prise inexistante") + else: + cur = conn.cursor() + cur.execute("SELECT chambre, prise_crans FROM prises WHERE batiment = %s", batiment) + ret = {} + for chambre, prise_crans in cur.fetchall(): + try: + ret["%03d" % prise_crans].append(chambre) + except KeyError: + ret["%03d" % prise_crans] = [chambre] + + if not ret: + raise ValueError("Batiment inexistant") + return ret + +def is_crans(batiment, chambre): + """Chambre cablee au Cr@ns ?""" + cur = conn.cursor() + cur.execute("SELECT crans FROM prises WHERE (batiment, chambre) = (%s, %s)", (batiment, chambre)) + return cur.fetchone()[0] + +def crous_to_crans(batiment, chambre): + """Passage d'une chambre de CROUS a Cr@ns""" + if is_crans(batiment, chambre): + return + cur = conn.cursor() + cur.execute("UPDATE prises SET (crans, crous, cablage_effectue) = (TRUE, FALSE, FALSE) WHERE (batiment, chambre) = (%s, %s)", (batiment, chambre)) + conn.commit() + cur.close() + +# Prises d'uplink, de machines du crans / Prises d'utilité CRANS +uplink_prises={ 'a' : +{ 49 : 'uplink->backbone', 50 : 'uplink->bata-1', + 149 : 'uplink->bata-0', 150 : 'uplink->bata-2', + 202 : 'libre-service', + 225 : 'uplink->bata-1', 226 : 'uplink->bata-3', + 325 : 'uplink->bata-2', 326 : 'libre-service' }, +'b' : +{ 48 : 'libre-service', + 49 : 'uplink->backbone', 50 : 'uplink->batb-1', + 149 : 'uplink->batb-0', 150 : 'uplink->batb-3', + 249 : 'libre-service', 250 : 'uplink->batb-3', # 249 morte ?! (olasd 21/01/2010) + 349 : 'uplink->batb-1', 350 : 'uplink->batb-2'}, +'c' : +{ 49 : 'uplink->backbone', 50 : 'uplink->batc-1', + 149 : 'uplink->batc-0', 150 : 'uplink->batc-2', + 225 : 'uplink->batc-1' }, +'g' : +{ 25 : 'libre-service', 26 : 'libre-service', + 27 : 'uplink->backbone', 28 : 'uplink->batg-1', + 149 : 'uplink->batg-4', 150 : 'uplink->batg-2', + 151 : 'uplink->batg-0', # fibre + 249 : 'uplink->batg-1', 250 : 'uplink->batg-3', + 325 : 'uplink->batg-2', 326 : 'libre-service', + 449 : 'uplink->batg-1', 450 : 'uplink->batg-5', + 549 : 'uplink->batg-4', 550 : 'uplink->batg-6', + 649 : 'uplink->batg-5', 650 : 'uplink->batg-5', + 725 : 'uplink->batg-6', 726 : 'libre-service'}, +'h' : +{ 49 : 'uplink->backbone', 50 : 'uplink->bath-1', + 149 : 'uplink->bath-0', 150 : 'uplink->bath-2', + 225 : 'uplink->bath-1' }, +'i' : +{ 49 : 'uplink->backbone', 50 : 'uplink->bati-1', + 149 : 'uplink->bati-0', 150 : 'uplink->bati-2', + 225 : 'uplink->bati-1', 226 : 'libre-service' }, +'j' : +{ 49 : 'uplink->batj-3', 50 : 'libre-service', + 149 : 'uplink->batj-3', 150 : 'libre-service', # XXX: 150 semble morte + 225 : 'uplink->batj-3', 226 : 'libre-service', + 321 : 'uplink->backbone', + 301 : 'uplink->batj-0', 303 : 'uplink->batj-1', + 305 : 'uplink->batj-2', 307 : 'uplink->multiprise', +}, +'m' : +{ + 49 : 'libre-service', 50 : 'uplink->batm-7', + 149 : 'libre-service', 150 : 'uplink->batm-7', + 249 : 'libre-service', 250 : 'uplink->batm-7', + 349 : 'libre-service', 350 : 'uplink->batm-7', + 449 : 'libre-service', 450 : 'uplink->batm-7', + 549 : 'libre-service', 550 : 'uplink->batm-7', + 649 : 'libre-service', 650 : 'uplink->batm-7', + + 724 : 'libre-service', 723 : 'libre-service', + 722 : 'libre-service', + + 721 : 'uplink->backbone', 720 : 'uplink->batm-0', + 719 : 'uplink->batm-1', 718 : 'uplink->batm-2', + 717 : 'uplink->batm-3', 716 : 'uplink->batm-4', + 715 : 'uplink->batm-5', 714 : 'uplink->batm-6', + }, + 'p' : +{349 : 'uplink->batp-2', 350 : 'libre-service', + 249 : 'uplink->batp-1', 247 : 'uplink->batp-3', + 149 : 'uplink->batp-0', 150 : 'uplink->batp-2', + 49 : 'uplink->backbone', 50 : 'uplink->batp-1' } , + 'o' : + { 25 : 'uplink->D-Link(autocom)', 26 : 'libre-service' } , + 'v' : +{ 49 : 'libre-service', 50 : 'libre-service', + 51 : 'uplink->backbone', 52 : 'libre-service', + 149 : 'libre-service', 150 : 'libre-service', + 749 : 'libre-service', 750 : 'libre-service' } +} + +def all_switchs(bat=None): + """Retourne la liste des switchs pour un batiment. + + Si bat est donné, seulement pour le bâtiment demandé, sinon pour + tous les bâtiments. bat peut être une liste aussi. Le backbone n'est + pas pris en compte. La convention est batx-y sauf si y=0 et on a donc + simplement batx""" + def cmp(x,y): + if int(x[5]) < int(y[5]): return 1 + if x[3] < y[3]: return 1 + return -1 + + if bat == None: + bat = bat_switchs + if type(bat) not in [ tuple, list ] : + bat = [bat] + switchs = [] + for b in map(lambda x: x.lower(), bat): + dup = map(lambda x: x[0], reverse(b).keys()) + # dup contient des elements en double + for n in list(dict(zip(dup,[None]*len(dup)))): + switchs.append("bat%s-%s.adm.crans.org" % (b, n)) + switchs.sort(cmp) + return switchs + +# Locaux clubs : lecture dans chbre_prises et ajout des locaux dans les bats non +# manageables +def locaux_clubs() : + """ Retourne le dictionaire des locaux club : {bat: [locaux]} """ + # Corespondance chbre -> nom du local club + locaux_clubs = { 'Bcl0' : 'Kfet' , + 'Bcl1' : 'Med', + 'Pcl0' : 'Bds' , + 'Mcl0' : 'Shape', + 'Mcl1' : 'Krobot', + 'EXT' : 'EXT' } + # Ajout des locaux d'étage A, B et C + for b in 'ABC' : + for i in range(2,7) : + locaux_clubs['%scl%i' % ( b, i)] = '%i@%s' % (i, b) + # Ajout de ceux des H, I et J + for b in 'HIJ' : + for i in range(1,5) : + locaux_clubs['%scl%i' % ( b, i)] = '%i@%s' % (i, b) + # Supression du 2@B et 4@J + locaux_clubs.pop('Bcl2') + locaux_clubs.pop('Jcl4') + + return locaux_clubs