<commit_before>import os
import subprocess

from needle.engines.base import EngineBase


class Engine(EngineBase):
    cmp_path = "cmp"
    cmp_command = "{cmp} {baseline} {new}"
    compare_path = "compare"
    compare_command = ("{compare} -metric RMSE -subimage-search {baseline} "
                       "{new} {diff}")

    def assertSameFiles(self, output_file, baseline_file, threshold=0):
        diff_file = output_file.replace('.png', '.diff.png')

        cmp_cmd = self.cmp_command.format(cmp=self.cmp_path,
                                          baseline=baseline_file,
                                          new=output_file)
        if subprocess.call(cmp_cmd, shell=True) == 0:
            os.remove(output_file)
            # remove a possible earlier diff file
            os.remove(diff_file)
            return

        compare_cmd = self.compare_command.format(
            compare=self.compare_path,
            baseline=baseline_file,
            new=output_file,
            diff=diff_file)
        process = subprocess.Popen(compare_cmd, shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        compare_stdout, compare_stderr = process.communicate()

        if process.returncode != 0:
            raise RuntimeError("{compare} returned a non-zero exit status.\n"
                               "{cmd}\n"
                               "{stdout}{stderr}"
                               .format(compare=self.compare_path,
                                       cmd=compare_cmd,
                                       stdout=compare_stdout,
                                       stderr=compare_stderr))

        difference = float(compare_stderr.split()[1][1:-1])

        if difference <= threshold:
            os.remove(diff_file)
            os.remove(output_file)
            return

        raise AssertionError("The new screenshot '{new}' did not match "
                             "the baseline '{baseline}' (See {diff}):\n"
                             "{stdout}{stderr}"
                             .format(new=output_file,
                                     baseline=baseline_file,
                                     diff=diff_file,
                                     stdout=compare_stdout,
                                     stderr=compare_stderr))<commit_msg>Removed the use of the ``cmd`` command in the ImageMagick engine as we can't guarantee that all users will have it available.<commit_after>import os
import subprocess

from needle.engines.base import EngineBase


class Engine(EngineBase):
    compare_path = "compare"
    compare_command = ("{compare} -metric RMSE -subimage-search {baseline} "
                       "{new} {diff}")

    def assertSameFiles(self, output_file, baseline_file, threshold=0):
        diff_file = output_file.replace('.png', '.diff.png')

        compare_cmd = self.compare_command.format(
            compare=self.compare_path,
            baseline=baseline_file,
            new=output_file,
            diff=diff_file)
        process = subprocess.Popen(compare_cmd, shell=True,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        compare_stdout, compare_stderr = process.communicate()

        difference = float(compare_stderr.split()[1][1:-1])
        if difference <= threshold:
            os.remove(diff_file)
            return

        raise AssertionError("The new screenshot '{new}' did not match "
                             "the baseline '{baseline}' (See {diff}):\n"
                             "{stdout}{stderr}"
                             .format(new=output_file,
                                     baseline=baseline_file,
                                     diff=diff_file,
                                     stdout=compare_stdout,
                                     stderr=compare_stderr))
{'fraction_non_alphanumeric': 0.08818342151675485, 'fraction_numerical': 0.0025195263290501385, 'mean_word_length': 1.4355828220858895, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5698325', 'n_tokens_mistral': 987, 'n_tokens_neox': 934, 'n_words': 196}