Пример #1
0
    def test_get_tool_from_snapcraft_snap_path(self, in_snap, tool_path,
                                               fake_exists):
        abs_tool_path = pathlib.Path("/snap/snapcraft/current") / tool_path
        fake_exists.paths = [abs_tool_path]

        assert file_utils.get_snap_tool_path(
            "tool-command") == abs_tool_path.as_posix()
Пример #2
0
def _get_icon_from_snap_file(snap_path):
    icon_file = None
    with tempfile.TemporaryDirectory() as temp_dir:
        unsquashfs_path = get_snap_tool_path("unsquashfs")
        try:
            output = subprocess.check_output([
                unsquashfs_path,
                "-d",
                os.path.join(temp_dir, "squashfs-root"),
                snap_path,
                "-e",
                "meta/gui",
            ])
        except subprocess.CalledProcessError:
            raise SnapDataExtractionError(os.path.basename(snap_path))
        logger.debug("Output extracting icon from snap: %s", output)
        for extension in ("png", "svg"):
            icon_name = "icon.{}".format(extension)
            icon_path = os.path.join(temp_dir, "squashfs-root", "meta/gui",
                                     icon_name)
            if os.path.exists(icon_path):
                icon_file = open(icon_path, "rb")
                break
        try:
            yield icon_file
        finally:
            if icon_file is not None:
                icon_file.close()
Пример #3
0
def clear_execstack(*, elf_files: FrozenSet[elf.ElfFile]) -> None:
    """Clears the execstack for the relevant elf_files

    param elf.ElfFile elf_files: the full list of elf files to analyze
                                 and clear the execstack if present.
    """
    execstack_path = file_utils.get_snap_tool_path("execstack")
    elf_files_with_execstack = [e for e in elf_files if e.execstack_set]

    if elf_files_with_execstack:
        formatted_items = [
            "- {}".format(e.path) for e in elf_files_with_execstack
        ]
        logger.warning(
            "The execstacks are going to be cleared for the following "
            "files:\n{}\n"
            "To disable this behavior set "
            "`build-attributes: [keep-execstack]` "
            "for the part.".format("\n".join(formatted_items)))

    for elf_file in elf_files_with_execstack:
        try:
            subprocess.check_call(
                [execstack_path, "--clear-execstack", elf_file.path])
        except subprocess.CalledProcessError:
            logger.warning("Failed to clear execstack for {!r}".format(
                elf_file.path))
Пример #4
0
    def test_get_tool_from_docker_snap_path(self, monkeypatch, in_snap,
                                            tool_path, fake_exists):
        abs_tool_path = pathlib.Path("/snap/snapcraft/current") / tool_path
        fake_exists.paths = [abs_tool_path]
        monkeypatch.setattr(common, "is_process_container", lambda: True)

        assert file_utils.get_snap_tool_path(
            "tool-command") == abs_tool_path.as_posix()
Пример #5
0
    def test_get_tool_from_host_path(self, monkeypatch, tool_path,
                                     fake_exists):
        abs_tool_path = pathlib.Path("/") / tool_path
        fake_exists.paths = [abs_tool_path]
        monkeypatch.setattr(shutil, "which",
                            lambda x: abs_tool_path.as_posix())

        assert file_utils.get_snap_tool_path(
            "tool-command") == abs_tool_path.as_posix()
Пример #6
0
    def __init__(
        self, *, dynamic_linker: str, root_path: str, preferred_patchelf_path=None
    ) -> None:
        """Create a Patcher instance.

        :param str dynamic_linker: the path to the dynamic linker to set the
                                   elf file to.
        :param str root_path: the base path for the snap to determine
                              if use of $ORIGIN is possible.
        :param str preferred_patchelf_path: patch the necessary elf_files with
                                        this patchelf.
        """
        self._dynamic_linker = dynamic_linker
        self._root_path = root_path

        if preferred_patchelf_path:
            self._patchelf_cmd = preferred_patchelf_path
        else:
            self._patchelf_cmd = file_utils.get_snap_tool_path("patchelf")

        self._strip_cmd = file_utils.get_snap_tool_path("strip")
Пример #7
0
    def __init__(self,
                 *,
                 source_path: str,
                 target_path: str,
                 delta_tool: str,
                 delta_format: str,
                 delta_file_extname: str = "delta") -> None:
        self.source_path = source_path
        self.target_path = target_path
        self.delta_format = delta_format
        self.delta_file_extname = delta_file_extname
        self.delta_tool_path = file_utils.get_snap_tool_path(delta_tool)

        # some pre-checks
        self._check_properties()
        self._check_file_existence()
Пример #8
0
def _get_data_from_snap_file(snap_path):
    with tempfile.TemporaryDirectory() as temp_dir:
        unsquashfs_path = get_snap_tool_path("unsquashfs")
        try:
            output = subprocess.check_output([
                unsquashfs_path,
                "-d",
                os.path.join(temp_dir, "squashfs-root"),
                snap_path,
                "-e",
                # cygwin unsquashfs on windows uses unix paths.
                Path("meta", "snap.yaml").as_posix(),
            ])
        except subprocess.CalledProcessError:
            raise SnapDataExtractionError(os.path.basename(snap_path))
        logger.debug(output)
        with open(os.path.join(temp_dir, "squashfs-root", "meta",
                               "snap.yaml")) as yaml_file:
            snap_yaml = yaml_utils.load(yaml_file)
    return snap_yaml
Пример #9
0
 def _get_snap_deb_arch(self, snap_filename):
     with tempfile.TemporaryDirectory() as temp_dir:
         unsquashfs_path = file_utils.get_snap_tool_path("unsquashfs")
         output = subprocess.check_output([
             unsquashfs_path,
             "-d",
             os.path.join(temp_dir, "squashfs-root"),
             snap_filename,
             "-e",
             # cygwin unsquashfs uses unix paths.
             Path("meta", "snap.yaml").as_posix(),
         ])
         logger.debug(output)
         with open(
                 os.path.join(temp_dir, "squashfs-root", "meta",
                              "snap.yaml")) as yaml_file:
             snap_yaml = yaml_utils.load(yaml_file)
     # XXX: add multiarch support later
     try:
         return snap_yaml["architectures"][0]
     except KeyError:
         return "all"