Commit 5d73cf31 authored by Chris Lamb's avatar Chris Lamb 💬

Tidy imports.

Signed-off-by: Chris Lamb's avatarChris Lamb <lamby@debian.org>
parent d822f4e6
......@@ -19,8 +19,9 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import os.path
import os
import sys
for root in ['.', '..']:
if os.path.exists(os.path.join(root, 'diffoscope/__init__.py')):
sys.path = [root] + sys.path
......
......@@ -17,12 +17,14 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from functools import wraps
import logging
from distutils.spawn import find_executable
import os
import shutil
import logging
import platform
import tempfile
import functools
from distutils.spawn import find_executable
VERSION = "59"
......@@ -231,7 +233,6 @@ class OutputParsingError(Exception):
self.object_class = object.__class__
def get_current_os():
import platform
system = platform.system()
if system == "Linux":
# FIXME: Will break under Python 3.7, see:
......@@ -247,11 +248,11 @@ def tool_required(command):
tool_required.all.add(command)
def wrapper(original_function):
if find_executable(command):
@wraps(original_function)
@functools.wraps(original_function)
def tool_check(*args, **kwargs):
return original_function(*args, **kwargs)
else:
@wraps(original_function)
@functools.wraps(original_function)
def tool_check(*args, **kwargs):
raise RequiredToolNotFound(command)
return tool_check
......
......@@ -44,7 +44,9 @@ __license__ = 'MIT'
import os.path
import hashlib
import subprocess
from debian import deb822
from diffoscope import logger, tool_required
......
......@@ -18,68 +18,75 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
import sys
import magic
import operator
import os.path
import re
import sys
try:
import tlsh
except ImportError:
tlsh = None
from diffoscope import logger, tool_required
from diffoscope.config import Config
from diffoscope.difference import Difference
from diffoscope.comparators.ar import ArFile
from diffoscope.comparators.binary import \
File, FilesystemFile, NonExistingFile, compare_binary_files
from diffoscope.comparators.bzip2 import Bzip2File
from diffoscope.comparators.java import ClassFile
from diffoscope.comparators.cbfs import CbfsFile
from diffoscope.comparators.cpio import CpioFile
from diffoscope.comparators.ps import PsFile
from diffoscope.comparators.xz import XzFile
from diffoscope.comparators.deb import DebFile, Md5sumsFile, DebDataTarFile
try:
from diffoscope.comparators.debian import DotChangesFile, DotDscFile, DotBuildinfoFile
except ImportError as ex:
if hasattr(ex, 'msg') and not ex.msg.startswith("No module named 'debian"):
raise
from diffoscope.comparators.debian_fallback import DotChangesFile, DotDscFile, DotBuildinfoFile
from diffoscope.comparators.device import Device
from diffoscope.comparators.zip import ZipFile, MozillaZipFile
from diffoscope.comparators.dex import DexFile
from diffoscope.comparators.directory import FilesystemDirectory, Directory, compare_directories
from diffoscope.comparators.tar import TarFile
from diffoscope.comparators.ipk import IpkFile
from diffoscope.comparators.png import PngFile
from diffoscope.comparators.ppu import PpuFile
from diffoscope.comparators.elf import ElfFile, ElfSection, StaticLibFile
from diffoscope.comparators.fsimage import FsImageFile
from diffoscope.comparators.fonts import TtfFile
from diffoscope.comparators.gettext import MoFile
from diffoscope.comparators.icc import IccFile
from diffoscope.comparators.git import GitIndexFile
from diffoscope.comparators.pdf import PdfFile
from diffoscope.comparators.rust import RustObjectFile
from diffoscope.comparators.cpio import CpioFile
from diffoscope.comparators.text import TextFile
from diffoscope.comparators.gzip import GzipFile
from diffoscope.comparators.haskell import HiFile
from diffoscope.comparators.icc import IccFile
from diffoscope.comparators.image import ImageFile
from diffoscope.comparators.ipk import IpkFile
from diffoscope.comparators.iso9660 import Iso9660File
from diffoscope.comparators.java import ClassFile
from diffoscope.comparators.json import JSONFile
from diffoscope.comparators.llvm import LlvmBitCodeFile
from diffoscope.comparators.macho import MachoFile
from diffoscope.comparators.mono import MonoExeFile
from diffoscope.comparators.pdf import PdfFile
from diffoscope.comparators.png import PngFile
from diffoscope.comparators.ppu import PpuFile
from diffoscope.comparators.ps import PsFile
from diffoscope.comparators.rust import RustObjectFile
from diffoscope.comparators.cbfs import CbfsFile
from diffoscope.comparators.image import ImageFile
from diffoscope.comparators.fonts import TtfFile
from diffoscope.comparators.macho import MachoFile
from diffoscope.comparators.bzip2 import Bzip2File
from diffoscope.comparators.sqlite import Sqlite3Database
from diffoscope.comparators.binary import File, FilesystemFile, NonExistingFile, \
compare_binary_files
from diffoscope.comparators.device import Device
from diffoscope.comparators.symlink import Symlink
from diffoscope.comparators.fsimage import FsImageFile
from diffoscope.comparators.gettext import MoFile
from diffoscope.comparators.iso9660 import Iso9660File
from diffoscope.comparators.haskell import HiFile
from diffoscope.comparators.squashfs import SquashfsFile
from diffoscope.comparators.directory import FilesystemDirectory, Directory, \
compare_directories
try:
import tlsh
except ImportError:
tlsh = None
try:
from diffoscope.comparators.debian import DotChangesFile, DotDscFile, \
DotBuildinfoFile
except ImportError as ex:
if hasattr(ex, 'msg') and not ex.msg.startswith("No module named 'debian"):
raise
from diffoscope.comparators.debian_fallback import DotChangesFile, \
DotDscFile, DotBuildinfoFile
try:
from diffoscope.comparators.rpm import RpmFile
except ImportError as ex:
if hasattr(ex, 'msg') and ex.msg != "No module named 'rpm'":
raise
from diffoscope.comparators.rpm_fallback import RpmFile
from diffoscope.comparators.sqlite import Sqlite3Database
from diffoscope.comparators.squashfs import SquashfsFile
from diffoscope.comparators.symlink import Symlink
from diffoscope.comparators.text import TextFile
from diffoscope.comparators.tar import TarFile
from diffoscope.comparators.xz import XzFile
from diffoscope.comparators.zip import ZipFile, MozillaZipFile
def bail_if_non_existing(*paths):
......
......@@ -19,12 +19,13 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
from diffoscope import tool_required
from diffoscope import logger, tool_required
from diffoscope.difference import Difference
from diffoscope.comparators.binary import File
from diffoscope.comparators.libarchive import LibarchiveContainer, list_libarchive
from diffoscope.comparators.utils import Command
from diffoscope import logger
from diffoscope.comparators.binary import File
from diffoscope.comparators.libarchive import LibarchiveContainer, \
list_libarchive
# TODO: this would also be useful for Go archives. Currently those are handled
......
......@@ -17,22 +17,25 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from abc import ABCMeta, abstractmethod
from binascii import hexlify
from io import StringIO
import io
import os
import os.path
import re
from stat import S_ISCHR, S_ISBLK
import abc
import stat
import magic
import binascii
import subprocess
from diffoscope import tool_required, RequiredToolNotFound, logger, \
OutputParsingError
from diffoscope.config import Config
from diffoscope.difference import Difference
try:
import tlsh
except ImportError:
tlsh = None
import magic
from diffoscope.config import Config
from diffoscope.difference import Difference
from diffoscope import tool_required, RequiredToolNotFound, OutputParsingError, logger
# helper function to convert to bytes if necessary
def maybe_decode(s):
......@@ -42,10 +45,10 @@ def maybe_decode(s):
return s
def hexdump_fallback(path):
hexdump = StringIO()
hexdump = io.StringIO()
with open(path, 'rb') as f:
for buf in iter(lambda: f.read(32), b''):
hexdump.write('%s\n' % hexlify(buf).decode('us-ascii'))
hexdump.write('%s\n' % binascii.hexlify(buf).decode('us-ascii'))
return hexdump.getvalue()
......@@ -62,7 +65,7 @@ def compare_binary_files(file1, file2, source=None):
SMALL_FILE_THRESHOLD = 65536 # 64 kiB
class File(object, metaclass=ABCMeta):
class File(object, metaclass=abc.ABCMeta):
if hasattr(magic, 'open'): # use Magic-file-extensions from file
@classmethod
def guess_file_type(self, path):
......@@ -98,7 +101,7 @@ class File(object, metaclass=ABCMeta):
# This should return a path that allows to access the file content
@property
@abstractmethod
@abc.abstractmethod
def path(self):
raise NotImplementedError()
......@@ -154,15 +157,15 @@ class File(object, metaclass=ABCMeta):
self._fuzzy_hash = None
return self._fuzzy_hash
@abstractmethod
@abc.abstractmethod
def is_directory():
raise NotImplementedError()
@abstractmethod
@abc.abstractmethod
def is_symlink():
raise NotImplementedError()
@abstractmethod
@abc.abstractmethod
def is_device():
raise NotImplementedError()
......@@ -258,7 +261,7 @@ class FilesystemFile(File):
def is_device(self):
mode = os.lstat(self._name).st_mode
return S_ISCHR(mode) or S_ISBLK(mode)
return stat.S_ISCHR(mode) or stat.S_ISBLK(mode)
class NonExistingFile(File):
......
......@@ -17,13 +17,14 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
import os.path
import re
import os.path
import subprocess
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Archive, get_compressed_content_name
import collections
from diffoscope import logger, tool_required
from diffoscope.comparators.utils import Archive, get_compressed_content_name
from diffoscope.comparators.binary import File
class Bzip2Container(Archive):
......@@ -34,7 +35,7 @@ class Bzip2Container(Archive):
pass
def get_members(self):
return OrderedDict({'bzip2-content': self.get_member(self.get_member_names()[0])})
return collections.OrderedDict({'bzip2-content': self.get_member(self.get_member_names()[0])})
def get_member_names(self):
return [get_compressed_content_name(self.source.path, '.bz2')]
......
......@@ -19,14 +19,14 @@
import io
import os
import os.path
import re
import subprocess
import struct
import subprocess
from diffoscope import logger, tool_required
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Archive, Command
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Archive, Command
from diffoscope.comparators.binary import File
class CbfsListing(Command):
......
......@@ -19,11 +19,12 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
from diffoscope import tool_required
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Command
from diffoscope.comparators.binary import File
from diffoscope.comparators.libarchive import LibarchiveContainer, list_libarchive
from diffoscope.comparators.utils import Command
from diffoscope.difference import Difference
class CpioContent(Command):
......
......@@ -18,18 +18,20 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
try:
from debian import deb822
except ImportError:
deb822 = None
from diffoscope import logger
from diffoscope.difference import Difference
import diffoscope.comparators
from diffoscope.comparators.tar import TarContainer
from diffoscope.comparators.utils import ArchiveMember
from diffoscope.comparators.binary import File
from diffoscope.comparators.libarchive import LibarchiveContainer, list_libarchive
from diffoscope.comparators.utils import \
ArchiveMember
from diffoscope.comparators.tar import TarContainer
import diffoscope.comparators
try:
from debian import deb822
except ImportError:
deb822 = None
# Return a dict with build ids as keys and file as values for all deb in the
......
......@@ -17,16 +17,18 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
from functools import partial
import hashlib
import os.path
import re
import os.path
import hashlib
import functools
import collections
from debian.deb822 import Dsc
from diffoscope.changes import Changes
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Container
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Container
from diffoscope.comparators.binary import File
DOT_CHANGES_FIELDS = [
......@@ -82,7 +84,7 @@ class DebControlContainer(Container):
return re.compile(re.escape(version))
def get_members(self):
return OrderedDict([(self._trim_version_number(name), self.get_member(name)) for name in self.get_member_names()])
return collections.OrderedDict([(self._trim_version_number(name), self.get_member(name)) for name in self.get_member_names()])
def get_member_names(self):
field = self.source.deb822.get('Files') or self.source.deb822.get('Checksums-Sha256')
......@@ -160,7 +162,7 @@ class DotDscFile(DebControlFile):
if not os.path.exists(in_dsc_path):
return False
with open(in_dsc_path, 'rb') as f:
for buf in iter(partial(f.read, 32768), b''):
for buf in iter(functools.partial(f.read, 32768), b''):
md5.update(buf)
if md5.hexdigest() != d['md5sum']:
return False
......@@ -187,7 +189,7 @@ class DotBuildinfoFile(DebControlFile):
if not os.path.exists(in_buildinfo_path):
return False
with open(in_buildinfo_path, 'rb') as f:
for buf in iter(partial(f.read, 32768), b''):
for buf in iter(functools.partial(f.read, 32768), b''):
sha256.update(buf)
if sha256.hexdigest() != d['sha256']:
return False
......
......@@ -18,8 +18,10 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
from diffoscope.comparators.text import TextFile
class DotChangesFile(TextFile):
RE_FILE_EXTENSION = re.compile(r'\.changes$')
......
......@@ -18,10 +18,11 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import os
from diffoscope.comparators.binary import File, FilesystemFile
from diffoscope.comparators.utils import format_device
from diffoscope import get_named_temporary_file
from diffoscope.difference import Difference
from diffoscope import logger, get_named_temporary_file
from diffoscope.comparators.utils import format_device
from diffoscope.comparators.binary import File, FilesystemFile
class Device(File):
......
......@@ -17,10 +17,11 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
import re
import os.path
import subprocess
import collections
from diffoscope import logger, tool_required
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Archive, get_compressed_content_name
......@@ -38,7 +39,7 @@ class DexContainer(Archive):
pass
def get_members(self):
return OrderedDict({'dex-content': self.get_member(self.get_member_names()[0])})
return collections.OrderedDict({'dex-content': self.get_member(self.get_member_names()[0])})
def get_member_names(self):
return [get_compressed_content_name(self.source.path, '.dex') + '.jar']
......
......@@ -18,14 +18,15 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import os
import os.path
import re
import subprocess
import diffoscope.comparators
from diffoscope import logger, tool_required, RequiredToolNotFound
from diffoscope.difference import Difference
import diffoscope.comparators
from diffoscope.comparators.binary import FilesystemFile
from diffoscope.comparators.utils import Container, Command
from diffoscope.comparators.binary import FilesystemFile
def list_files(path):
......
......@@ -17,17 +17,18 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
import os.path
import re
import os.path
import subprocess
from diffoscope import tool_required, OutputParsingError
from diffoscope import logger, get_named_temporary_file
from diffoscope.comparators.binary import File
from diffoscope.comparators.libarchive import list_libarchive
import collections
from diffoscope import tool_required, OutputParsingError, logger, \
get_named_temporary_file
from diffoscope.difference import Difference
from diffoscope.comparators.deb import DebFile, get_build_id_map
from diffoscope.comparators.utils import Command, Container
from diffoscope.difference import Difference
from diffoscope.comparators.binary import File
from diffoscope.comparators.libarchive import list_libarchive
class Readelf(Command):
......@@ -335,7 +336,7 @@ class ElfContainer(Container):
output = output[2:]
output = output[5:]
self._sections = OrderedDict()
self._sections = collections.OrderedDict()
# Entires of readelf --section-headers have the following columns:
# [Nr] Name Type Address Off Size ES Flg Lk Inf Al
for line in output:
......
......@@ -18,10 +18,11 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
from diffoscope import tool_required
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Command
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Command
from diffoscope.comparators.binary import File
class Showttf(Command):
......
......@@ -17,17 +17,19 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
import re
import os.path
import collections
from diffoscope import logger
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Archive
from diffoscope.comparators.binary import File
try:
import guestfs
except ImportError:
guestfs = None
from diffoscope import logger
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Archive
from diffoscope.difference import Difference
class FsImageContainer(Archive):
......@@ -55,7 +57,7 @@ class FsImageContainer(Archive):
self.g.close()
def get_members(self):
return OrderedDict({'fsimage-content': self.get_member(self.get_member_names()[0])})
return collections.OrderedDict({'fsimage-content': self.get_member(self.get_member_names()[0])})
def get_member_names(self):
return [os.path.basename(self.source.path) + '.tar']
......
......@@ -17,13 +17,14 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from io import BytesIO
import io
import re
from diffoscope import logger
from diffoscope import tool_required
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Command
from diffoscope.difference import Difference
from diffoscope import logger
from diffoscope.comparators.utils import Command
from diffoscope.comparators.binary import File
class Msgunfmt(Command):
......@@ -31,7 +32,7 @@ class Msgunfmt(Command):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._header = BytesIO()
self._header = io.BytesIO()
self._encoding = None
@tool_required('msgunfmt')
......
......@@ -17,13 +17,14 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
from collections import OrderedDict
import re
import subprocess
import os.path
import subprocess
import collections
from diffoscope import logger, tool_required
from diffoscope.comparators.utils import Archive, get_compressed_content_name
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Archive, get_compressed_content_name
class GzipContainer(Archive):
......@@ -34,7 +35,7 @@ class GzipContainer(Archive):
pass
def get_members(self):
return OrderedDict({'gzip-content': self.get_member(self.get_member_names()[0])})
return collections.OrderedDict({'gzip-content': self.get_member(self.get_member_names()[0])})
def get_member_names(self):
return [get_compressed_content_name(self.source.path, '.gz')]
......
......@@ -18,14 +18,14 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
import platform
import struct
import platform
import subprocess
from diffoscope import tool_required
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Command
from diffoscope import tool_required, logger
from diffoscope.difference import Difference
from diffoscope import logger
from diffoscope.comparators.utils import Command
from diffoscope.comparators.binary import File
class ShowIface(Command):
......
......@@ -18,10 +18,11 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
from diffoscope import tool_required
from diffoscope.comparators.binary import File
from diffoscope.comparators.utils import Command
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Command
from diffoscope.comparators.binary import File
class Iccdump(Command):
......
......@@ -19,6 +19,7 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
import re
from diffoscope.comparators.gzip import GzipFile
......
......@@ -19,11 +19,12 @@
import re
import subprocess
from diffoscope import tool_required
from diffoscope.difference import Difference
from diffoscope.comparators.utils import Command
from diffoscope.comparators.binary import File
from diffoscope.comparators.libarchive import LibarchiveContainer
from diffoscope.comparators.utils import Command
from diffoscope.difference import Difference
@tool_required('isoinfo')
......
......@@ -18,12 +18,13 @@
# You should have received a copy of the GNU General Public License