Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
).format(self._checksum),
"utf-8",
)
return data
with tarfile.open("test-sdk.tar", "w") as test_sdk_tar:
open("test-sdk", "w").close()
test_sdk_tar.add("test-sdk")
checksum = file_utils.calculate_hash("test-sdk.tar", algorithm="sha512")
patcher = mock.patch("urllib.request.urlopen")
urlopen_mock = patcher.start()
urlopen_mock.side_effect = fake_urlopen
self.addCleanup(patcher.stop)
original_check_call = snapcraft.internal.common.run
patcher = mock.patch("snapcraft.internal.common.run")
self.mock_check_call = patcher.start()
self.addCleanup(patcher.stop)
def side_effect(cmd, *args, **kwargs):
if cmd[0].endswith("dotnet"):
pass
else:
original_check_call(cmd, *args, **kwargs)
self.mock_check_call.side_effect = side_effect
self.assertThat(options.deb_arch, Equals(self.expected_deb_arch))
self.assertThat(options.kernel_arch, Equals(self.expected_kernel_arch))
# The core dynamic linker is correct. Guard against stray absolute
# paths, as they cause os.path.join to discard the previous
# argument.
self.assertFalse(os.path.isabs(self.expected_core_dynamic_linker))
with mock.patch("os.path.lexists") as mock_lexists:
mock_lexists.return_value = True
with mock.patch("os.path.islink") as mock_islink:
mock_islink.return_value = False
self.assertThat(
options.get_core_dynamic_linker("core"),
Equals(
os.path.join(
common.get_installed_snap_path("core"),
self.expected_core_dynamic_linker,
)
self.useFixture(temp_cwd_fixture)
self.path = temp_cwd_fixture.path
# Use a separate path for XDG dirs, or changes there may be detected as
# source changes.
self.xdg_path = self.useFixture(fixtures.TempDir()).path
self.useFixture(fixture_setup.TempXDG(self.xdg_path))
self.fake_terminal = fixture_setup.FakeTerminal()
self.useFixture(self.fake_terminal)
self.useFixture(fixture_setup.SilentSnapProgress())
# Some tests will directly or indirectly change the plugindir, which
# is a module variable. Make sure that it is returned to the original
# value when a test ends.
self.addCleanup(common.set_plugindir, common.get_plugindir())
self.addCleanup(common.set_schemadir, common.get_schemadir())
self.addCleanup(common.set_extensionsdir, common.get_extensionsdir())
self.addCleanup(common.set_keyringsdir, common.get_keyringsdir())
self.addCleanup(common.reset_env)
common.set_schemadir(os.path.join(get_snapcraft_path(), "schema"))
self.fake_logger = fixtures.FakeLogger(level=logging.ERROR)
self.useFixture(self.fake_logger)
# Some tests will change the apt Dir::Etc::Trusted and
# Dir::Etc::TrustedParts directories. Make sure they're properly reset.
self.addCleanup(
apt.apt_pkg.config.set,
"Dir::Etc::Trusted",
apt.apt_pkg.config.find_file("Dir::Etc::Trusted"),
)
self.addCleanup(
apt.apt_pkg.config.set,
"Dir::Etc::TrustedParts",
def _fix_xml_tools(root):
xml2_config_path = os.path.join(root, 'usr', 'bin', 'xml2-config')
if os.path.isfile(xml2_config_path):
common.run(
['sed', '-i', '-e', 's|prefix=/usr|prefix={}/usr|'.
format(root), xml2_config_path])
xslt_config_path = os.path.join(root, 'usr', 'bin', 'xslt-config')
if os.path.isfile(xslt_config_path):
common.run(
['sed', '-i', '-e', 's|prefix=/usr|prefix={}/usr|'.
format(root), xslt_config_path])
def patch_snapcraft():
import snapcraft.internal.common
import snapcraft.internal.sources._local
# very hacky but gets the job done for now, right now
# SNAPCRAFT_FILES is only used to know what to exclude
snapcraft.internal.common.SNAPCRAFT_FILES.remove("snap")
def _patched_check(self, target):
return False
snapcraft.internal.sources._local.Local._check = _patched_check
assembled_env = assembled_env.replace(self._prime_dir, "$SNAP")
assembled_env = self._install_path_pattern.sub("$SNAP", assembled_env)
if assembled_env:
os.makedirs(os.path.dirname(meta_runner), exist_ok=True)
with open(meta_runner, "w") as f:
print("#!/bin/sh", file=f)
print(assembled_env, file=f)
print(
"export LD_LIBRARY_PATH=$SNAP_LIBRARY_PATH:$LD_LIBRARY_PATH",
file=f,
)
print('exec "$@"', file=f)
os.chmod(meta_runner, 0o755)
common.reset_env()
command_chain.append(os.path.relpath(meta_runner, self._prime_dir))
return command_chain
snap = _snap_data_from_dir(directory)
# Verify that the snap command is available and use it to
# validate the layout.
# The snap command will most likely be found as it lives in
# core and the snapcraft snap lives on top of it (on the side
# rather).
if os.path.exists(_SNAP_PATH):
_run_snap_pack_verification(directory=directory)
elif snap.get("license"):
logger.warning(
"Could not find {!r}, validation of the license string will only "
"take place once pushed to the store.".format(_SNAP_PATH)
)
output_snap_name = output or common.format_snap_name(snap)
# If a .snap-build exists at this point, when we are about to override
# the snap blob, it is stale. We rename it so user have a chance to
# recover accidentally lost assertions.
snap_build = output_snap_name + "-build"
if os.path.isfile(snap_build):
_new = "{}.{}".format(snap_build, int(time.time()))
logger.warning("Renaming stale build assertion to {}".format(_new))
os.rename(snap_build, _new)
_run_mksquashfs(
mksquashfs_path,
directory=directory,
snap_name=snap["name"],
snap_type=snap["type"],
output_snap_name=output_snap_name,
)
for envvar in ["CPPFLAGS", "CFLAGS", "CXXFLAGS"]:
env.append(
formatting_utils.format_path_variable(
envvar, paths, prepend="-I", separator=" "
)
)
paths = common.get_library_paths(root, arch_triplet)
if paths:
env.append(
formatting_utils.format_path_variable(
"LDFLAGS", paths, prepend="-L", separator=" "
)
)
paths = common.get_pkg_config_paths(root, arch_triplet)
if paths:
env.append(
formatting_utils.format_path_variable(
"PKG_CONFIG_PATH", paths, prepend="", separator=":"
)
)
return env
def build_env(root: str, snap_name: str, arch_triplet: str) -> List[str]:
"""Set the environment variables required for building.
This is required for the current parts installdir due to stage-packages
and also to setup the stagedir.
"""
env = []
paths = common.get_include_paths(root, arch_triplet)
if paths:
for envvar in ["CPPFLAGS", "CFLAGS", "CXXFLAGS"]:
env.append(
formatting_utils.format_path_variable(
envvar, paths, prepend="-I", separator=" "
)
)
paths = common.get_library_paths(root, arch_triplet)
if paths:
env.append(
formatting_utils.format_path_variable(
"LDFLAGS", paths, prepend="-L", separator=" "
)
)
def get_build_provider(skip_sanity_checks: bool = False, **kwargs) -> str:
"""Get build provider and determine if running as managed instance."""
provider = kwargs.get("provider")
if not provider:
if kwargs.get("use_lxd"):
provider = "lxd"
elif kwargs.get("destructive_mode"):
provider = "host"
elif common.is_process_container():
provider = "host"
else:
# Default is multipass.
provider = "multipass"
# Sanity checks may be skipped for the purpose of checking legacy.
if not skip_sanity_checks:
_sanity_check_build_provider_flags(provider, **kwargs)
return provider