Merge lp:~ajkavanagh/charm-helpers/fix-path-mangling-lp1540299 into lp:charm-helpers

Proposed by Alex Kavanagh
Status: Merged
Merged at revision: 526
Proposed branch: lp:~ajkavanagh/charm-helpers/fix-path-mangling-lp1540299
Merge into: lp:charm-helpers
Diff against target: 46 lines (+22/-7)
1 file modified
charmhelpers/contrib/python/packages.py (+22/-7)
To merge this branch: bzr merge lp:~ajkavanagh/charm-helpers/fix-path-mangling-lp1540299
Reviewer Review Type Date Requested Status
Liam Young (community) Approve
Review via email: mp+284737@code.launchpad.net

Description of the change

Mod to charmhelpers.contrib.python.packages to stop importing pip from adding wheels to the path when it is included as an import in another file. This is to prevent the side-effect of unintentionally including an updated module over an expected system package. The pip_execute() command still causes the path/wheels to be included for the duration of the pip_execute() command only (the path is restored after the pip_execute()) finishes.

To post a comment you must log in.
Revision history for this message
Liam Young (gnuoy) wrote :

Approved, thanks for the fix

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'charmhelpers/contrib/python/packages.py'
2--- charmhelpers/contrib/python/packages.py 2015-11-03 19:40:58 +0000
3+++ charmhelpers/contrib/python/packages.py 2016-02-02 13:51:49 +0000
4@@ -19,20 +19,35 @@
5
6 import os
7 import subprocess
8+import sys
9
10 from charmhelpers.fetch import apt_install, apt_update
11 from charmhelpers.core.hookenv import charm_dir, log
12
13-try:
14- from pip import main as pip_execute
15-except ImportError:
16- apt_update()
17- apt_install('python-pip')
18- from pip import main as pip_execute
19-
20 __author__ = "Jorge Niedbalski <jorge.niedbalski@canonical.com>"
21
22
23+def pip_execute(*args, **kwargs):
24+ """Overriden pip_execute() to stop sys.path being changed.
25+
26+ The act of importing main from the pip module seems to cause add wheels
27+ from the /usr/share/python-wheels which are installed by various tools.
28+ This function ensures that sys.path remains the same after the call is
29+ executed.
30+ """
31+ try:
32+ _path = sys.path
33+ try:
34+ from pip import main as _pip_execute
35+ except ImportError:
36+ apt_update()
37+ apt_install('python-pip')
38+ from pip import main as _pip_execute
39+ _pip_execute(*args, **kwargs)
40+ finally:
41+ sys.path = _path
42+
43+
44 def parse_options(given, available):
45 """Given a set of options, check if available"""
46 for key, value in sorted(given.items()):

Subscribers

People subscribed via source and target branches