Merge lp:~james-page/charm-helpers/get-upstream-version into lp:charm-helpers

Proposed by James Page
Status: Merged
Merged at revision: 631
Proposed branch: lp:~james-page/charm-helpers/get-upstream-version
Merge into: lp:charm-helpers
Diff against target: 112 lines (+40/-19)
4 files modified
charmhelpers/contrib/openstack/utils.py (+8/-18)
charmhelpers/fetch/__init__.py (+1/-0)
charmhelpers/fetch/ubuntu.py (+20/-0)
tests/fetch/test_fetch.py (+11/-1)
To merge this branch: bzr merge lp:~james-page/charm-helpers/get-upstream-version
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+306250@code.launchpad.net

Description of the change

Add a helper to grab the upstream version of an installed package.

To post a comment you must log in.
632. By James Page

Refactor openstack.utils to use get_upstream_version

Revision history for this message
Liam Young (gnuoy) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'charmhelpers/contrib/openstack/utils.py'
--- charmhelpers/contrib/openstack/utils.py 2016-09-16 09:27:02 +0000
+++ charmhelpers/contrib/openstack/utils.py 2016-09-21 07:37:06 +0000
@@ -81,7 +81,12 @@
81 service_resume,81 service_resume,
82 restart_on_change_helper,82 restart_on_change_helper,
83)83)
84from charmhelpers.fetch import apt_install, apt_cache, install_remote84from charmhelpers.fetch import (
85 apt_install,
86 apt_cache,
87 install_remote,
88 get_upstream_version
89)
85from charmhelpers.contrib.storage.linux.utils import is_block_device, zap_disk90from charmhelpers.contrib.storage.linux.utils import is_block_device, zap_disk
86from charmhelpers.contrib.storage.linux.loopback import ensure_loopback_device91from charmhelpers.contrib.storage.linux.loopback import ensure_loopback_device
87from charmhelpers.contrib.openstack.exceptions import OSContextError92from charmhelpers.contrib.openstack.exceptions import OSContextError
@@ -1894,25 +1899,10 @@
18941899
1895def os_application_version_set(package):1900def os_application_version_set(package):
1896 '''Set version of application for Juju 2.0 and later'''1901 '''Set version of application for Juju 2.0 and later'''
1897 import apt_pkg as apt1902 application_version = get_upstream_version(package)
1898 cache = apt_cache()
1899 application_version = None
1900 application_codename = os_release(package)
1901
1902 try:
1903 pkg = cache[package]
1904 if not pkg.current_ver:
1905 juju_log('Package {} is not currently installed.'.format(package),
1906 DEBUG)
1907 else:
1908 application_version = apt.upstream_version(pkg.current_ver.ver_str)
1909 except:
1910 juju_log('Package {} has no installation candidate.'.format(package),
1911 DEBUG)
1912
1913 # NOTE(jamespage) if not able to figure out package version, fallback to1903 # NOTE(jamespage) if not able to figure out package version, fallback to
1914 # openstack codename version detection.1904 # openstack codename version detection.
1915 if not application_version:1905 if not application_version:
1916 application_version_set(application_codename)1906 application_version_set(os_release(package))
1917 else:1907 else:
1918 application_version_set(application_version)1908 application_version_set(application_version)
19191909
=== modified file 'charmhelpers/fetch/__init__.py'
--- charmhelpers/fetch/__init__.py 2016-08-11 15:47:44 +0000
+++ charmhelpers/fetch/__init__.py 2016-09-21 07:37:06 +0000
@@ -92,6 +92,7 @@
92 apt_mark = fetch.apt_mark92 apt_mark = fetch.apt_mark
93 apt_hold = fetch.apt_hold93 apt_hold = fetch.apt_hold
94 apt_unhold = fetch.apt_unhold94 apt_unhold = fetch.apt_unhold
95 get_upstream_version = fetch.get_upstream_version
95elif __platform__ == "centos":96elif __platform__ == "centos":
96 yum_search = fetch.yum_search97 yum_search = fetch.yum_search
9798
9899
=== modified file 'charmhelpers/fetch/ubuntu.py'
--- charmhelpers/fetch/ubuntu.py 2016-09-16 13:27:28 +0000
+++ charmhelpers/fetch/ubuntu.py 2016-09-21 07:37:06 +0000
@@ -314,3 +314,23 @@
314314
315 else:315 else:
316 subprocess.call(cmd, env=env)316 subprocess.call(cmd, env=env)
317
318
319def get_upstream_version(package):
320 """Determine upstream version based on installed package
321
322 @returns None (if not installed) or the upstream version
323 """
324 import apt_pkg
325 cache = apt_cache()
326 try:
327 pkg = cache[package]
328 except:
329 # the package is unknown to the current apt cache.
330 return None
331
332 if not pkg.current_ver:
333 # package is known, but no version is currently installed.
334 return None
335
336 return apt_pkg.upstream_version(pkg.current_ver.ver_str)
317337
=== modified file 'tests/fetch/test_fetch.py'
--- tests/fetch/test_fetch.py 2016-09-16 13:32:52 +0000
+++ tests/fetch/test_fetch.py 2016-09-21 07:37:06 +0000
@@ -40,7 +40,7 @@
40 raise KeyError40 raise KeyError
41 pkg.name = package41 pkg.name = package
42 if 'current_ver' in FAKE_APT_CACHE[package]:42 if 'current_ver' in FAKE_APT_CACHE[package]:
43 pkg.current_ver = FAKE_APT_CACHE[package]['current_ver']43 pkg.current_ver.ver_str = FAKE_APT_CACHE[package]['current_ver']
44 else:44 else:
45 pkg.current_ver = None45 pkg.current_ver = None
46 return pkg46 return pkg
@@ -1269,3 +1269,13 @@
1269 from charmhelpers.fetch.centos import _run_yum_command1269 from charmhelpers.fetch.centos import _run_yum_command
1270 _run_yum_command(["some", "command"], fatal=True)1270 _run_yum_command(["some", "command"], fatal=True)
1271 self.assertTrue(sleep.called)1271 self.assertTrue(sleep.called)
1272
1273 @patch.object(osplatform, 'get_platform')
1274 @patch('apt_pkg.Cache')
1275 def test_get_upstream_version(self, cache, platform):
1276 platform.return_value = 'ubuntu'
1277 imp.reload(fetch)
1278 cache.side_effect = fake_apt_cache
1279 self.assertEqual(fetch.get_upstream_version('vim'), '7.3.547')
1280 self.assertEqual(fetch.get_upstream_version('emacs'), None)
1281 self.assertEqual(fetch.get_upstream_version('unknown'), None)

Subscribers

People subscribed via source and target branches