Commit f784d2cd authored by Chris Lamb's avatar Chris Lamb 👀
Browse files

Split out command-line formatting into a separate utility function.

parent cea78e64
Loading
Loading
Loading
Loading
+5 −10
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import logging
import shlex
import subprocess

from ...utils import format_cmdline

logger = logging.getLogger(__name__)


@@ -65,16 +67,9 @@ class Command(metaclass=abc.ABCMeta):
    def cmdline(self):
        raise NotImplementedError()

    def shell_cmdline(self):
        def fn(x):
            if x == self.path:
                return '{}'
            x = repr(x)
            if ' ' not in x:
                x = x[1:-1]
            return x

        return ' '.join(fn(x) for x in self.cmdline())
    def shell_cmdline(self, *args, **kwargs):
        kwargs.setdefault('replace', (self.path,))
        return format_cmdline(self.cmdline(), *args, **kwargs)

    def env(self):
        return None  # inherit parent environment by default

diffoscope/utils.py

0 → 100644
+30 −0
Original line number Diff line number Diff line
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2019 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/>.


def format_cmdline(cmd, replace=()):
    def fn(x):
        if x in replace:
            return '{}'
        x = repr(x)
        if ' ' not in x:
            x = x[1:-1]
        return x

    return ' '.join(fn(x) for x in cmd)