mail_deduplicate.bug_report moduleΒΆ

Bug reports for the errors mdedup has no rule for.

Such an error is a bug in mdedup, or a mail shape it does not handle yet. To fix it, a maintainer needs two facts: the code that raised the error, and the shape of the mail that triggered it. This module collects both, and hands them to click_extra.log_report(), which renders them as a block that a user can paste into an issue, and saves it in the cache directory.

Caution

A report never holds mail content. Mails carry the personal data of their senders and recipients, and issues are public. So fingerprint() describes a mail by its structure alone. The traceback keeps module names and line numbers, and drops the file paths, which hold the name of the user on most systems.

mail_deduplicate.bug_report.ISSUES_URL = 'https://github.com/kdeldycke/mail-deduplicate/issues'ΒΆ

Where users file bug reports.

Repeats the Issues URL of pyproject.toml, and a test keeps the two in sync: a binary built by Nuitka ships no package metadata to read it from.

mail_deduplicate.bug_report.REPORT_FORM = 'bug-report.yml'ΒΆ

File name of the issue form a report links to, in .github/ISSUE_TEMPLATE/.

mail_deduplicate.bug_report.REPORT_URL = 'https://github.com/kdeldycke/mail-deduplicate/issues/new?template=bug-report.yml'ΒΆ

The report_url of the mdedup command: the bug report form, to which click_extra.log_report() adds the title of the issue.

mail_deduplicate.bug_report.FAILURE_ATTRIBUTE = 'mdedup_failure'ΒΆ

Attribute of an exception that holds the Failure attached by blame().

mail_deduplicate.bug_report.FINGERPRINTED_MAILS = 2ΒΆ

Mails of one failure that get a fingerprint.

A failing duplicate set does not tell which of its mails caused the error, and its first two mails usually include the culprit.

mail_deduplicate.bug_report.MAX_FINGERPRINT_LINES = 60ΒΆ

Cap on the lines of a fingerprint, for mails with hundreds of headers or parts.

mail_deduplicate.bug_report.MAX_KINDS = 5ΒΆ

Kinds of failure detailed in a report. The warnings of the run name the others.

mail_deduplicate.bug_report.MAX_VALUE_LENGTH = 200ΒΆ

Cap on the characters of a header value shown verbatim.

mail_deduplicate.bug_report.PARAMETER_HEADERS = frozenset({'content-disposition', 'content-type'})ΒΆ

Headers a fingerprint shows with their structure only: see structure().

mail_deduplicate.bug_report.SAFE_PARAMETERS = frozenset({'charset', 'delsp', 'format', 'micalg', 'protocol', 'report-type', 'type'})ΒΆ

Parameters of PARAMETER_HEADERS shown as they are: they tell how to read a part.

structure() names the SHAPED_PARAMETERS without their value, and hides the name of any other parameter too.

mail_deduplicate.bug_report.SAMPLES = 3ΒΆ

Failures of each kind that a report details, with the fingerprint of their mails.

More samples of the same bug add length to a report, not information, and each fingerprint costs one more read and parse of its mail.

mail_deduplicate.bug_report.SHAPED_PARAMETERS = frozenset({'boundary', 'filename', 'name'})ΒΆ

Parameters of PARAMETER_HEADERS a fingerprint names, with their shape alone.

They can hold content: a file name, or a boundary built from a sender address.

mail_deduplicate.bug_report.VERBATIM_HEADERS = frozenset({'content-transfer-encoding', 'date', 'mime-version'})ΒΆ

Headers a fingerprint shows as they are: they hold structure, not content.

Their comments are free text, so mask_comments() shows each one by its shape. Date caused several of the past crashes, by its parsing or by the bytes of its comment. See: https://github.com/kdeldycke/mail-deduplicate/issues/600, https://github.com/kdeldycke/mail-deduplicate/issues/954 and https://github.com/kdeldycke/mail-deduplicate/issues/1117

mail_deduplicate.bug_report.CONTROL_BYTES = re.compile(b'[\\x00-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]')ΒΆ

C0 control bytes and DEL, except the tab and the line breaks of a mail.

mail_deduplicate.bug_report.ENCODED_WORD = re.compile('=\\?([^?\\s]+)\\?([bBqQ])\\?')ΒΆ

