source: trunk/aeres/scripts/ldap_biblio.py @ 325

Last change on this file since 325 was 325, checked in by pinsard, 11 years ago

improvments of doc + some doctest features

  • Property svn:keywords set to Id URL
File size: 6.3 KB
Line 
1#! /usr/bin/env python
2# -*- coding: iso-8859-15 -*-
3
4"""
5
6==============
7ldap_biblio.py
8==============
9
10SYNOPSIS
11========
12
13::
14 
15  ldap_biblio.py
16
17DESCRIPTION
18===========
19
20extract information from LDAP
21
22.. only:: man
23
24    Figure is visible on PDF and HTML documents only.
25
26    .. only:: html or latex
27
28       .. graphviz::
29
30          digraph ldap_biblio {
31                 ldaphost [shape=diamond,
32                     fontname=Courier,
33                     label='{ldap host}'
34
35                 ldap_biblio [shape=box,
36                     fontname=Courier,
37                     color=blue,
38                     URL="http://forge.ipsl.jussieu.fr/pulsation/browser/trunk/aeres/scripts/ldap_biblio.py",
39                     label="${PROJECT}/aeres/scripts/ldap_biblio.py"];
40
41                 {ldaphost} -> {ldap_biblio} -> {++}
42             }
43
44SEE ALSO
45========
46
47.. index:: LDAP
48
49EXAMPLES
50========
51
52::
53
54 ldap_biblio.py
55
56TIPS
57====
58
59To install ldap for python on mac lion::
60
61 sudo pip install --upgrade python-ldap==2.3.13
62
63see troubles with newer release in
64http://stackoverflow.com/questions/6475118/python-ldap-os-x-10-6-and-python-2-6
65
66TODO
67====
68
69make it work on halios (pb version python-ldap)
70
71server en argument
72
73documentation
74
75pas de diactrique dans ldap
76
77EVOLUTIONS
78==========
79
80$Id$
81
82$URL$
83
84- fplod 20120405
85
86  * reprise dans le cadre de superbib aeres
87
88- fplod 20111109T103458Z cratos (Linux)
89
90  * reprise
91
92    voici tous les champs::
93
94    ["cn: ['Adel Ammar (S. Thiria)']",
95     "objectClass: ['inetLocalMailRecipient', 'person', 'organizationalPerson', 'inetOrgPerson', 'loceanPersonne', 'posixAccount', 'top']",
96     "uidNumber: ['36310']",
97     "street: ['LOCEAN']",
98     "uid: ['aalod']",
99     "title: ['http://www.locean-ipsl.upmc.fr/photo.jpg']",
100     "mailLocalAddress: ['aalod@locean-ipsl.upmc.fr']",
101     "postalCode: ['75252 PARIS Cedex 05']",
102     "mail: ['aalod@locean-ipsl.upmc.fr', 'aalod@lodyc.jussieu.fr', 'Ammar.Adel@locean-ipsl.upmc.fr', 'Ammar.Adel@lodyc.jussieu.fr', 'Ammar@lodyc.jussieu.fr', 'Ammar@locean-ipsl.upmc.fr', 'Adel.Ammar@locean-ipsl.upmc.fr', 'Adel.Ammar@lodyc.jussieu.fr']",
103      "postalAddress: ['4 place Jussieu']",
104      "description: ['S. Thiria']",
105      "loginShell: ['/bin/tcsh']",
106      "gidNumber: ['1664']",
107      "employeeNumber: ['44']",
108      "mailForwardingAddress: ['Adel.Ammar@locean-ipsl.upmc.fr']",
109      "loceanAnnuaire: ['44']",
110      "displayName: ['Adel.Ammar']",
111      "roomNumber: ['Bureau XXX, Tour 45']",
112      "loceanPhotoUrl: ['http://www.locean-ipsl.upmc.fr/photo.jpg']",
113      "carLicense: ['Thiria']",
114      "mailHost: ['locean-ipsl.upmc.fr']",
115      "gecos: ['Adel Ammar (S. Thiria)']",
116      "sn: ['Ammar']",
117      "homeDirectory: ['/usr/home/aalod']",
118      "givenName: ['Adel']",
119      "uid: ['abelod']", "objectClass: ['posixAccount', 'person', 'organizationalPerson', 'inetOrgPerson', 'loceanPersonne', 'top', 'inetLocalMailRecipient']", "uidNumber: ['36337']", "street: ['LOCEAN']",
120   
121- fplod 20110617T080312Z cratos.locean-ipsl.upmc.fr (Linux)
122
123  * no more key error
124
125- fplod 20110616T144623Z cratos.locean-ipsl.upmc.fr (Linux)
126
127  * creation
128    cf. http://www.ibm.com/developerworks/aix/library/au-ldap_crud/
129    cf. http://www.packtpub.com/article/python-ldap-applications-ldap-opearations
130
131"""
132
133import sys
134import os
135from os.path import dirname, basename
136import glob
137from optparse import OptionParser
138
139import ldap
140
141import itertools
142
143ldap_host = 'nestor.locean-ipsl.upmc.fr'
144def get_option_parser ():
145    """parse CLI arguments
146
147           :returns: parser
148          :rtype: :class:`optparse.OptionParser`
149    """
150
151    parser = OptionParser('%prog [--verbose ]')
152    parser.add_option ('-V', '--verbose',
153        help='produce verbose output',
154        action='store_true',
155        default=False,
156        dest='is_verbose')
157
158    return parser
159
160def readldap(ildap, baseDN, searchScope, searchFilter, is_verbose):
161    """
162    read LPAP
163
164    return people dictionnary sn: uid: givenName:  mailForwardingAddress: uidNumber
165    """
166
167    retrieveAttributes = ['uid', 'sn', 'givenName', 'mailForwardingAddress', 'uidNumber']
168    keys_for_all = []
169    values_for_all = [] 
170    ldap_result_id = ildap.search_s(baseDN, searchScope, searchFilter, retrieveAttributes)
171    for ldap_result_one in ldap_result_id:
172        if retrieveAttributes:
173            values_for_one = []
174            attrib_dict = ldap_result_one[1]
175            keys_for_one = attrib_dict.keys()
176            for key in attrib_dict.keys():
177                values_for_one.append(attrib_dict[key][0])
178            keys_for_all.append(attrib_dict['uid'][0])
179            values_for_all.append(dict(itertools.izip(keys_for_one, values_for_one)))
180
181            if is_verbose == True:
182                # pour voir la derniere personne ajoutée
183                print values_for_all[-1]
184
185    # populate dictonnary
186    people = dict(itertools.izip(keys_for_all, values_for_all))
187
188    if is_verbose == True:
189        print attrib_dict.keys()
190        # vu le 20120427 avec retrieveAttributes = ['*']
191        # ['uid', 'objectClass', 'uidNumber', 'sambaAcctFlags', 'street', 'cn', 'employeeType', 'facsimileTelephoneNumber', 'mailLocalAddress', 'sambaPwdMustChange', 'postalCode', 'mail', 'postalAddress', 'departmentNumber', 'loginShell', 'gidNumber', 'mailForwardingAddress', 'telephoneNumber', 'loceanAnnuaire', 'displayName', 'roomNumber', 'loceanPhotoUrl', 'carLicense', 'sambaSID', 'mailHost', 'sn', 'homeDirectory', 'givenName', 'loceanFinLogin']
192
193        #print people.keys()
194        #print people['fplod']
195        #print people['fplod'].values()
196
197    return people
198
199def ldap_biblio():
200    """main
201    """
202
203    try:
204        parser = get_option_parser ()
205        (fromcli, args) = parser.parse_args()
206    except IOError, msg:
207        parser.error(str(msg))
208
209    is_verbose = fromcli.is_verbose
210
211    print ldap.__version__
212
213    #++if ldap.__version__ == '2.3.13'
214    #++l = ldap.initialize(ldap_host)
215    #++else
216    l = ldap.open(ldap_host)
217
218    l.protocol_version = ldap.VERSION3
219   
220    baseDN = 'ou=utilisateurs ,dc=locean-ipsl,dc=upmc,dc=fr'
221    searchScope = ldap.SCOPE_SUBTREE
222
223    searchFilter = 'uid=*'
224
225    people_ldap = readldap(l, baseDN, searchScope, searchFilter, is_verbose)
226
227    if is_verbose == True:
228        print (' result looking for key fplod: %s ' % people_ldap['fplod'])
229
230    #++ find in dict
231
232
233# Run main, if called from the command line
234if __name__ == '__main__':
235    ldap_biblio()
Note: See TracBrowser for help on using the repository browser.