How to use the diffoscope.comparators.utils.command.Command function in diffoscope

To help you get started, we’ve selected a few diffoscope examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github anthraxx / diffoscope / tests / comparators / test_utils.py View on Github external
def test_trim_stderr_in_command():
    class FillStderr(Command):
        def cmdline(self):
            return ['tee', '/dev/stderr']

        def feed_stdin(self, stdin):
            for dummy in range(0, Command.MAX_STDERR_LINES + 1):
                stdin.write('error {}\n'.format(self.path).encode('utf-8'))
    difference = Difference.from_command(FillStderr, 'dummy1', 'dummy2')
    assert '[ 1 lines ignored ]' in difference.comment
github anthraxx / diffoscope / diffoscope / comparators / ar.py View on Github external
class ArContainer(LibarchiveContainer):
    def get_members(self):
        members = LibarchiveContainer.get_members(self)
        cls = members.__class__
        known_ignores = {
            "/" : "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",
        }
        filtered_out = cls([p for p in members.items() if p[0] in known_ignores])
        if filtered_out:
            for k, v in filtered_out.items():
                logger.debug("ignored ar member '%s' because %s", k, known_ignores[k])
        return cls([p for p in members.items() if p[0] not in known_ignores])

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

class ArFile(File):
    CONTAINER_CLASS = ArContainer
    RE_FILE_TYPE = re.compile(r'\bar archive\b')

    def compare_details(self, other, source=None):
        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")]
github anthraxx / diffoscope / diffoscope / comparators / elf.py View on Github external
self._path = path
        self._section_name = section_name
        super().__init__(path, *args, **kwargs)

    @property
    def section_name(self):
        return self._section_name

    def readelf_options(self):
        return ReadElfSection.base_options() + ['--hex-dump={}'.format(self.section_name)]

class ReadelfStringSection(ReadElfSection):
    def readelf_options(self):
        return ReadElfSection.base_options() + ['--string-dump={}'.format(self.section_name)]

class ObjdumpSection(Command):
    def __init__(self, path, section_name, *args, **kwargs):
        self._path = path
        self._path_bin = path.encode('utf-8')
        self._section_name = section_name
        super().__init__(path, *args, **kwargs)

    def objdump_options(self):
        return []

    @tool_required('objdump')
    def cmdline(self):
        return [
            'objdump',
        ] + self.objdump_options() + [
            '--section={}'.format(self._section_name),
            self.path,
github anthraxx / diffoscope / diffoscope / comparators / macho.py View on Github external
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with diffoscope.  If not, see .

import re
import subprocess

from diffoscope.tools import tool_required
from diffoscope.difference import Difference

from .utils.file import File
from .utils.command import Command


class Otool(Command):
    def __init__(self, path, arch, *args, **kwargs):
        self._path = path
        self._arch = arch
        super().__init__(path, *args, **kwargs)

    @tool_required('otool')
    def cmdline(self):
        return ['otool'] + self.otool_options() + [self.path]

    def otool_options(self):
        return ['-arch', self._arch]

    def filter(self, line):
        try:
            # Strip the filename itself, it's in the first line on its own, terminated by a colon
            if line and line.decode('utf-8').strip() == self._path + ':':
github anthraxx / diffoscope / diffoscope / comparators / pdf.py View on Github external
import re

from diffoscope.tools import tool_required
from diffoscope.difference import Difference

from .utils.file import File
from .utils.command import Command


class Pdftotext(Command):
    @tool_required('pdftotext')
    def cmdline(self):
        return ['pdftotext', self.path, '-']


class Pdftk(Command):
    @tool_required('pdftk')
    def cmdline(self):
        return ['pdftk', self.path, 'output', '-', 'uncompress']

    def filter(self, line):
        return line.decode('latin-1').encode('utf-8')


class PdfFile(File):
    RE_FILE_TYPE = re.compile(r'^PDF document\b')

    def compare_details(self, other, source=None):
        return [Difference.from_command(Pdftotext, self.path, other.path),
                Difference.from_command(Pdftk, self.path, other.path)]
github anthraxx / diffoscope / diffoscope / comparators / utils / command.py View on Github external
def _read_stderr(self):
        for line in iter(self._process.stderr.readline, b''):
            self._stderr_line_count += 1
            if self._stderr_line_count <= Command.MAX_STDERR_LINES:
                self._stderr.write(line)
        if self._stderr_line_count > Command.MAX_STDERR_LINES:
            self._stderr.write('[ {} lines ignored ]\n'.format(self._stderr_line_count - Command.MAX_STDERR_LINES).encode('utf-8'))
        self._process.stderr.close()
github anthraxx / diffoscope / diffoscope / comparators / mono.py View on Github external
# 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
# along with diffoscope.  If not, see .

import re

from diffoscope.tools import tool_required
from diffoscope.difference import Difference

from .utils.file import File
from .utils.command import Command


class Pedump(Command):
    @tool_required('pedump')
    def cmdline(self):
        return ['pedump', self.path]


class MonoExeFile(File):
    RE_FILE_TYPE = re.compile(r'\bPE[0-9]+\b.*\bMono\b')

    def compare_details(self, other, source=None):
        return [Difference.from_command(Pedump, self.path, other.path)]
github anthraxx / diffoscope / diffoscope / comparators / elf.py View on Github external
'aranges',
    'macro',
    'frames',
    'loc',
    'ranges',
    'pubtypes',
    'trace_info',
    'trace_abbrev',
    'trace_aranges',
    'gdb_index',
)

logger = logging.getLogger(__name__)


class Readelf(Command):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # we don't care about the name of the archive
        self._archive_re = re.compile(r'^File: %s\(' % re.escape(self.path))

    @tool_required('readelf')
    def cmdline(self):
        return ['readelf', '--wide'] + self.readelf_options() + [self.path]

    def readelf_options(self):
        return []  # noqa

    def filter(self, line):
        try:
            # we don't care about the name of the archive
            line = self._archive_re.sub('File: lib.a(', line.decode('utf-8'))
github anthraxx / diffoscope / diffoscope / comparators / icc.py View on Github external
# 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
# along with diffoscope.  If not, see .

import re

from diffoscope.tools import tool_required
from diffoscope.difference import Difference

from .utils.file import File
from .utils.command import Command


class Iccdump(Command):
    @tool_required('cd-iccdump')
    def cmdline(self):
        return ['cd-iccdump', self.path]


class IccFile(File):
    RE_FILE_TYPE = re.compile(r'\bColorSync (ICC|color) [Pp]rofile')

    def compare_details(self, other, source=None):
        return [Difference.from_command(Iccdump, self.path, other.path)]
github anthraxx / diffoscope / diffoscope / comparators / openssh.py View on Github external
# 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
# along with diffoscope.  If not, see .

import re

from diffoscope.tools import tool_required
from diffoscope.difference import Difference

from .utils.file import File
from .utils.command import Command


class SSHKeyList(Command):
    @tool_required('ssh-keygen')
    def cmdline(self):
        return ['ssh-keygen', '-l', '-f', self.path]

class PublicKeyFile(File):
    RE_FILE_TYPE = re.compile(r'^OpenSSH \S+ public key')

    def compare_details(self, other, source=None):
        return [Difference.from_command(SSHKeyList, self.path, other.path)]