Start of an RFC 2047 encoded word: its charset and its encoding.

mail_deduplicate.bug_report.TOKEN = re.compile("[!#$%&'*+.^_`|~0-9A-Za-z-]+(?:/[!#$%&'*+.^_`|~0-9A-Za-z-]+)?")ΒΆ

An RFC 2045 token, or a type/subtype pair of them.

mail_deduplicate.bug_report.plural(count, singular, several=None)[source]ΒΆ

Count things in words, or say nothing about a count of zero.

Return type:

str

mail_deduplicate.bug_report.to_bytes(text)[source]ΒΆ

Recover the bytes a parsed value came from.

Parsing a mail from bytes keeps each non-ASCII byte as a lone surrogate, which surrogateescape turns back into that byte. Other surrogates come from no byte, so surrogatepass writes them as the invalid UTF-8 they are.

Return type:

bytes

mail_deduplicate.bug_report.byte_class(value)[source]ΒΆ

Describe a value by its length and the kind of its bytes, never by its bytes.

Return type:

str

mail_deduplicate.bug_report.shape(value)[source]ΒΆ

Describe a value by its bytes, its folding and its encoded words.

Return type:

str

mail_deduplicate.bug_report.verbatim(value)[source]ΒΆ

Show a value as it is, with its folding collapsed and its length capped.

A value holding non-ASCII bytes renders as bytes, so each byte shows as an escape rather than as a lone surrogate.

Return type:

str

mail_deduplicate.bug_report.mask_comments(value)[source]ΒΆ

Replace the text of each comment of a header value by its shape.

A comment is free text in parentheses, which RFC 5322 allows between the tokens of a structured header. Some mail clients write the name of their time zone there, after a Date, and some their own name, after MIME-Version. A comment can hold others, and a backslash quotes the character after it. A comment left open runs to the end of the value.

Return type:

str

mail_deduplicate.bug_report.split_parameters(value)[source]ΒΆ

Split a header value on its semicolons, except the ones in a quoted string.

Return type:

list[str]

mail_deduplicate.bug_report.structure(value)[source]ΒΆ

Show a Content-Type or Content-Disposition value by its structure alone.

The leading type/subtype or disposition shows as it is, and so do the SAFE_PARAMETERS. The SHAPED_PARAMETERS keep their name, and any other parameter shows as ?, all of them with the shape of their value only.

Return type:

str

mail_deduplicate.bug_report.header_value(name, value)[source]ΒΆ

Show a header value as its name allows: verbatim, by structure, or by shape.

A verbatim value still shows its comments by their shape.

Return type:

str

mail_deduplicate.bug_report.line_endings(raw)[source]ΒΆ

Name the line endings of a mail, with their counts when they are mixed.

Return type:

str

mail_deduplicate.bug_report.first_raw(part, name)[source]ΒΆ

The raw value of the first header of a part with that name.

A parsed header holding 8-bit bytes reads back as a Header object, and its raw value as a string with lone surrogates, which to_bytes() turns back to bytes.

Return type:

str | None

mail_deduplicate.bug_report.part_lines(part, depth=0)[source]ΒΆ

One line per MIME part, with the subparts of a multipart indented under it.

Return type:

Iterator[str]

mail_deduplicate.bug_report.fingerprint(raw)[source]ΒΆ

Describe a mail by its structure, with none of its content.

The description shows the size and line endings of the mail, then every header name, in order. Headers in VERBATIM_HEADERS show their value but for their comments, those in PARAMETER_HEADERS their structure, and any other one only the shape of its value: length, kind of bytes, folding and encoded words. Then comes the MIME tree, with the type, charset, transfer encoding and kind of bytes of each part, the preamble and epilogue of each multipart, and the defects the parser found.

Never raises: a mail too broken to describe gets a line saying so.

Return type:

str

mail_deduplicate.bug_report.fingerprint_mail(mail)[source]ΒΆ

The fingerprint() of a mail, read again from its source.

Never raises: a mail that cannot be read again gets a line saying so.

Return type:

str

mail_deduplicate.bug_report.box_format_of(item)[source]ΒΆ

The format of a box, or of the box a mail comes from.

Return type:

str

