Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python{2,3} plugins: Make site-packages link relative. #169

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions snapcraft/plugins/python2.py
Expand Up @@ -97,11 +97,11 @@ def _pip(self):
site_packages_dir = os.path.join(
prefix, 'lib', self.python_version, 'site-packages')

# If site-packages doesn't exist, make sure it points to the
# dist-packages in the same directory (this is a relative link so that
# it's still valid when the .snap is installed).
if not os.path.exists(site_packages_dir):
os.symlink(
os.path.join(prefix, 'lib', self.python_version,
'dist-packages'),
site_packages_dir)
os.symlink('dist-packages', site_packages_dir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do the same for python3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good call, I didn't even look at python3. Fixing now. I'm going to go ahead and include the fix in this PR since it's small and I'd say it's the same bug.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


self.run(['python2', easy_install, '--prefix', prefix, 'pip'])

Expand Down
9 changes: 6 additions & 3 deletions snapcraft/plugins/python3.py
Expand Up @@ -97,10 +97,13 @@ def _pip(self):
site_packages_dir = os.path.join(
prefix, 'lib', self.python_version, 'site-packages')

# If site-packages doesn't exist, make sure it points to the
# python3 dist-packages (this is a relative link so that it's still
# valid when the .snap is installed). Note that all python3 versions
# share the same dist-packages (e.g. in python3, not python3.4).
if not os.path.exists(site_packages_dir):
os.symlink(
os.path.join(prefix, 'lib', 'python3', 'dist-packages'),
site_packages_dir)
os.symlink(os.path.join('..', 'python3', 'dist-packages'),
site_packages_dir)

self.run(['python3', easy_install, '--prefix', prefix, 'pip'])

Expand Down
55 changes: 55 additions & 0 deletions snapcraft/tests/test_plugin_python2.py
@@ -0,0 +1,55 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2015 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

from unittest import mock

from snapcraft import tests
from snapcraft.plugins import python2


class Python2PluginTestCase(tests.TestCase):

def setUp(self):
super().setUp()

class Options:
requirements = ''
python_packages = []

self.options = Options()

@mock.patch.object(python2.Python2Plugin, 'run')
@mock.patch.object(python2.Python2Plugin, 'run_output',
return_value='python2.7')
def test_pip_relative_site_packages_symlink(self, run_output_mock,
run_mock):
plugin = python2.Python2Plugin('test-part', self.options)
os.makedirs(plugin.sourcedir)
os.makedirs(os.path.join(plugin.installdir, 'usr', 'lib', 'python2.7',
'dist-packages'))

open(os.path.join(plugin.sourcedir, 'setup.py'), 'w').close()

plugin._pip()

link = os.readlink(os.path.join(plugin.installdir, 'usr', 'lib',
'python2.7', 'site-packages'))
self.assertEqual(link, 'dist-packages',
'Expected site-packages to be a relative link to '
'"dist-packages", but it was a link to "{}"'
.format(link))
57 changes: 57 additions & 0 deletions snapcraft/tests/test_plugin_python3.py
@@ -0,0 +1,57 @@
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2015 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os

from unittest import mock

from snapcraft import tests
from snapcraft.plugins import python3


class Python3PluginTestCase(tests.TestCase):

def setUp(self):
super().setUp()

class Options:
requirements = ''
python_packages = []

self.options = Options()

@mock.patch.object(python3.Python3Plugin, 'run')
@mock.patch.object(python3.Python3Plugin, 'run_output',
return_value='python3.4')
def test_pip_relative_site_packages_symlink(self, run_output_mock,
run_mock):
plugin = python3.Python3Plugin('test-part', self.options)
os.makedirs(plugin.sourcedir)
os.makedirs(os.path.join(plugin.installdir, 'usr', 'lib', 'python3.4'))
os.makedirs(os.path.join(plugin.installdir, 'usr', 'lib', 'python3',
'dist-packages'))

open(os.path.join(plugin.sourcedir, 'setup.py'), 'w').close()

plugin._pip()

link = os.readlink(os.path.join(plugin.installdir, 'usr', 'lib',
'python3.4', 'site-packages'))
expected = os.path.join('..', 'python3', 'dist-packages')
self.assertEqual(link, expected,
'Expected site-packages to be a relative link to '
'"{}", but it was a link to "{}"'.format(expected,
link))