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

Last change on this file since 194 was 194, checked in by pinsard, 12 years ago

big progress on ldap reading

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