Merge lp:~freyes/charm-helpers/lp1660630 into lp:charm-helpers

Proposed by Felipe Reyes
Status: Merged
Merged at revision: 692
Proposed branch: lp:~freyes/charm-helpers/lp1660630
Merge into: lp:charm-helpers
Diff against target: 277 lines (+73/-18)
10 files modified
charmhelpers/contrib/database/mysql.py (+5/-1)
charmhelpers/contrib/hardening/templating.py (+5/-1)
charmhelpers/contrib/mellanox/infiniband.py (+6/-1)
charmhelpers/contrib/network/ip.py (+16/-4)
charmhelpers/contrib/openstack/context.py (+4/-1)
charmhelpers/contrib/openstack/templating.py (+8/-2)
charmhelpers/contrib/python/packages.py (+9/-2)
tests/contrib/network/test_ip.py (+9/-2)
tests/contrib/openstack/test_os_templating.py (+4/-1)
tests/contrib/python/test_packages.py (+7/-3)
To merge this branch: bzr merge lp:~freyes/charm-helpers/lp1660630
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Review via email: mp+317419@code.launchpad.net

Description of the change

Verify python version to decide which package to install

Check using six.PY2 to decide if python-foo or python3-foo package should be installed after an ImportError exception.

To post a comment you must log in.
Revision history for this message
Stuart Bishop (stub) wrote :

Thanks for this.

I think this is changing quite a lot of untested codepaths, but that is the way of contrib.

