def test_get_host_tool_failure(monkeypatch): monkeypatch.setattr(shutil, "which", lambda x: None) with pytest.raises(errors.SnapcraftHostToolNotFoundError) as error: file_utils.get_host_tool_path(command_name="foo", package_name="foo-pkg") assert error.command_name == "foo" assert error.package_name == "foo-pkg"
def _get_usable_keys(name=None): snap_path = get_host_tool_path(command_name="snap", package_name="snapd") keys = json.loads( subprocess.check_output([snap_path, "keys", "--json"], universal_newlines=True)) if keys is not None: for key in keys: if name is None or name == key["name"]: yield key
def _pack( directory: str, *, compression: Optional[str] = None, output: Optional[str] ) -> None: """Pack a snap. :param directory: directory to snap :param compression: compression type to use, None for defaults :param output: Output may either be: (1) a directory path to output snaps to (2) an explicit file path to output snap to (3) unpsecified/None to output to current (project) directory """ output_file = None output_dir = None if output: output_path = pathlib.Path(output) output_parent = output_path.parent if output_path.is_dir(): output_dir = str(output_path) elif output_parent and output_parent != pathlib.Path("."): output_dir = str(output_parent) output_file = output_path.name else: output_file = output snap_path = file_utils.get_host_tool_path(command_name="snap", package_name="snapd") command: List[Union[str, pathlib.Path]] = [snap_path, "pack"] # When None, just use snap pack's default settings. if compression is not None: if compression != "xz": echo.warning( f"EXPERIMENTAL: Setting the squash FS compression to {compression!r}." ) command.extend(["--compression", compression]) if output_file is not None: command.extend(["--filename", output_file]) command.append(directory) if output_dir is not None: command.append(output_dir) logger.debug(f"Running pack command: {command}") snap_filename = _run_pack(command) echo.info(f"Snapped {snap_filename}")
def _ldd(path: str, ld_library_paths: List[str], *, ld_preload: Optional[str] = None) -> Dict[str, str]: """Use host ldd to determine library dependencies.""" ldd_path = str( file_utils.get_host_tool_path(command_name="ldd", package_name="libc-bin")) env = { "LD_LIBRARY_PATH": ":".join(ld_library_paths), } if ld_preload: env["LD_PRELOAD"] = ld_preload return _parse_ldd_output(_check_output([ldd_path, path], extra_env=env))
def _generate_snap_build(authority_id, snap_id, grade, key_name, snap_filename): """Return the signed snap-build declaration for a snap on disk.""" snap_path = get_host_tool_path(command_name="snap", package_name="snapd") cmd = [ snap_path, "sign-build", "--developer-id=" + authority_id, "--snap-id=" + snap_id, "--grade=" + grade, ] if key_name: cmd.extend(["-k", key_name]) cmd.append(snap_filename) try: return subprocess.check_output(cmd) except subprocess.CalledProcessError as e: raise storeapi.errors.SignBuildAssertionError(snap_filename) from e
def _pack(directory: str, *, compression: Optional[str] = None, output: Optional[str]) -> None: snap_path = file_utils.get_host_tool_path(command_name="snap", package_name="snapd") command: List[Union[str, pathlib.Path]] = [snap_path, "pack"] # When None, just use snap pack's default settings. if compression is not None: if compression != "xz": echo.warning( f"EXPERIMENTAL: Setting the squash FS compression to {compression!r}." ) command.extend(["--compression", compression]) if output is not None: command.extend(["--filename", output]) command.append(directory) snap_filename = _run_pack(command) echo.info(f"Snapped {snap_filename}")
def test_get_host_tool_finds_command(monkeypatch): monkeypatch.setattr(shutil, "which", lambda x: "/usr/bin/foo") assert file_utils.get_host_tool_path(command_name="foo", package_name="foo")