Skip to content

Commit

Permalink
repo: always use host release and arch for Ubuntu (#3006)
Browse files Browse the repository at this point in the history
- Remove dependency of ProjectOptions parameter.
- Update format_sources_list() tp query release arch/release
  separately.
- Update tests accordingly.

Signed-off-by: Chris Patterson <chris.patterson@canonical.com>
  • Loading branch information
Chris Patterson committed Apr 3, 2020
1 parent 7396004 commit 7045361
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 120 deletions.
5 changes: 1 addition & 4 deletions snapcraft/internal/project_loader/_parts_config.py
Expand Up @@ -195,10 +195,7 @@ def load_part(self, part_name, plugin_name, part_properties):
keyrings = plugin.PLUGIN_STAGE_KEYRINGS

stage_packages_repo = repo.Repo(
plugin.osrepodir,
sources=sources,
keyrings=keyrings,
project_options=self._project,
plugin.osrepodir, sources=sources, keyrings=keyrings
)

grammar_processor = grammar_processing.PartGrammarProcessor(
Expand Down
34 changes: 7 additions & 27 deletions snapcraft/internal/repo/_deb.py
Expand Up @@ -30,7 +30,6 @@

import apt

import snapcraft
from snapcraft import file_utils
from snapcraft.internal import cache, common, os_release, repo
from snapcraft.internal.indicators import is_dumb_terminal
Expand Down Expand Up @@ -193,8 +192,7 @@ def _run_dpkg_query_list_files(package_name: str) -> Set[str]:


class _AptCache:
def __init__(self, deb_arch, *, sources: List[str], keyrings: List[str]):
self._deb_arch = deb_arch
def __init__(self, *, sources: List[str], keyrings: List[str]):
self._sources = sources
self._keyrings = keyrings

Expand Down Expand Up @@ -344,12 +342,8 @@ def _collected_sources_list(self) -> str:

# Append additionally configured repositories, if any.
if self._sources:
release = os_release.OsRelease()
additional_sources = _format_sources_list(
"\n".join(self._sources),
deb_arch=self._deb_arch,
release=release.version_codename(),
)

additional_sources = _format_sources_list("\n".join(self._sources))
sources = "\n".join([sources, additional_sources])

return sources
Expand Down Expand Up @@ -532,18 +526,13 @@ def __init__(
super().__init__(rootdir)
self._downloaddir = os.path.join(rootdir, "download")

if not project_options:
project_options = snapcraft.ProjectOptions()

if sources is None:
sources = list()

if keyrings is None:
keyrings = list()

self._apt = _AptCache(
project_options.deb_arch, sources=sources, keyrings=keyrings
)
self._apt = _AptCache(sources=sources, keyrings=keyrings)

self._cache = cache.AptStagePackageCache(
sources_digest=self._apt.sources_digest()
Expand Down Expand Up @@ -659,19 +648,10 @@ def _get_local_sources_list():
return sources


def _format_sources_list(sources_list: str, *, deb_arch: str, release: str = "xenial"):
if deb_arch in ("amd64", "i386"):
prefix = "archive"
suffix = "ubuntu"
security = "security"
else:
prefix = "ports"
suffix = "ubuntu-ports"
security = "ports"
def _format_sources_list(sources_list: str):
release = os_release.OsRelease().version_codename()

return string.Template(sources_list).substitute(
{"prefix": prefix, "release": release, "suffix": suffix, "security": security}
)
return string.Template(sources_list).substitute({"release": release})


def _set_pkg_version(pkg, version):
Expand Down
1 change: 0 additions & 1 deletion snapcraft/plugins/v1/_ros/rosdep.py
Expand Up @@ -98,7 +98,6 @@ def setup(self):
self._rosdep_path,
sources=self._ubuntu_sources,
keyrings=self._ubuntu_keyrings,
project_options=self._project,
)

logger.info("Fetching rosdep...")
Expand Down
1 change: 0 additions & 1 deletion snapcraft/plugins/v1/_ros/wstool.py
Expand Up @@ -92,7 +92,6 @@ def setup(self) -> None:
self._wstool_path,
sources=self._ubuntu_sources,
keyrings=self._ubuntu_keyrings,
project_options=self._project,
)
logger.info("Fetching wstool...")
ubuntu.get(["python-wstool"])
Expand Down
2 changes: 0 additions & 2 deletions snapcraft/plugins/v1/catkin.py
Expand Up @@ -549,7 +549,6 @@ def _setup_apt_dependencies(self, apt_dependencies):
ubuntudir,
sources=self.PLUGIN_STAGE_SOURCES,
keyrings=self.PLUGIN_STAGE_KEYRINGS,
project_options=self.project,
)

logger.info("Fetching apt dependencies...")
Expand Down Expand Up @@ -993,7 +992,6 @@ def setup(self):
self._catkin_path,
sources=self._ubuntu_sources,
keyrings=self._ubuntu_keyrings,
project_options=self._project,
)
logger.info("Fetching catkin...")
ubuntu.get(["ros-{}-catkin".format(self._ros_distro)])
Expand Down
1 change: 0 additions & 1 deletion snapcraft/plugins/v1/colcon.py
Expand Up @@ -383,7 +383,6 @@ def _setup_apt_dependencies(self, apt_dependencies):
ubuntudir,
sources=self.PLUGIN_STAGE_SOURCES,
keyrings=self.PLUGIN_STAGE_KEYRINGS,
project_options=self.project,
)

