ar.py 2.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2014-2015 Jérémy Bobbio <lunar@debian.org>
# Copyright © 2016 Ximin Luo <infinity0@pwned.gg>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope 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
19
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
20 21

import re
22
import logging
Chris Lamb's avatar
Chris Lamb committed
23

24
from diffoscope.tools import tool_required
25
from diffoscope.difference import Difference
26

27
from .utils.file import File
28
from .utils.command import Command
29
from .utils.libarchive import LibarchiveContainer, list_libarchive
30

31 32
logger = logging.getLogger(__name__)

33

34 35 36 37 38
# TODO: this would also be useful for Go archives. Currently those are handled
# by StaticLibFile, but then readelf complains with "Error: Not an ELF file".
# ArFile gives slightly more reasonable output, e.g. a readable plain diff of
# the __.PKGDEF member which is just a text file containing the Go interface.

39
class ArContainer(LibarchiveContainer):
40 41
    def get_adjusted_members(self):
        members = list(super().get_adjusted_members())
42
        known_ignores = {
Mattia Rizzolo's avatar
Mattia Rizzolo committed
43 44
            "/": "this is the symbol table, already accounted for in other output",
            "//": "this is the table for GNU long names, already accounted for in the archive filelist",
45
        }
46
        filtered_out = [p for p in members if p[0] in known_ignores]
47
        if filtered_out:
48
            for k, v in filtered_out:
49 50
                logger.debug("ignored ar member '%s' because %s",
                             k, known_ignores[k])
51
        return [p for p in members if p[0] not in known_ignores]
52

Mattia Rizzolo's avatar
Mattia Rizzolo committed
53

54 55 56 57
class ArSymbolTableDumper(Command):
    @tool_required('nm')
    def cmdline(self):
        return ['nm', '-s', self.path]
58

Mattia Rizzolo's avatar
Mattia Rizzolo committed
59

60
class ArFile(File):
61
    DESCRIPTION = "ar(1) archives"
62
    CONTAINER_CLASS = ArContainer
63
    FILE_TYPE_RE = re.compile(r'\bar archive\b')
64 65

    def compare_details(self, other, source=None):
66 67 68 69
        return [Difference.from_command(ArSymbolTableDumper, self.path, other.path),
                Difference.from_text_readers(list_libarchive(self.path),
                                             list_libarchive(other.path),
                                             self.path, other.path, source="file list")]