Commit 4e71b824 authored by Chris Lamb's avatar Chris Lamb 💬

diffoscope.__init__: Split out all remaining helper methods

Signed-off-by: Chris Lamb's avatarChris Lamb <lamby@debian.org>
parent e42bedfd
......@@ -17,80 +17,4 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import os
import shutil
import logging
import platform
import functools
import time
from distutils.spawn import find_executable
from diffoscope.profiling import profile
VERSION = "66"
logger = logging.getLogger("diffoscope")
logger.setLevel(logging.WARNING)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
formatter = logging.Formatter('%(created).3f %(levelname).1s: %(message)s')
ch.setFormatter(formatter)
OS_NAMES = {
'arch': 'Arch Linux',
'debian': 'Debian',
'FreeBSD': 'FreeBSD',
}
def get_current_os():
system = platform.system()
if system == "Linux":
# FIXME: Will break under Python 3.7, see:
# https://docs.python.org/3/library/platform.html#platform.linux_distribution
return platform.linux_distribution()[0]
return system
# Memoize calls to ``distutils.spawn.find_executable`` to avoid excessive stat
# calls
find_executable = functools.lru_cache()(find_executable)
def tool_required(command):
"""
Decorator that checks if the specified tool is installed
"""
if not hasattr(tool_required, 'all'):
tool_required.all = set()
tool_required.all.add(command)
def wrapper(original_function):
if find_executable(command):
@functools.wraps(original_function)
def tool_check(*args, **kwargs):
with profile('command', command):
return original_function(*args, **kwargs)
else:
@functools.wraps(original_function)
def tool_check(*args, **kwargs):
from .exc import RequiredToolNotFound
raise RequiredToolNotFound(command)
return tool_check
return wrapper
def set_locale():
"""Normalize locale so external tool gives us stable and properly
encoded output"""
for var in ['LANGUAGE', 'LC_ALL']:
if var in os.environ:
del os.environ[var]
for var in ['LANG', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY',
'LC_MESSAGES', 'LC_PAPER', 'LC_NAME', 'LC_ADDRESS',
'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION']:
os.environ[var] = 'C'
os.environ['LC_CTYPE'] = 'C.UTF-8'
os.environ['TZ'] = 'UTC'
time.tzset()
......@@ -47,7 +47,8 @@ import subprocess
from debian import deb822
from diffoscope import logger, tool_required
from .tools import tool_required
from .logging import logger
class ChangesFileException(Exception):
......
......@@ -21,7 +21,8 @@ import re
import os.path
import subprocess
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.tempfiles import get_temporary_directory
from .binary import File
......
......@@ -20,7 +20,8 @@
import re
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .binary import File
......
......@@ -22,7 +22,8 @@ import os.path
import subprocess
import collections
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from .utils.file import File
from .utils.archive import Archive
......
......@@ -23,7 +23,8 @@ import re
import struct
import subprocess
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .binary import File
......
......@@ -20,7 +20,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -19,7 +19,7 @@
import re
from diffoscope import logger
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .tar import TarContainer
......
......@@ -25,7 +25,7 @@ import collections
from debian.deb822 import Dsc
from diffoscope import logger
from diffoscope.logging import logger
from diffoscope.changes import Changes
from diffoscope.difference import Difference
......
......@@ -22,7 +22,8 @@ import os.path
import subprocess
import collections
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from .binary import File
from .utils.archive import Archive
......
......@@ -21,8 +21,9 @@ import os
import re
import subprocess
from diffoscope import logger, tool_required
from diffoscope.exc import RequiredToolNotFound
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.progress import Progress
from diffoscope.difference import Difference
......
......@@ -22,8 +22,9 @@ import os.path
import subprocess
import collections
from diffoscope import tool_required, logger
from diffoscope.exc import OutputParsingError
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.tempfiles import get_named_temporary_file
from diffoscope.difference import Difference
......
......@@ -19,7 +19,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -21,7 +21,7 @@ import re
import os.path
import collections
from diffoscope import logger
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .binary import File
......
......@@ -20,8 +20,8 @@
import io
import re
from diffoscope import logger
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .binary import File
......
......@@ -22,7 +22,8 @@ import os.path
import subprocess
import collections
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .utils.archive import Archive
......
......@@ -22,7 +22,8 @@ import struct
import platform
import subprocess
from diffoscope import tool_required, logger
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.profiling import profile
from diffoscope.difference import Difference
......
......@@ -19,7 +19,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -19,7 +19,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -20,7 +20,7 @@
import re
import subprocess
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -21,7 +21,7 @@
import re
import os.path
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -19,7 +19,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .utils.file import File
......
......@@ -18,7 +18,7 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -21,7 +21,7 @@
import re
import subprocess
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -19,8 +19,8 @@
import os
from diffoscope import logger
from diffoscope.config import Config
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .binary import FilesystemFile
......
......@@ -20,7 +20,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -19,7 +19,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -19,7 +19,7 @@
import re
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -20,7 +20,7 @@
import re
import functools
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -23,7 +23,8 @@ import os
import re
import subprocess
from diffoscope import tool_required, logger
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.profiling import profile
from diffoscope.difference import Difference
......
......@@ -19,8 +19,9 @@
import re
from diffoscope import tool_required, logger
from diffoscope.exc import RequiredToolNotFound
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .text import TextFile
......
......@@ -24,7 +24,8 @@ import os.path
import binascii
import subprocess
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.tempfiles import get_temporary_directory
from diffoscope.difference import Difference
......
......@@ -22,7 +22,8 @@ import zlib
import os.path
import collections
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .utils.archive import Archive
......
......@@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -23,7 +23,8 @@ import stat
import subprocess
import collections
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import Difference
from .binary import File
......
......@@ -19,7 +19,7 @@
import os
from diffoscope import logger
from diffoscope.logging import logger
from diffoscope.tempfiles import get_named_temporary_file
from diffoscope.difference import Difference
......
......@@ -19,7 +19,7 @@
import abc
from diffoscope import logger
from diffoscope.logging import logger
from diffoscope.profiling import profile
from diffoscope.tempfiles import get_temporary_directory
......
......@@ -22,7 +22,7 @@ import abc
import subprocess
import threading
from diffoscope import logger
from diffoscope.logging import logger
class Command(object, metaclass=abc.ABCMeta):
......
......@@ -22,7 +22,8 @@ import os
import sys
import binascii
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.exc import RequiredToolNotFound
from diffoscope.config import Config
from diffoscope.profiling import profile
......
......@@ -21,8 +21,8 @@ import abc
import itertools
import collections
from diffoscope import logger
from diffoscope.config import Config
from diffoscope.logging import logger
from diffoscope.progress import Progress
from ..missing_file import MissingFile
......
......@@ -23,8 +23,9 @@ import abc
import magic
import subprocess
from diffoscope import tool_required, logger
from diffoscope.exc import RequiredToolNotFound, OutputParsingError
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.profiling import profile
from diffoscope.difference import Difference
......
......@@ -19,8 +19,8 @@
import operator
from diffoscope import logger
from diffoscope.config import Config
from diffoscope.logging import logger
try:
import tlsh
......
......@@ -23,7 +23,7 @@ import os.path
import ctypes
import libarchive
from diffoscope import logger
from diffoscope.logging import logger
from diffoscope.tempfiles import get_temporary_directory
from ..device import Device
......
......@@ -19,7 +19,7 @@
import importlib
from diffoscope import logger
from diffoscope.logging import logger
from diffoscope.profiling import profile
from .. import COMPARATORS
......
......@@ -22,7 +22,8 @@ import os.path
import subprocess
import collections
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from .binary import File
from .utils.archive import Archive
......
......@@ -24,7 +24,7 @@ import os.path
import zipfile
import contextlib
from diffoscope import tool_required
from diffoscope.tools import tool_required
from diffoscope.difference import Difference
from .binary import File
......
......@@ -29,10 +29,11 @@ import tempfile
from multiprocessing.dummy import Queue
from diffoscope import logger, tool_required
from diffoscope.exc import RequiredToolNotFound
from diffoscope.config import Config
from diffoscope.profiling import profile
from .exc import RequiredToolNotFound
from .tools import tool_required
from .config import Config
from .logging import logger
from .profiling import profile
class DiffParser(object):
......
......@@ -18,6 +18,9 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
from .tools import get_current_os
class OutputParsingError(Exception):
def __init__(self, command, object):
self.command = command
......@@ -213,7 +216,6 @@ class RequiredToolNotFound(Exception):
self.command = command
def get_package(self):
from . import get_current_os
try:
providers = RequiredToolNotFound.PROVIDERS[self.command]
......
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2016 Chris Lamb <lamby@debian.org>
#
# 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
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import os
import time
def set_locale():
"""Normalize locale so external tool gives us stable and properly
encoded output"""
for var in ['LANGUAGE', 'LC_ALL']:
if var in os.environ:
del os.environ[var]
for var in ['LANG', 'LC_NUMERIC', 'LC_TIME', 'LC_COLLATE', 'LC_MONETARY',
'LC_MESSAGES', 'LC_PAPER', 'LC_NAME', 'LC_ADDRESS',
'LC_TELEPHONE', 'LC_MEASUREMENT', 'LC_IDENTIFICATION']:
os.environ[var] = 'C'
os.environ['LC_CTYPE'] = 'C.UTF-8'
os.environ['TZ'] = 'UTC'
time.tzset()
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2016 Chris Lamb <lamby@debian.org>
#
# 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
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import logging
logger = logging.getLogger("diffoscope")
logger.setLevel(logging.WARNING)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
logger.addHandler(ch)
formatter = logging.Formatter('%(created).3f %(levelname).1s: %(message)s')
ch.setFormatter(formatter)
......@@ -28,17 +28,20 @@ import argparse
import traceback
import contextlib
from diffoscope import logger, VERSION, set_locale
from diffoscope.exc import RequiredToolNotFound
from diffoscope.config import Config
from diffoscope.progress import ProgressManager, Progress
from diffoscope.tempfiles import clean_all_temp_files
from diffoscope.profiling import ProfileManager, profile
from diffoscope.difference import Difference
from diffoscope.presenters.html import output_html, output_html_directory, \
from . import VERSION
from .exc import RequiredToolNotFound
from .tools import tool_required, OS_NAMES, get_current_os
from .config import Config
from .locale import set_locale
from .logging import logger
from .progress import ProgressManager, Progress
from .tempfiles import clean_all_temp_files
from .profiling import ProfileManager, profile
from .difference import Difference
from .presenters.html import output_html, output_html_directory, \
JQUERY_SYSTEM_LOCATIONS
from diffoscope.presenters.text import output_text
from diffoscope.comparators.utils.compare import compare_root_paths
from .presenters.text import output_text
from .comparators.utils.compare import compare_root_paths
try:
import tlsh
......@@ -192,8 +195,6 @@ class RangeCompleter(object):
class ListToolsAction(argparse.Action):
def __call__(self, parser, namespace, os_override, option_string=None):
from diffoscope import tool_required, OS_NAMES, get_current_os
print("External-Tools-Required: ", end='')
print(', '.join(sorted(tool_required.all)))
if os_override:
......
......@@ -40,9 +40,11 @@ import codecs
import hashlib
import contextlib
from diffoscope import logger, VERSION
from diffoscope.config import Config
from diffoscope.presenters.icon import FAVICON_BASE64
from .. import VERSION
from ..config import Config
from ..logging import logger
from .icon import FAVICON_BASE64
# minimum line size, we add a zero-sized breakable space every
......
......@@ -21,7 +21,8 @@
import subprocess
import sys
from diffoscope import logger, tool_required
from diffoscope.tools import tool_required
from diffoscope.logging import logger
from diffoscope.difference import color_unified_diff
......
......@@ -20,7 +20,7 @@
import os
import tempfile
from diffoscope import logger
from .logging import logger
_DIRS, _FILES = [], []
......