logger.info("Fetching apt dependencies...")
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/plugins/v1/ros/test_rosdep.py
Expand Up @@ -62,10 +62,7 @@ def test_setup(self):
self.ubuntu_mock.assert_has_calls(
[
mock.call(
self.rosdep._rosdep_path,
sources="sources",
keyrings=["keyring"],
project_options=self.project,
self.rosdep._rosdep_path, sources="sources", keyrings=["keyring"]
),
mock.call().get(["python-rosdep"]),
mock.call().unpack(self.rosdep._rosdep_install_path),
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/plugins/v1/ros/test_wstool.py
Expand Up @@ -55,10 +55,7 @@ def test_setup(self):
self.ubuntu_mock.assert_has_calls(
[
mock.call(
self.wstool._wstool_path,
sources="sources",
keyrings=["keyring"],
project_options=self.project,
self.wstool._wstool_path, sources="sources", keyrings=["keyring"]
),
mock.call().get(["python-wstool"]),
mock.call().unpack(self.wstool._wstool_install_path),
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/plugins/v1/test_catkin.py
Expand Up @@ -1741,10 +1741,7 @@ def test_setup(self):
self.ubuntu_mock.assert_has_calls(
[
mock.call(
self.catkin._catkin_path,
sources="sources",
keyrings=["keyring"],
project_options=self.project,
self.catkin._catkin_path, sources="sources", keyrings=["keyring"]
),
mock.call().get(["ros-kinetic-catkin"]),
mock.call().unpack(self.catkin._catkin_install_path),
Expand Down
89 changes: 17 additions & 72 deletions tests/unit/repo/test_deb.py
Expand Up @@ -18,12 +18,12 @@
import os
import textwrap
from subprocess import CalledProcessError
from unittest import mock
from unittest.mock import ANY, DEFAULT, call, patch, MagicMock

from testtools.matchers import Contains, Equals, FileExists, Not
import fixtures

import snapcraft
from snapcraft.internal import repo
from snapcraft.internal.repo import errors
from tests import fixture_setup, unit
Expand All @@ -50,8 +50,7 @@ def _fetch_binary(download_dir, **kwargs):
def test_cache_update_failed(self, mock_apt_pkg):
self.mock_cache().is_virtual_package.return_value = False
self.mock_cache().update.side_effect = apt.cache.FetchFailedException()
project_options = snapcraft.ProjectOptions()
ubuntu = repo.Ubuntu(self.tempdir, project_options=project_options)
ubuntu = repo.Ubuntu(self.tempdir)
self.assertRaises(errors.CacheUpdateFailedError, ubuntu.get, ["fake-package"])

@patch("shutil.rmtree")
Expand All @@ -64,8 +63,7 @@ def test_cache_hashsum_mismatch(self, mock_apt_pkg, mock_rmtree):
),
DEFAULT,
]
project_options = snapcraft.ProjectOptions()
ubuntu = repo.Ubuntu(self.tempdir, project_options=project_options)
ubuntu = repo.Ubuntu(self.tempdir)
ubuntu.get(["fake-package"])

def test_get_pkg_name_parts_name_only(self):
Expand Down Expand Up @@ -99,8 +97,7 @@ def _fake_find_file(key: str):

mock_apt_pkg.config.find_file.side_effect = _fake_find_file

project_options = snapcraft.ProjectOptions()
ubuntu = repo.Ubuntu(self.tempdir, project_options=project_options)
ubuntu = repo.Ubuntu(self.tempdir)
ubuntu.get(["fake-package"])

mock_apt_pkg.assert_has_calls(
Expand Down Expand Up @@ -148,8 +145,7 @@ def test_get_package_fetch_error(self, mock_apt_pkg):
"foo"
)
self.mock_cache().is_virtual_package.return_value = False
project_options = snapcraft.ProjectOptions()
ubuntu = repo.Ubuntu(self.tempdir, project_options=project_options)
ubuntu = repo.Ubuntu(self.tempdir)
raised = self.assertRaises(
errors.PackageFetchError, ubuntu.get, ["fake-package"]
)
Expand All @@ -167,8 +163,7 @@ def _fake_find_file(key: str):

mock_apt_pkg.config.find_file.side_effect = _fake_find_file

project_options = snapcraft.ProjectOptions()
ubuntu = repo.Ubuntu(self.tempdir, project_options=project_options)
ubuntu = repo.Ubuntu(self.tempdir)
ubuntu.get(["fake-package"])

mock_apt_pkg.assert_has_calls(
Expand Down Expand Up @@ -218,8 +213,7 @@ def _fake_find_file(key: str):

mock_apt_pkg.config.find_file.side_effect = _fake_find_file

project_options = snapcraft.ProjectOptions()
ubuntu = repo.Ubuntu(self.tempdir, project_options=project_options)
ubuntu = repo.Ubuntu(self.tempdir)
ubuntu.get(["fake-package:arch"])

mock_apt_pkg.assert_has_calls(
Expand Down Expand Up @@ -253,71 +247,23 @@ def _fake_find_file(key: str):
os.path.join(self.tempdir, "download", "fake-package.deb"), FileExists()
)

def test_sources_amd64_vivid(self):
self.maxDiff = None
@mock.patch(
"snapcraft.internal.os_release.OsRelease.version_codename", return_value="testy"
)
def test_sources_formatting(self, mock_version_codename):
sources_list = textwrap.dedent(
"""
deb http://${prefix}.ubuntu.com/${suffix}/ ${release} main restricted
deb http://${prefix}.ubuntu.com/${suffix}/ ${release}-updates main restricted
deb http://${prefix}.ubuntu.com/${suffix}/ ${release} universe
deb http://${prefix}.ubuntu.com/${suffix}/ ${release}-updates universe
deb http://${prefix}.ubuntu.com/${suffix}/ ${release} multiverse
deb http://${prefix}.ubuntu.com/${suffix}/ ${release}-updates multiverse
deb http://${security}.ubuntu.com/${suffix} ${release}-security main restricted
deb http://${security}.ubuntu.com/${suffix} ${release}-security universe
deb http://${security}.ubuntu.com/${suffix} ${release}-security multiverse
"""
)

sources_list = repo._deb._format_sources_list(
sources_list, release="vivid", deb_arch="amd64"
)

expected_sources_list = textwrap.dedent(
"""
deb http://archive.ubuntu.com/ubuntu/ vivid main restricted
deb http://archive.ubuntu.com/ubuntu/ vivid-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ vivid universe
deb http://archive.ubuntu.com/ubuntu/ vivid-updates universe
deb http://archive.ubuntu.com/ubuntu/ vivid multiverse
deb http://archive.ubuntu.com/ubuntu/ vivid-updates multiverse
deb http://security.ubuntu.com/ubuntu vivid-security main restricted
deb http://security.ubuntu.com/ubuntu vivid-security universe
deb http://security.ubuntu.com/ubuntu vivid-security multiverse
deb http://archive.ubuntu.com/ubuntu ${release} main restricted
deb http://archive.ubuntu.com/ubuntu ${release}-updates main restricted
"""
)
self.assertThat(sources_list, Equals(expected_sources_list))

def test_sources_armhf_trusty(self):
sources_list = textwrap.dedent(
"""
deb http://${prefix}.ubuntu.com/${suffix}/ ${release} main restricted
deb http://${prefix}.ubuntu.com/${suffix}/ ${release}-updates main restricted
deb http://${prefix}.ubuntu.com/${suffix}/ ${release} universe
deb http://${prefix}.ubuntu.com/${suffix}/ ${release}-updates universe
deb http://${prefix}.ubuntu.com/${suffix}/ ${release} multiverse
deb http://${prefix}.ubuntu.com/${suffix}/ ${release}-updates multiverse
deb http://${security}.ubuntu.com/${suffix} ${release}-security main restricted
deb http://${security}.ubuntu.com/${suffix} ${release}-security universe
deb http://${security}.ubuntu.com/${suffix} ${release}-security multiverse
"""
)

sources_list = repo._deb._format_sources_list(
sources_list, deb_arch="armhf", release="trusty"
)
sources_list = repo._deb._format_sources_list(sources_list)

expected_sources_list = textwrap.dedent(
"""
deb http://ports.ubuntu.com/ubuntu-ports/ trusty main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ trusty-updates main restricted
deb http://ports.ubuntu.com/ubuntu-ports/ trusty universe
deb http://ports.ubuntu.com/ubuntu-ports/ trusty-updates universe
deb http://ports.ubuntu.com/ubuntu-ports/ trusty multiverse
deb http://ports.ubuntu.com/ubuntu-ports/ trusty-updates multiverse
deb http://ports.ubuntu.com/ubuntu-ports trusty-security main restricted
deb http://ports.ubuntu.com/ubuntu-ports trusty-security universe
deb http://ports.ubuntu.com/ubuntu-ports trusty-security multiverse
deb http://archive.ubuntu.com/ubuntu testy main restricted
deb http://archive.ubuntu.com/ubuntu testy-updates main restricted
"""
)
self.assertThat(sources_list, Equals(expected_sources_list))
Expand Down Expand Up @@ -377,8 +323,7 @@ def test_autokeep(self):
self.fake_apt_cache.cache["dependency"]
]

project_options = snapcraft.ProjectOptions()
ubuntu = repo.Ubuntu(self.tempdir, project_options=project_options)
ubuntu = repo.Ubuntu(self.tempdir)
ubuntu.get(["main-package", "conflicting-dependency"])

# Verify that the package was actually fetched and copied into the
Expand Down

0 comments on commit 7045361

Please sign in to comment.