diff --git a/charmtools/build/builder.py b/charmtools/build/builder.py index 9eaddbc..2020127 100755 --- a/charmtools/build/builder.py +++ b/charmtools/build/builder.py @@ -786,7 +786,8 @@ def workaround_charmcraft_maybe_ensure_build_packages(self): or os.environ.get('CHARMCRAFT_PART_NAME', None))): log.warning('Probably running as root in charmcraft, proactively ' 'installing the `git` and `virtualenv` packages.') - subprocess.run(('apt', '-y', 'install', 'git', 'virtualenv'), check=True, env={}) + subprocess.run(('apt', '-y', 'install', 'git', 'virtualenv'), + check=True, env=utils.host_env()) def generate(self): layers = self.fetch() diff --git a/charmtools/build/tactics.py b/charmtools/build/tactics.py index bca0a36..667fcfc 100644 --- a/charmtools/build/tactics.py +++ b/charmtools/build/tactics.py @@ -1064,24 +1064,14 @@ def __str__(self): return "Building wheelhouse in {}".format(directory) def _get_env(self): - """Get environment appropriate for executing external commands. + """Get environment for executing commands outside snap context. :returns: Dictionary with environment variables :rtype: Dict[str,str] """ if self.use_python_from_snap: return os.environ.copy() - - env = os.environ.copy() - for key in ('PREFIX', 'PYTHONHOME', 'PYTHONPATH', - 'GIT_TEMPLATE_DIR', 'GIT_EXEC_PATH'): - if key in env: - del(env[key]) - env['PATH'] = ':'.join([ - element - for element in env['PATH'].split(':') - if not element.startswith('/snap/charm/')]) - return env + return utils.host_env() def combine(self, existing): "" # suppress inherited doc diff --git a/charmtools/utils.py b/charmtools/utils.py index 3c98da3..10808fa 100644 --- a/charmtools/utils.py +++ b/charmtools/utils.py @@ -633,3 +633,21 @@ def validate_display_name(entity, linter): linter.err('display-name: not in valid format. ' 'Only letters, numbers, dashes, and hyphens are permitted.') return + + +def host_env(): + """Get environment appropriate for executing commands outside snap context. + + :returns: Dictionary with environment variables + :rtype: Dict[str,str] + """ + env = os.environ.copy() + for key in ('PREFIX', 'PYTHONHOME', 'PYTHONPATH', + 'GIT_TEMPLATE_DIR', 'GIT_EXEC_PATH'): + if key in env: + del(env[key]) + env['PATH'] = ':'.join([ + element + for element in env['PATH'].split(':') + if not element.startswith('/snap/charm/')]) + return env diff --git a/tests/test_utils.py b/tests/test_utils.py index 4b9fc56..105c836 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,6 @@ from __future__ import print_function +import unittest from unittest import TestCase from charmtools import utils from six import StringIO @@ -43,3 +44,19 @@ def react(db): self.assertIn("Beta", output) self.assertIn("@when('db.ready'", output) self.assertIn("bar", output) + + @unittest.mock.patch("os.environ") + def test_host_env(self, mock_environ): + mock_environ.copy.return_value = { + 'PREFIX': 'fake-prefix', + 'PYTHONHOME': 'fake-pythonhome', + 'PYTHONPATH': 'fake-pythonpath', + 'GIT_TEMPLATE_DIR': 'fake-git-template-dir', + 'GIT_EXEC_PATH': 'fake-git-exec-path', + 'SOME_OTHER_KEY': 'fake-some-other-key', + 'PATH': '/snap/charm/current/bin:/usr/bin:' + '/snap/charm/current/usr/bin:/bin', + } + self.assertDictEqual( + {'SOME_OTHER_KEY': 'fake-some-other-key', 'PATH': '/usr/bin:/bin'}, + utils.host_env())