Fix up the copyright notices.
[git-dm.git] / ConfigFile.py
blobe5e1cf5ebd9ad8b900472d75180011cf0495ea6b
2 # Stuff for dealing with configuration files.
5 # This code is part of the LWN git data miner.
7 # Copyright 2007-8 LWN.net
8 # Copyright 2007-8 Jonathan Corbet <corbet@lwn.net>
10 # This file may be distributed under the terms of the GNU General
11 # Public License, version 2.
13 import sys, re, datetime
14 import database
17 # Read a line and strip out junk.
19 def ReadConfigLine (file):
20 line = file.readline ()
21 if not line:
22 return None
23 line = line.split('#')[0] # Get rid of any comments
24 line = line.strip () # and extra white space
25 if len (line) == 0: # we got rid of everything
26 return ReadConfigLine (file)
27 return line
30 # Give up and die.
32 def croak (message):
33 sys.stderr.write (message + '\n')
34 sys.exit (1)
37 # Read a list of email aliases.
39 def ReadEmailAliases (name):
40 try:
41 file = open (name, 'r')
42 except IOError:
43 croak ('Unable to open email alias file %s' % (name))
44 line = ReadConfigLine (file)
45 while line:
46 sline = line.split ()
47 if len (sline) != 2:
48 croak ('Funky email alias line "%s"' % (line))
49 if sline[0].index ('@') <= 0 or sline[1].index ('@') <= 0:
50 croak ('Non-addresses in email alias "%s"' % (line))
51 database.AddEmailAlias (sline[0], sline[1])
52 line = ReadConfigLine (file)
53 file.close ()
56 # The Email/Employer map
58 EMMpat = re.compile (r'^([^\s]+)\s+([^<]+)\s*(<\s*(\d+-\d+-\d+)\s*)?$')
60 def ReadEmailEmployers (name):
61 try:
62 file = open (name, 'r')
63 except IOError:
64 croak ('Unable to open email/employer file %s' % (name))
65 line = ReadConfigLine (file)
66 while line:
67 m = EMMpat.match (line)
68 if not m:
69 croak ('Funky email/employer line "%s"' % (line))
70 email = m.group (1)
71 company = m.group (2).strip ()
72 enddate = ParseDate (m.group (4))
73 database.AddEmailEmployerMapping (email, company, enddate)
74 line = ReadConfigLine (file)
75 file.close ()
77 def ParseDate (cdate):
78 if not cdate:
79 return None
80 sdate = cdate.split ('-')
81 return datetime.date (int (sdate[0]), int (sdate[1]), int (sdate[2]))
84 def ReadGroupMap (fname, employer):
85 try:
86 file = open (fname, 'r')
87 except IOError:
88 croak ('Unable to open group map file %s' % (fname))
89 line = ReadConfigLine (file)
90 while line:
91 database.AddEmailEmployerMapping (line, employer)
92 line = ReadConfigLine (file)
93 file.close ()
96 # Read an overall config file.
98 def ConfigFile (name):
99 try:
100 file = open (name, 'r')
101 except IOError:
102 croak ('Unable to open config file %s' % (name))
103 line = ReadConfigLine (file)
104 while line:
105 sline = line.split ()
106 if len (sline) < 2:
107 croak ('Funky config line: "%s"' % (line))
108 if sline[0] == 'EmailAliases':
109 ReadEmailAliases (sline[1])
110 elif sline[0] == 'EmailMap':
111 ReadEmailEmployers (sline[1])
112 elif sline[0] == 'GroupMap':
113 if len (sline) != 3:
114 croak ('Funky group map line "%s"' % (line))
115 ReadGroupMap (sline[1], sline[2])
116 else:
117 croak ('Unrecognized config line: "%s"' % (line))
118 line = ReadConfigLine (file)