This file is indexed.

/usr/bin/wrap-and-sort is in devscripts 2.17.12ubuntu1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#!/usr/bin/python3
#
# Copyright (C) 2010-2016, 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.

import glob
import operator
import optparse
import os
import re
import sys

from devscripts.control import Control

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",
)


class WrapAndSortControl(Control):
    def __init__(self, filename, max_line_length):
        super().__init__(filename)
        self.max_line_length = max_line_length

    def wrap_and_sort(self, wrap_always, short_indent, sort_paragraphs,
                      keep_first, trailing_comma):
        for paragraph in self.paragraphs:
            for field in CONTROL_LIST_FIELDS:
                if field in paragraph:
                    self._wrap_field(paragraph, field, wrap_always,
                                     short_indent, trailing_comma)
            if "Uploaders" in paragraph:
                self._wrap_field(paragraph, "Uploaders", wrap_always,
                                 short_indent, trailing_comma, 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 sort_paragraphs:
            first = self.paragraphs[:1 + int(keep_first)]
            sortable = self.paragraphs[1 + int(keep_first):]
            self.paragraphs = first + sorted(sortable, key=operator.itemgetter("Package"))

    def _wrap_field(self, control, entry, wrap_always, short_indent,
                    trailing_comma, sort=True):
        # An empty element is not explicitly disallowed by Policy but known to
        # break QA tools, so remove any
        packages = [x.strip() for x in control[entry].split(",") if x.strip()]

        # Sanitize alternative packages. E.g. "a|b  |c" -> "a | b | c"
        packages = [" | ".join([x.strip() for x in p.split("|")]) for p in packages]

        if sort:
            # Remove duplicate entries
            packages = set(packages)
            packages = sort_list(packages)

        length = len(entry) + sum([2 + len(package) for package in packages])
        if wrap_always or length > self.max_line_length:
            indentation = " "
            if not short_indent:
                indentation *= len(entry) + 2
            packages_with_indention = [indentation + x for x in packages]
            packages_with_indention = ",\n".join(packages_with_indention)
            if trailing_comma:
                packages_with_indention += ','
            if short_indent:
                control[entry] = "\n" + packages_with_indention
            else:
                control[entry] = packages_with_indention.strip()
        else:
            control[entry] = ", ".join(packages).strip()


class Install(object):
    def __init__(self, filename):
        self.content = None
        self.filename = None
        self.open(filename)

    def open(self, filename):
        assert os.path.isfile(filename), "%s does not exist." % (filename)
        self.filename = filename
        self.content = [l.strip() for l in open(filename).readlines() if l.strip()]

    def save(self, filename=None):
        if filename:
            self.filename = filename
        install_file = open(self.filename, "w")
        install_file.write("\n".join(self.content) + "\n")
        install_file.close()

    def sort(self):
        self.content = sorted(self.content)


def remove_trailing_whitespaces(filename):
    assert os.path.isfile(filename), "%s does not exist." % (filename)
    content = open(filename).read()
    if len(content) == 0:
        return
    content = content.rstrip() + "\n"
    lines = content.split("\n")
    lines = [l.rstrip() for l in lines]
    new_content = "\n".join(lines)
    f = open(filename, "w")
    f.write(new_content)
    f.close()


def sort_list(unsorted_list):
    packages = [x for x in unsorted_list if re.match("[a-z0-9]", x)]
    special = [x for x in unsorted_list if not re.match("[a-z0-9]", x)]
    return sorted(packages) + sorted(special)


def wrap_and_sort(options):
    control_files = [f for f in options.files if re.search("/control[^/]*$", f)]
    for control_file in control_files:
        if options.verbose:
            print(control_file)
        control = WrapAndSortControl(control_file, options.max_line_length)
        if options.cleanup:
            control.strip_trailing_spaces()
        control.wrap_and_sort(options.wrap_always, options.short_indent,
                              options.sort_binary_packages, options.keep_first,
                              options.trailing_comma)
        control.save()

    copyright_files = [f for f in options.files
                       if re.search("/copyright[^/]*$", f)]
    for copyright_file in copyright_files:
        if options.verbose:
            print(copyright_file)
        remove_trailing_whitespaces(copyright_file)

    pattern = "(dirs|docs|examples|info|install|links|maintscript|manpages)$"
    install_files = [f for f in options.files if re.search(pattern, f)]
    for install_file in sorted(install_files):
        if options.verbose:
            print(install_file)
        install = Install(install_file)
        install.sort()
        install.save()


def get_files(debian_directory):
    """Returns a list of files that should be wrapped and sorted."""
    files = []
    for supported_files in SUPPORTED_FILES:
        file_pattern = os.path.join(debian_directory, supported_files)
        files.extend(file_name for file_name in glob.glob(file_pattern)
                     if not os.access(file_name, os.X_OK))
    return files


def main():
    script_name = os.path.basename(sys.argv[0])
    usage = "%s [options]" % (script_name)
    epilog = "See %s(1) for more info." % (script_name)
    parser = optparse.OptionParser(usage=usage, epilog=epilog)

    parser.add_option("-a", "--wrap-always", action="store_true", default=False,
                      help="wrap lists even if they do not exceed the line length limit")
    parser.add_option("-s", "--short-indent", dest="short_indent",
                      help="only indent wrapped lines by one space (default is "
                           "in-line with the field name)",
                      action="store_true", default=False)
    parser.add_option("-b", "--sort-binary-packages",
                      help="Sort binary package paragraphs by name",
                      dest="sort_binary_packages", action="store_true",
                      default=False)
    parser.add_option("-k", "--keep-first",
                      help="When sorting binary package paragraphs, leave the "
                           "first one at the top. Unqualified debhelper "
                           "configuration files are applied to the first "
                           "package.",
                      dest="keep_first", action="store_true", default=False)
    parser.add_option("-n", "--no-cleanup", help="don't cleanup whitespaces",
                      dest="cleanup", action="store_false", default=True)
    parser.add_option("-t", "--trailing-comma", help="add trailing comma",
                      dest="trailing_comma", action="store_true",
                      default=False)
    parser.add_option("-d", "--debian-directory", dest="debian_directory",
                      help="location of the 'debian' directory (default: "
                           "./debian)", metavar="PATH", default="debian")
    parser.add_option("-f", "--file", metavar="FILE",
                      dest="files", action="append", default=list(),
                      help="Wrap and sort only the specified file.")
    parser.add_option("-v", "--verbose",
                      help="print all files that are touched",
                      dest="verbose", action="store_true", default=False)
    parser.add_option("--max-line-length", type='int', default=79,
                      help="set maximum allowed line length before wrapping (default: %default)")

    (options, args) = parser.parse_args()

    if len(args) != 0:
        parser.error("Unsupported additional parameters specified: %s" %
                     ", ".join(args))

    if not os.path.isdir(options.debian_directory):
        parser.error('Debian directory not found, expecting "%s".' %
                     options.debian_directory)

    not_found = [f for f in options.files if not os.path.isfile(f)]
    if not_found:
        parser.error('Specified files not found: %s' % ", ".join(not_found))

    if not options.files:
        options.files = get_files(options.debian_directory)

    wrap_and_sort(options)


if __name__ == "__main__":
    main()