Merge lp:~cjwatson/launchpad/geoip2 into lp:launchpad

Proposed by Colin Watson
Status: Merged
Merged at revision: 19031
Proposed branch: lp:~cjwatson/launchpad/geoip2
Merge into: lp:launchpad
Prerequisite: lp:~cjwatson/launchpad/geoip-country
Diff against target: 83 lines (+23/-9)
3 files modified
constraints.txt (+2/-0)
lib/lp/services/geoip/model.py (+20/-9)
setup.py (+1/-0)
To merge this branch: bzr merge lp:~cjwatson/launchpad/geoip2
Reviewer Review Type Date Requested Status
William Grant code Approve
Review via email: mp+371095@code.launchpad.net

Commit message

Add optional geoip2 support.

Description of the change

We don't have a useful way to test this until the relevant data packages gain the right set of database files, but I've run the existing tests with modified configuration to point to a local download of GeoLite2-Country.mmdb and they pass.

Once we're using this, it will give us GeoIP support for IPv6 addresses.

https://code.launchpad.net/~cjwatson/lp-source-dependencies/+git/lp-source-dependencies/+merge/371094 and probably https://code.launchpad.net/~cjwatson/meta-lp-deps/geoip2/+merge/371092 should land first.

To post a comment you must log in.
Revision history for this message
William Grant (wgrant) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'constraints.txt'
--- constraints.txt 2019-07-09 12:32:22 +0000
+++ constraints.txt 2019-08-08 15:23:22 +0000
@@ -262,6 +262,7 @@
262fixtures==3.0.0262fixtures==3.0.0
263FormEncode==1.2.4263FormEncode==1.2.4
264futures==3.2.0264futures==3.2.0
265geoip2==2.9.0
265grokcore.component==1.6266grokcore.component==1.6
266gunicorn==19.8.1267gunicorn==19.8.1
267html5browser==0.0.9268html5browser==0.0.9
@@ -299,6 +300,7 @@
299manuel==1.7.2300manuel==1.7.2
300Markdown==2.3.1301Markdown==2.3.1
301martian==0.11302martian==0.11
303maxminddb==1.4.1
302meliae==0.2.0.final.0304meliae==0.2.0.final.0
303mistune==0.8.3305mistune==0.8.3
304mock==1.0.1306mock==1.0.1
305307
=== modified file 'lib/lp/services/geoip/model.py'
--- lib/lp/services/geoip/model.py 2019-08-08 15:22:41 +0000
+++ lib/lp/services/geoip/model.py 2019-08-08 15:23:22 +0000
@@ -10,6 +10,8 @@
10import os10import os
1111
12import GeoIP as libGeoIP12import GeoIP as libGeoIP
13from geoip2.database import Reader
14from geoip2.errors import AddressNotFoundError
13from zope.component import getUtility15from zope.component import getUtility
14from zope.i18n.interfaces import IUserPreferredLanguages16from zope.i18n.interfaces import IUserPreferredLanguages
15from zope.interface import implementer17from zope.interface import implementer
@@ -38,20 +40,29 @@
38 if not os.path.exists(config.launchpad.geoip_database):40 if not os.path.exists(config.launchpad.geoip_database):
39 raise NoGeoIPDatabaseFound(41 raise NoGeoIPDatabaseFound(
40 "No GeoIP DB found. Please install launchpad-dependencies.")42 "No GeoIP DB found. Please install launchpad-dependencies.")
41 return libGeoIP.open(43 if config.launchpad.geoip_database.endswith('.mmdb'):
42 config.launchpad.geoip_database, libGeoIP.GEOIP_MEMORY_CACHE)44 return Reader(config.launchpad.geoip_database)
45 else:
46 return libGeoIP.open(
47 config.launchpad.geoip_database, libGeoIP.GEOIP_MEMORY_CACHE)
4348
44 def getCountryCodeByAddr(self, ip_address):49 def getCountryCodeByAddr(self, ip_address):
45 """See `IGeoIP`."""50 """See `IGeoIP`."""
46 if not ipaddress_is_global(ip_address):51 if not ipaddress_is_global(ip_address):
47 return None52 return None
48 try:53 if isinstance(self._gi, Reader):
49 return self._gi.country_code_by_addr(ip_address)54 try:
50 except SystemError:55 return self._gi.country(ip_address).country.iso_code
51 # libGeoIP may raise a SystemError if it doesn't find a record for56 except AddressNotFoundError:
52 # some IP addresses (e.g. 255.255.255.255), so we need to catch57 return None
53 # that and return None here.58 else:
54 return None59 try:
60 return self._gi.country_code_by_addr(ip_address)
61 except SystemError:
62 # libGeoIP may raise a SystemError if it doesn't find a
63 # record for some IP addresses (e.g. 255.255.255.255), so we
64 # need to catch that and return None here.
65 return None
5566
56 def getCountryByAddr(self, ip_address):67 def getCountryByAddr(self, ip_address):
57 """See `IGeoIP`."""68 """See `IGeoIP`."""
5869
=== modified file 'setup.py'
--- setup.py 2019-08-08 15:22:41 +0000
+++ setup.py 2019-08-08 15:23:22 +0000
@@ -161,6 +161,7 @@
161 'feedparser',161 'feedparser',
162 'feedvalidator',162 'feedvalidator',
163 'fixtures',163 'fixtures',
164 'geoip2',
164 'gunicorn[gthread]',165 'gunicorn[gthread]',
165 'html5browser',166 'html5browser',
166 'importlib-resources',167 'importlib-resources',