Lint free and tests all pass.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/database/mysql.py'
2--- charmhelpers/contrib/database/mysql.py 2016-07-06 14:41:05 +0000
3+++ charmhelpers/contrib/database/mysql.py 2017-02-16 03:08:57 +0000
4@@ -17,6 +17,7 @@
5 import platform
6 import os
7 import glob
8+import six
9
10 # from string import upper
11
12@@ -50,7 +51,10 @@
13 import MySQLdb
14 except ImportError:
15 apt_update(fatal=True)
16- apt_install(filter_installed_packages(['python-mysqldb']), fatal=True)
17+ if six.PY2:
18+ apt_install(filter_installed_packages(['python-mysqldb']), fatal=True)
19+ else:
20+ apt_install(filter_installed_packages(['python3-mysqldb']), fatal=True)
21 import MySQLdb
22
23
24
25=== modified file 'charmhelpers/contrib/hardening/templating.py'
26--- charmhelpers/contrib/hardening/templating.py 2016-07-06 14:41:05 +0000
27+++ charmhelpers/contrib/hardening/templating.py 2017-02-16 03:08:57 +0000
28@@ -13,6 +13,7 @@
29 # limitations under the License.
30
31 import os
32+import six
33
34 from charmhelpers.core.hookenv import (
35 log,
36@@ -26,7 +27,10 @@
37 from charmhelpers.fetch import apt_install
38 from charmhelpers.fetch import apt_update
39 apt_update(fatal=True)
40- apt_install('python-jinja2', fatal=True)
41+ if six.PY2:
42+ apt_install('python-jinja2', fatal=True)
43+ else:
44+ apt_install('python3-jinja2', fatal=True)
45 from jinja2 import FileSystemLoader, Environment
46
47
48
49=== modified file 'charmhelpers/contrib/mellanox/infiniband.py'
50--- charmhelpers/contrib/mellanox/infiniband.py 2016-07-06 14:41:05 +0000
51+++ charmhelpers/contrib/mellanox/infiniband.py 2017-02-16 03:08:57 +0000
52@@ -17,6 +17,8 @@
53
54 __author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
55
56+import six
57+
58 from charmhelpers.fetch import (
59 apt_install,
60 apt_update,
61@@ -30,7 +32,10 @@
62 try:
63 from netifaces import interfaces as network_interfaces
64 except ImportError:
65- apt_install('python-netifaces')
66+ if six.PY2:
67+ apt_install('python-netifaces')
68+ else:
69+ apt_install('python3-netifaces')
70 from netifaces import interfaces as network_interfaces
71
72 import os
73
74=== modified file 'charmhelpers/contrib/network/ip.py'
75--- charmhelpers/contrib/network/ip.py 2017-01-25 17:13:37 +0000
76+++ charmhelpers/contrib/network/ip.py 2017-02-16 03:08:57 +0000
77@@ -31,14 +31,20 @@
78 import netifaces
79 except ImportError:
80 apt_update(fatal=True)
81- apt_install('python-netifaces', fatal=True)
82+ if six.PY2:
83+ apt_install('python-netifaces', fatal=True)
84+ else:
85+ apt_install('python3-netifaces', fatal=True)
86 import netifaces
87
88 try:
89 import netaddr
90 except ImportError:
91 apt_update(fatal=True)
92- apt_install('python-netaddr', fatal=True)
93+ if six.PY2:
94+ apt_install('python-netaddr', fatal=True)
95+ else:
96+ apt_install('python3-netaddr', fatal=True)
97 import netaddr
98
99
100@@ -414,7 +420,10 @@
101 try:
102 import dns.resolver
103 except ImportError:
104- apt_install('python-dnspython', fatal=True)
105+ if six.PY2:
106+ apt_install('python-dnspython', fatal=True)
107+ else:
108+ apt_install('python3-dnspython', fatal=True)
109 import dns.resolver
110
111 if isinstance(address, dns.name.Name):
112@@ -462,7 +471,10 @@
113 try:
114 import dns.reversename
115 except ImportError:
116- apt_install("python-dnspython", fatal=True)
117+ if six.PY2:
118+ apt_install("python-dnspython", fatal=True)
119+ else:
120+ apt_install("python3-dnspython", fatal=True)
121 import dns.reversename
122
123 rev = dns.reversename.from_address(address)
124
125=== modified file 'charmhelpers/contrib/openstack/context.py'
126--- charmhelpers/contrib/openstack/context.py 2016-12-14 17:48:32 +0000
127+++ charmhelpers/contrib/openstack/context.py 2017-02-16 03:08:57 +0000
128@@ -100,7 +100,10 @@
129 try:
130 import psutil
131 except ImportError:
132- apt_install('python-psutil', fatal=True)
133+ if six.PY2:
134+ apt_install('python-psutil', fatal=True)
135+ else:
136+ apt_install('python3-psutil', fatal=True)
137 import psutil
138
139 CA_CERT_PATH = '/usr/local/share/ca-certificates/keystone_juju_ca_cert.crt'
140
141=== modified file 'charmhelpers/contrib/openstack/templating.py'
142--- charmhelpers/contrib/openstack/templating.py 2016-07-06 14:41:05 +0000
143+++ charmhelpers/contrib/openstack/templating.py 2017-02-16 03:08:57 +0000
144@@ -28,7 +28,10 @@
145 from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions
146 except ImportError:
147 apt_update(fatal=True)
148- apt_install('python-jinja2', fatal=True)
149+ if six.PY2:
150+ apt_install('python-jinja2', fatal=True)
151+ else:
152+ apt_install('python3-jinja2', fatal=True)
153 from jinja2 import FileSystemLoader, ChoiceLoader, Environment, exceptions
154
155
156@@ -207,7 +210,10 @@
157 # if this code is running, the object is created pre-install hook.
158 # jinja2 shouldn't get touched until the module is reloaded on next
159 # hook execution, with proper jinja2 bits successfully imported.
160- apt_install('python-jinja2')
161+ if six.PY2:
162+ apt_install('python-jinja2')
163+ else:
164+ apt_install('python3-jinja2')
165
166 def register(self, config_file, contexts):
167 """
168
169=== modified file 'charmhelpers/contrib/python/packages.py'
170--- charmhelpers/contrib/python/packages.py 2016-07-06 14:41:05 +0000
171+++ charmhelpers/contrib/python/packages.py 2017-02-16 03:08:57 +0000
172@@ -16,6 +16,7 @@
173 # limitations under the License.
174
175 import os
176+import six
177 import subprocess
178 import sys
179
180@@ -39,7 +40,10 @@
181 from pip import main as _pip_execute
182 except ImportError:
183 apt_update()
184- apt_install('python-pip')
185+ if six.PY2:
186+ apt_install('python-pip')
187+ else:
188+ apt_install('python3-pip')
189 from pip import main as _pip_execute
190 _pip_execute(*args, **kwargs)
191 finally:
192@@ -136,7 +140,10 @@
193
194 def pip_create_virtualenv(path=None):
195 """Create an isolated Python environment."""
196- apt_install('python-virtualenv')
197+ if six.PY2:
198+ apt_install('python-virtualenv')
199+ else:
200+ apt_install('python3-virtualenv')
201
202 if path:
203 venv_path = path
204
205=== modified file 'tests/contrib/network/test_ip.py'
206--- tests/contrib/network/test_ip.py 2016-07-18 15:59:52 +0000
207+++ tests/contrib/network/test_ip.py 2017-02-16 03:08:57 +0000
208@@ -641,7 +641,10 @@
209 fake_dns = FakeDNS('5.5.5.5')
210 with patch(builtin_import, side_effect=[ImportError, fake_dns]):
211 nsq = net_ip.ns_query('5.5.5.5')
212- apt_install.assert_called_with('python-dnspython', fatal=True)
213+ if six.PY2:
214+ apt_install.assert_called_with('python-dnspython', fatal=True)
215+ else:
216+ apt_install.assert_called_with('python3-dnspython', fatal=True)
217 self.assertEquals(nsq, '5.5.5.5')
218
219 @patch('charmhelpers.contrib.network.ip.apt_install')
220@@ -708,7 +711,11 @@
221 with patch(builtin_import, side_effect=[ImportError, fake_dns,
222 fake_dns]):
223 hn = net_ip.get_hostname('4.2.2.1')
224- apt_install.assert_called_with('python-dnspython', fatal=True)
225+ if six.PY2:
226+ apt_install.assert_called_with('python-dnspython', fatal=True)
227+ else:
228+ apt_install.assert_called_with('python3-dnspython', fatal=True)
229+
230 self.assertEquals(hn, 'www.ubuntu.com')
231
232 @patch('charmhelpers.contrib.network.ip.socket.gethostbyaddr')
233
234=== modified file 'tests/contrib/openstack/test_os_templating.py'
235--- tests/contrib/openstack/test_os_templating.py 2015-02-11 21:41:57 +0000
236+++ tests/contrib/openstack/test_os_templating.py 2017-02-16 03:08:57 +0000
237@@ -78,7 +78,10 @@
238 templating.FileSystemLoader = MockFSLoader
239 templating.ChoiceLoader = MockChoiceLoader
240 templating.Environment = MagicMock
241- apt.assert_called_with('python-jinja2')
242+ if six.PY2:
243+ apt.assert_called_with('python-jinja2')
244+ else:
245+ apt.assert_called_with('python3-jinja2')
246
247 def test_create_renderer_invalid_templates_dir(self):
248 '''Ensure OSConfigRenderer checks templates_dir'''
249
250=== modified file 'tests/contrib/python/test_packages.py'
251--- tests/contrib/python/test_packages.py 2015-10-28 21:59:47 +0000
252+++ tests/contrib/python/test_packages.py 2017-02-16 03:08:57 +0000
253@@ -1,11 +1,12 @@
254 #!/usr/bin/env python
255 # coding: utf-8
256
257+import mock
258+import six
259+
260 from unittest import TestCase
261 from charmhelpers.contrib.python import packages
262
263-import mock
264-
265 __author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
266
267 TO_PATCH = [
268@@ -169,5 +170,8 @@
269 """
270 join.return_value = 'joined-path'
271 packages.pip_create_virtualenv()
272- self.apt_install.assert_called_with('python-virtualenv')
273+ if six.PY2:
274+ self.apt_install.assert_called_with('python-virtualenv')
275+ else:
276+ self.apt_install.assert_called_with('python3-virtualenv')
277 check_call.assert_called_with(['virtualenv', 'joined-path'])

Subscribers

People subscribed via source and target branches