class mail_deduplicate.bug_report.Failure(step, error, frames, function='', culprits=(), formats=(), fingerprints=(), trace='')[source]ΒΆ

Bases: object

An error mdedup has no rule for, reduced to strings.

Strings can leave a worker process, and keep nothing alive: the mails, boxes and frames of the error are dropped as soon as it is captured.

step: strΒΆ

What the run was doing: hashing, selecting, deleting, and so on.

error: strΒΆ

The class and message of the exception.

frames: tuple[str, ...]ΒΆ

The traceback, as click_extra.compact_frames() renders it.

function: str = ''ΒΆ

The innermost function of mdedup the error went through, as click_extra.culprit_function() finds it.

culprits: tuple[str, ...] = ()ΒΆ

The repr() of each mail involved. They hold paths, so they never reach the report block.

formats: tuple[str, ...] = ()ΒΆ

The box format of each mail involved.

fingerprints: tuple[str, ...] = ()ΒΆ

The fingerprint of the first mails involved, or nothing for a failure that is not a sample.

trace: str = ''ΒΆ

The full traceback, for the debug log, or nothing for a failure that is not a sample.

property kind: tuple[str, str, str]ΒΆ

Failures of one kind share their step, exception class and raising line.

property title: strΒΆ

The title of an issue reporting the failure.

It names the exception class, the innermost function of mdedup in the traceback and the step. Reports of the same bug get the same title, so GitHub lists the issue already open for it while the next user types theirs. The function tells apart two bugs raising the same class in the same step, and unlike a line number, it does not change between releases.

mail_deduplicate.bug_report.capture(expt, step, mails=(), sample=True)[source]ΒΆ

Reduce an error to a Failure.

Only a sample gets the fingerprints of its first mails and the full traceback: both cost far more than the rest.

Return type:

Failure

mail_deduplicate.bug_report.failure_lines(samples, title)[source]ΒΆ

The lines of a report detailing one kind of failure.

Return type:

Iterator[str]

class mail_deduplicate.bug_report.FailureLog[source]ΒΆ

Bases: object

The failures of a run: each one counted, the first SAMPLES of a kind kept.

Memory stays flat however many mails fail the same way, which a single bug can make all of them do.

counts: Counter[tuple[str, str, str]]ΒΆ

Number of failures of each kind.

samples: dict[tuple[str, str, str], list[Failure]]ΒΆ

The first failures of each kind, in the order they happened.

capture(expt, step, mails=())[source]ΒΆ

Reduce an error to a Failure, sampled while its kind has room left.

Return type:

Failure

add(failure)[source]ΒΆ

Count a failure, and keep it if its kind still has room for a sample.

Return type:

None

headline()[source]ΒΆ

The first line of the report, saying how much the failures set aside.

It also says that the run continued, as the word error reads to some users as a crash that stopped everything.

Return type:

str

title()[source]ΒΆ

The title of the issue: that of the most frequent kind of failure.

Return type:

str

traces()[source]ΒΆ

The full traceback of each sample, for the debug log.

Return type:

Iterator[str]

report_lines()[source]ΒΆ

The report lines detailing each kind of failure, most frequent first.

Return type:

Iterator[str]

mail_deduplicate.bug_report.log_report(level, headline, title, details, sources)[source]ΒΆ

Log a bug report with click_extra.log_report(), the sources listed first.

The format of each source tells which code of mailbox read it, which the options of the run do not say.

Return type:

None

mail_deduplicate.bug_report.blame(mail, step)[source]ΒΆ

Attach the mail being processed to any error escaping the block.

The error goes on to stop the run, and report_unexpected_errors() names the mail. It also fingerprints the mail in the bug report, except for an OSError, which gets no bug report.

Return type:

Generator[None, None, None]

mail_deduplicate.bug_report.report_unexpected_errors(dedup)[source]ΒΆ

End the run with a one-line error or a bug report, whatever escapes the block.

The exceptions of Click pass through: they are how a command exits, or refuses an input. An OSError comes from the system, like a missing permission or a full disk, so the user can fix it: it ends the run with a one-line error. Any other error is a bug. Its traceback is logged with the report inviting the user to file it, and the run exits with code 1.

Return type:

Generator[None, None, None]