Added proper copyright notice
[mirror/userdir-ldap.git] / ud-xearth
1 #!/usr/bin/env python
2 # -*- mode: python -*-
3
4 #   Copyright (c) 1999-2000  Jason Gunthorpe <jgg@debian.org>
5 #   Copyright (c) 2004       Joey Schulze <joey@debian.org>
6 #
7 #   This program is free software; you can redistribute it and/or modify
8 #   it under the terms of the GNU General Public License as published by
9 #   the Free Software Foundation; either version 2 of the License, or
10 #   (at your option) any later version.
11 #
12 #   This program is distributed in the hope that it will be useful,
13 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #   GNU General Public License for more details.
16 #
17 #   You should have received a copy of the GNU General Public License
18 #   along with this program; if not, write to the Free Software
19 #   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 # Generate an xearth database from the LDAP entries
22 # LDAP entires for lat/long can be in one of 3 different formats
23 #    1) Decimal Degrees
24 #        +-DDD.DDDDDDDDDDDDDDD
25 #    2) Degrees Minutes (DGM), common output from GPS units
26 #        +-DDDMM.MMMMMMMMMMMMM
27 #    3) Degrees Minutes Seconds (DGMS)
28 #        +-DDDMMSS.SSSSSSSSSSS
29 # Decimal Degrees is the most basic format, but to have good accuracy it
30 # needs a large number of decimals. The other formats are all derived from it:
31 #  DGM -> DD   DDD + (MM.MMMMMMMM)/60
32 #  DGMS -> DD  DDD + (MM + (SS.SSSSSS)/60)/60
33 # For Latitude + is North, for Longitude + is East
34
35 import string, re, time, ldap, getopt, sys, pwd, os, posix;
36 from userdir_ldap import *;
37
38 Anon = 0;
39
40 # Main program starts here
41 User = pwd.getpwuid(posix.getuid())[0];
42 BindUser = User;
43 (options, arguments) = getopt.getopt(sys.argv[1:], "au:")
44 for (switch, val) in options:
45    if (switch == '-u'):
46       User = val;
47    if (switch == '-a'):
48       Anon = 1;
49
50 # Connect to the ldap server
51 l = passwdAccessLDAP(LDAPServer, BaseDn, User)
52
53 Attrs = l.search_s(BaseDn,ldap.SCOPE_ONELEVEL,"latitude=*",\
54          ["uid","cn","mn","sn","latitude","longitude"]);
55
56 Attrs.sort();
57
58 print "Markers file will be written to markers.dat,",
59 sys.stdout.flush();
60 F = open("markers.dat","w");
61 Count = 0;
62 Failed = 0;
63 for x in Attrs:
64    if x[1].has_key("latitude") == 0 or x[1].has_key("longitude") == 0:
65       continue;
66    Count = Count + 1;
67    try:
68       if Anon != 0:
69          F.write("%8s %8s \"\"\n"%(DecDegree(GetAttr(x,"latitude"),Anon),DecDegree(GetAttr(x,"longitude"),Anon)));
70       else:
71          F.write("%16s %16s \"%s\" \t# %s\n"%(DecDegree(GetAttr(x,"latitude"),Anon),DecDegree(GetAttr(x,"longitude"),Anon),GetAttr(x,"uid"),EmailAddress(x)));
72    except:
73       Failed = Failed + 1;
74       if Anon == 0:
75          F.write("# Failed %s => %s: %s\n" %(x[0],sys.exc_type,sys.exc_value));
76       else:
77          F.write("# Failed => %s: %s\n" %(sys.exc_type,sys.exc_value));
78 F.close();
79 print Count,"entries,",Failed,"failures.";