Skip to content

Commit

Permalink
Add option to prune snap path when building wheelhouse
Browse files Browse the repository at this point in the history
When running charm-tools from a snap, the default is to use Python
and associated tools from the snap when building a charm's
wheelhouse etc.

This is normally not what you would want as the Python version in
the snap does not necessarily match the Python version of the
target system.

The wheelhouse needs to be built with a Python version compatible
with the Python version of the target system on which the built
charm is to be run on.

Add `--use-python-from-snap` option where the default is 'False'.
This is a change of behavior and mandates a major version bump for
this tool.

Fixes #623
  • Loading branch information
fnordahl committed Sep 16, 2022
1 parent 98f20bf commit 2482f0f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/tox.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ jobs:
tox:
name: Tests
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10']
steps:
- name: Check out code
uses: actions/checkout@v2
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: ${{ matrix.python-version }}
- name: Install Tox
run: pip install tox
- name: Run tests
run: tox
run: tox -e py
5 changes: 5 additions & 0 deletions charmtools/build/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,10 @@ def main(args=None):
help='Same as --binary-wheels but build all the '
'wheels from source code, even if a binary '
'distribution is available on PyPi.')
parser.add_argument('--use-python-from-snap', action='store_true',
default=False,
help='Use Python and associated tools from the snap '
'when building the wheelhouse.')
parser.add_argument('charm', nargs="?", default=".", type=path,
help='Source directory for charm layer to build '
'(default: .)')
Expand All @@ -1205,6 +1209,7 @@ def main(args=None):
WheelhouseTactic.per_layer = build.wheelhouse_per_layer
WheelhouseTactic.binary_build = build.binary_wheels
WheelhouseTactic.binary_build_from_source = build.binary_wheels_from_source
WheelhouseTactic.use_python_from_snap = build.use_python_from_snap

configLogging(build)

Expand Down
25 changes: 23 additions & 2 deletions charmtools/build/tactics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ class WheelhouseTactic(ExactMatch, Tactic):
per_layer = False
binary_build = False
binary_build_from_source = False
use_python_from_snap = False

def __init__(self, *args, **kwargs):
super(WheelhouseTactic, self).__init__(*args, **kwargs)
Expand All @@ -1062,6 +1063,25 @@ def __str__(self):
directory = self.target.directory / 'wheelhouse'
return "Building wheelhouse in {}".format(directory)

def _get_env(self):
"""Get environment appropriate for executing external commands.
: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'):
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

def combine(self, existing):
"" # suppress inherited doc
self.previous = existing.previous + [existing]
Expand Down Expand Up @@ -1213,7 +1233,7 @@ def _run_in_venv(self, *args):
# have to use bash to activate the venv properly first
res = utils.Process(('bash', '-c', ' '.join(
('.', self._venv / 'bin' / 'activate', ';') + args
)))()
)), env=self._get_env())()
if res.exit_code != 0:
raise BuildError(res.output)
return res
Expand All @@ -1228,7 +1248,8 @@ def __call__(self):
wheelhouse.mkdir_p()
if create_venv:
utils.Process(
('virtualenv', '--python', 'python3', self._venv)
('virtualenv', '--python', 'python3', self._venv),
env=self._get_env()
).exit_on_error()()
if self.per_layer:
self._process_per_layer(wheelhouse)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def test_wheelhouse(self, Process, mkdtemp, rmtree_p, ph, pi, pv, sign):
bu.report = False
bu.wheelhouse_overrides = self.dirname / 'wh-over.txt'

def _store_wheelhouses(args):
def _store_wheelhouses(args, **kwargs):
filename = args[-1].split()[-1]
if filename.endswith('.txt'):
Process._wheelhouses.append(path(filename).lines(retain=False))
Expand Down

0 comments on commit 2482f0f

Please sign in to comment.