4f1773bbfe8733e992be4bd346debb09bee7d363
[mirror/userdir-ldap.git] / ud-xearth
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3 # Generate an xearth database from the LDAP entries
4 # LDAP entires for lat/long can be in one of 3 different formats
5 #    1) Decimal Degrees
6 #        +-DDD.DDDDDDDDDDDDDDD
7 #    2) Degrees Minutes (DGM), common output from GPS units
8 #        +-DDDMM.MMMMMMMMMMMMM
9 #    3) Degrees Minutes Seconds (DGMS)
10 #        +-DDDMMSS.SSSSSSSSSSS
11 # Decimal Degrees is the most basic format, but to have good accuracy it
12 # needs a large number of decimals. The other formats are all derived from it:
13 #  DGM -> DD   DDD + (MM.MMMMMMMM)/60
14 #  DGMS -> DD  DDD + (MM + (SS.SSSSSS)/60)/60
15 # For Latitude + is North, for Longitude + is East
16
17 import string, re, time, ldap, getopt, sys, pwd, posix;
18 from userdir_ldap import *;
19
20 Anon = 0;
21
22 # Main program starts here
23 User = pwd.getpwuid(posix.getuid())[0];
24 BindUser = User;
25 (options, arguments) = getopt.getopt(sys.argv[1:], "au:")
26 for (switch, val) in options:
27    if (switch == '-u'):
28       User = val;
29    if (switch == '-a'):
30       Anon = 1;
31
32 # Connect to the ldap server
33 l = ldap.open(LDAPServer);
34 print "Accessing LDAP directory as '" + User + "'";
35 Password = getpass(User + "'s password: ");
36 UserDn = "uid=" + User + "," + BaseDn;
37 l.simple_bind_s(UserDn,Password);
38
39 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"latitude=*",\
40          ["uid","cn","mn","sn","latitude","longitude"]);
41
42 Attrs.sort();
43
44 print "Markers file will be written to markers.dat,",
45 sys.stdout.flush();
46 F = open("markers.dat","w");
47 Count = 0;
48 Failed = 0;
49 for x in Attrs:
50    if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
51       continue;
52    Count = Count + 1;
53    try:
54       if Anon != 0:
55          F.write("%8s %8s \"\"\n"%(DecDegree(GetAttr(x,"latitude"),Anon),DecDegree(GetAttr(x,"longitude"),Anon)));
56       else:
57          F.write("%16s %16s \"%s\" \t# %s\n"%(DecDegree(GetAttr(x,"latitude"),Anon),DecDegree(GetAttr(x,"longitude"),Anon),GetAttr(x,"uid"),EmailAddress(x)));
58    except:
59       Failed = Failed + 1;
60       if Anon == 0:
61          F.write("# Failed %s => %s: %s\n" %(x[0],sys.exc_type,sys.exc_value));
62       else:
63          F.write("# Failed => %s: %s\n" %(sys.exc_type,sys.exc_value));
64 F.close();
65 print Count,"entries,",Failed,"failures.";