blob: 4fb9022f6a78f22e59ed2f1a3246cd85ce0d2982 [file] [log] [blame]
Daniel Dunbar235aa412009-07-18 07:16:15 +00001import buildbot
2import zope
Galina Kistanova2ae61da2012-02-14 00:46:15 +00003import os
4
5from datetime import datetime, timedelta
6from twisted.python import log
Daniel Dunbar235aa412009-07-18 07:16:15 +00007
8class ConfigEmailLookup(buildbot.util.ComparableMixin):
9 """
10 Email lookup implementation which searchs a user specified configuration
11 file to match commit authors to email addresses.
12 """
13
Galina Kistanova2ae61da2012-02-14 00:46:15 +000014 # TODO: Document this class.
15 # Class loads llvm_authors from file and reload if the file was updated.
Daniel Dunbar235aa412009-07-18 07:16:15 +000016
Daniel Dunbarec304232009-11-09 06:08:36 +000017 zope.interface.implements(buildbot.interfaces.IEmailLookup)
18 compare_attrs = ["author_filename", "default_address", "only_addresses"]
19
Galina Kistanova2ae61da2012-02-14 00:46:15 +000020 def __init__(self, author_filename, default_address, only_addresses = None, update_interval=timedelta(hours=1)):
Daniel Dunbar235aa412009-07-18 07:16:15 +000021 from ConfigParser import ConfigParser
22
23 self.author_filename = author_filename
24 self.default_address = default_address
Daniel Dunbarec304232009-11-09 06:08:36 +000025 self.only_addresses = only_addresses
Galina Kistanova2ae61da2012-02-14 00:46:15 +000026 self.update_interval = update_interval
Daniel Dunbar235aa412009-07-18 07:16:15 +000027
28 self.config_parser = ConfigParser()
Galina Kistanova2ae61da2012-02-14 00:46:15 +000029 self.config_parser.read(self.author_filename)
30
31 self.time_checked = datetime.utcnow()
32 self.time_loaded = datetime.utcfromtimestamp(os.path.getmtime(self.author_filename))
Daniel Dunbar235aa412009-07-18 07:16:15 +000033
Daniel Dunbarec304232009-11-09 06:08:36 +000034 if only_addresses:
35 import re
36 self.address_match_p = re.compile(only_addresses).match
37 else:
38 self.address_match_p = lambda addr: True
39
Daniel Dunbar235aa412009-07-18 07:16:15 +000040 def getAddress(self, name):
Galina Kistanova2ae61da2012-02-14 00:46:15 +000041
Galina Kistanovab9032422019-10-17 23:18:02 +000042 # For a multiple types of pollers/schedulers we can get an email address or
43 # a fully qualified name/email in 'name' parameter. Check and return name if it
44 # contains email address.
45 if name is not None and '@' in name:
46 return name
47
Galina Kistanova2ae61da2012-02-14 00:46:15 +000048 try:
49
50 if (datetime.utcnow() - self.time_checked) >= timedelta(minutes=1):
51 self.time_checked = datetime.utcnow()
52 current_mtime = datetime.utcfromtimestamp(os.path.getmtime(self.author_filename))
Galina Kistanova2ae61da2012-02-14 00:46:15 +000053
54 if (current_mtime != self.time_loaded) and ((datetime.utcnow() - current_mtime) >= timedelta(minutes=1)):
55 # Reload the list of authors.
56 self.config_parser.read(self.author_filename)
57 self.time_loaded = current_mtime
58 log.msg('Reloaded file %s (mtime=%s) at %s' % (self.author_filename, self.time_loaded, self.time_checked))
59
60 except:
61 log.msg('Cannot load the %s file.' % self.author_filename)
62 pass
63
Daniel Dunbar235aa412009-07-18 07:16:15 +000064 try:
Daniel Dunbarec304232009-11-09 06:08:36 +000065 email = self.config_parser.get("authors", name)
Daniel Dunbar235aa412009-07-18 07:16:15 +000066 except:
67 return self.default_address
Daniel Dunbarec304232009-11-09 06:08:36 +000068
69 if self.address_match_p(email):
70 return email
71
72 return self.default_address