1
  5   ˆl–Ým_JJi?u8 _¸î6ê˜´o�þB gDÑ^‘§Z/óÅƒh¹ÄÂÁ      1
  L   	ˆl–Ým_JJi?u8 _¸'n61hß�þB gDëâDëŸÏÆƒh¹Åa–Ým_JJi?u8 _¸È®	•1hßÃ      	1
  5   ˆl–ß=¿Jõ4Ÿº‚ œP.\·˜´o�þB g Ùm–D7óÈƒh¹Ç¿Ä      00000048
  6   ˆl–äY>”Ên-ji@¿pB¸ÈÅ£�þZ	³‚�Æ1	çÞóÉ„�2×ÈÀÅ ?÷     #!/usr/bin/python3
#
# Copyright (C) 2010-2018, Benjamin Drung <bdrung@debian.org>
#               2010, Stefano Rivera <stefanor@ubuntu.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

# pylint: disable=invalid-name
# pylint: enable=invalid-name

import argparse
import glob
import operator
import os
import re
import sys

from devscripts.control import (
    HAS_FULL_RTS_FORMATTING,
    HAS_RTS_PARSER,
    Control,
    wrap_and_sort_formatter,
)

try:
    from debian._deb822_repro import LIST_COMMA_SEPARATED_INTERPRETATION
except ImportError:
    LIST_COMMA_SEPARATED_INTERPRETATION = object()


CONTROL_LIST_FIELDS = (
    "Breaks",
    "Build-Conflicts",
    "Build-Conflicts-Arch",
    "Build-Conflicts-Indep",
    "Build-Depends",
    "Build-Depends-Arch",
    "Build-Depends-Indep",
    "Built-Using",
    "Conflicts",
    "Depends",
    "Enhances",
    "Pre-Depends",
    "Provides",
    "Recommends",
    "Replaces",
    "Suggests",
    "Xb-Npp-MimeType",
)

SUPPORTED_FILES = (
    "clean",
    "control",
    "control*.in",
    "copyright",
    "copyright.in",
    "dirs",
    "*.dirs",
    "docs",
    "*.docs",
    "examples",
    "*.examples",
    "info",
    "*.info",
    "install",
    "*.install",
    "links",
    "*.links",
    "mainscript",
    "*.maintscript",
    "manpages",
    "*.manpages",
    "tests/control",
)


def erase_and_write(file_ob, data):
    """When a file is opened via r+ mode, replaces its content with data"""
    file_ob.seek(0)
    file_ob.write(data)
    file_ob.truncate()


class WrapAndSortControl(Control):
    def __init__(self, filename, args):
        # The Control module supports the RTS parser with python-debian 0.1.43.
        # However, the `wrap_and_sort_formatter` requires 0.1.44.  The command line
        # option check handles the compatibility check for now.
        super().__init__(filename, use_rts_parser=args.rts_parser)
        self.args = args
        self._formatter = None
        if args.rts_parser and HAS_FULL_RTS_FORMATTING:
            max_line_length = args.max_line_length
            self._formatter = wrap_and_sort_formatter(
                1 if args.short_indent else "FIELD_NAME_LENGTH",
                trailing_separator=args.trailing_comma,
                immediate_empty_line=args.short_indent,
                max_line_length_one_liner=0 if args.wrap_always else max_line_length,
            )

    def wrap_and_sort(self):
        for paragraph in self.paragraphs:
            for field in CONTROL_LIST_FIELDS:
                if field in paragraph:
                    self._wrap_field(paragraph, field, True)
            if "Uploaders" in paragraph:
                self._wrap_field(paragraph, "Uploaders", False)
            if "Architecture" in paragraph:
                archs = set(paragraph["Architecture"].split())
                # Sort, with wildcard entries (such as linux-any) first:
                archs = sorted(archs, key=lambda x: ("any" not in x, x))
                paragraph["Architecture"] = " ".join(archs)

        if self.args.sort_binary_packages:
            if self.file