Use the common routine from userdir_ldap.py which asks for the
[mirror/userdir-ldap.git] / ud-arbimport
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3 # This script imports arbitary lists of data. The input is a file with 
4 # the form of:
5 #  uid: <data>
6
7 import string, re, time, ldap, getopt, sys;
8 from userdir_ldap import *;
9
10 # Process options
11 (options, arguments) = getopt.getopt(sys.argv[1:], "u:m:n")
12 for (switch, val) in options:
13    if (switch == '-u'):
14       AdminUser = val
15    elif (switch == '-m'):
16        LoadOverride(val);
17    elif (switch == '-n'):
18        NoAct = 1;
19 if len(arguments) == 0:
20    print "Give the key to assignt to then the file to import";
21    os.exit(0);
22
23 # Main program starts here
24 l = passwdAccessLDAP(LDAPServer, BaseDn, AdminUser)
25
26 List = open(arguments[1],"r");
27 Set = [];
28 User = None;
29 while(1):
30    Line = List.readline();
31    if Line != "":
32       # Glob similar lines
33       Split = re.split("[:\n]",Line);
34       if User == None:
35          User = Split[0];
36       if Split[0] == User:
37          Set.append(string.strip(Split[1]));
38          continue;
39    else:
40       if len(Set) == 0:
41          break;
42    
43    # Generate the command..
44    Rec = [(ldap.MOD_REPLACE,arguments[0],Set[0])];
45    for x in Set[1:]:
46       Rec.append((ldap.MOD_ADD,arguments[0],x))
47
48    Dn = "uid=" + User + "," + BaseDn;
49    try:
50       print Dn,Rec;
51       l.modify_s(Dn,Rec);
52    except:
53       print "Failed",Dn;
54    
55    # Out of data..
56    if Line == "":
57       break;   
58    User = Split[0];
59    Set = [string.strip(Split[1])];