ubuntu-dev-tools/wrap-and-sort

200 lines
7.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/python
#
2010-12-19 01:47:18 +02:00
# Copyright (C) 2010, Benjamin Drung <bdrung@ubuntu.com>,
# 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 optparse
import os
import os.path
import sys
from ubuntutools.control import Control
CONTROL_LIST_FIELDS = (
2010-11-24 18:34:20 +02:00
"Breaks",
"Build-Depends",
"Build-Depends-Indep",
"Conflicts",
"Depends",
"Enhances",
"Provides",
"Recommends",
"Replaces",
"Suggests",
"Xb-Npp-MimeType",
)
class WrapAndSortControl(Control):
def wrap_and_sort(self, wrap_always, short_indent, sort_paragraphs,
keep_first):
2010-11-24 18:34:20 +02:00
for paragraph in self.paragraphs:
for field in CONTROL_LIST_FIELDS:
if field in paragraph:
self._wrap_field(paragraph, field, wrap_always,
short_indent)
2010-11-24 18:34:20 +02:00
if "Uploaders" in paragraph:
self._wrap_field(paragraph, "Uploaders", wrap_always,
short_indent, False)
2010-11-26 18:03:28 +02:00
if "Architecture" in paragraph:
archs = set(paragraph["Architecture"].split())
# Sort, with wildcard entries (such as linux-any) first:
2010-11-27 09:24:32 +02:00
archs = sorted(archs, key=lambda x: ("any" not in x, x))
paragraph["Architecture"] = " ".join(archs)
2010-11-24 18:34:20 +02:00
if sort_paragraphs:
first = self.paragraphs[:1 + int(keep_first)]
sortable = self.paragraphs[1 + int(keep_first):]
key = lambda x: x.get("Package")
self.paragraphs = first + sorted(sortable, key=key)
def _wrap_field(self, control, entry, wrap_always, short_indent, sort=True):
packages = [x.strip() for x in control[entry].split(",")]
2010-11-24 18:34:20 +02:00
if sort:
# Remove duplicate entries
packages = set(packages)
# Not explicitly disallowed by Policy but known to break QA tools:
if "" in packages:
packages.remove("")
packages = sort_list(packages)
2010-11-24 18:34:20 +02:00
length = len(entry) + sum([2 + len(package) for package in packages])
2010-11-24 19:59:44 +02:00
if wrap_always or length > 80:
indentation = " "
if not short_indent:
2010-11-24 20:01:06 +02:00
indentation *= len(entry) + 2
packages_with_indention = [indentation + x for x in packages]
2010-11-24 19:25:29 +02:00
packages_with_indention = ",\n".join(packages_with_indention)
if short_indent:
control[entry] = "\n" + packages_with_indention
else:
control[entry] = packages_with_indention.strip()
2010-11-24 18:34:20 +02:00
else:
control[entry] = ", ".join(packages).strip()
class Install(object):
2010-11-24 18:34:20 +02:00
def __init__(self, filename):
self.open(filename)
2010-11-24 18:34:20 +02:00
def open(self, filename):
assert os.path.isfile(filename), "%s does not exist." % (filename)
self.filename = filename
self.content = open(filename).readlines()
2010-11-24 18:34:20 +02:00
def save(self, filename=None):
if filename:
self.filename = filename
f = open(self.filename, "w")
f.write("".join(self.content))
f.close()
2010-11-24 18:34:20 +02:00
def sort(self):
self.content = sorted(self.content)
def remove_trailing_whitespaces(filename):
2010-11-24 18:34:20 +02:00
assert os.path.isfile(filename), "%s does not exist." % (filename)
content = open(filename).read().rstrip() + "\n"
lines = content.split("\n")
lines = [l.rstrip() for l in lines]
2010-11-24 18:34:20 +02:00
new_content = "\n".join(lines)
f = open(filename, "w")
f.write(new_content)
f.close()
def sort_list(unsorted_list):
normal = [x for x in unsorted_list if not x.startswith("${")]
param = [x for x in unsorted_list if x.startswith("${")]
2010-11-24 18:34:20 +02:00
return sorted(normal) + sorted(param)
def main(options):
debdir = lambda x: os.path.join(options.debian_directory, x)
2010-11-24 18:34:20 +02:00
control_files = filter(os.path.isfile,
[debdir("control"), debdir("control.in")])
2010-11-24 18:34:20 +02:00
for control_file in control_files:
if options.verbose:
2010-11-24 18:34:20 +02:00
print control_file
control = WrapAndSortControl(control_file)
if options.cleanup:
control.strip_trailing_spaces()
control.wrap_and_sort(options.wrap_always, options.short_indent,
options.sort_binary_packages, options.keep_first)
2010-11-24 18:34:20 +02:00
control.save()
copyright_files = filter(os.path.isfile,
[debdir("copyright"), debdir("copyright.in")])
2010-11-24 18:34:20 +02:00
for copyright_file in copyright_files:
if options.verbose:
2010-11-24 18:34:20 +02:00
print copyright_file
remove_trailing_whitespaces(copyright_file)
install_files = sorted(glob.glob(debdir("*.install")))
if os.path.isfile(debdir("install")):
install_files.insert(0, debdir("install"))
for install_file in install_files:
if options.verbose:
2010-11-24 18:34:20 +02:00
print install_file
install = Install(install_file)
install.sort()
install.save()
if __name__ == "__main__":
2010-11-24 18:34:20 +02:00
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)
2010-11-24 20:42:14 +01:00
parser.add_option("-a", "--wrap-always", dest="wrap_always",
help="wrap lists even if they fit into one 80 character "
"long line", action="store_true", default=False)
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)
2010-11-24 18:34:20 +02:00
parser.add_option("-n", "--no-cleanup", help="don't cleanup whitespaces",
2010-11-24 20:42:14 +01:00
dest="cleanup", action="store_false", default=True)
2010-11-24 22:54:18 +01:00
parser.add_option("-d", "--debian-directory", dest="debian_directory",
help="location of the 'debian' directory (default: "
"./debian)", metavar="PATH", default="debian")
2010-11-24 18:34:20 +02:00
parser.add_option("-v", "--verbose", help="print more information",
2010-11-24 20:42:14 +01:00
dest="verbose", action="store_true", default=False)
2010-11-24 18:34:20 +02:00
(options, args) = parser.parse_args()
if len(args) != 0:
2010-11-24 20:42:14 +01:00
print >> sys.stderr, ("%s: Error: Unsupported additional parameters "
"specified: %s") % (script_name, ", ".join(args))
2010-11-24 18:34:20 +02:00
sys.exit(1)
if not os.path.isdir(options.debian_directory):
2010-11-24 22:54:18 +01:00
print >> sys.stderr, ('%s: Error: Debian directory not found, '
'expecting "%s"') % (script_name,
options.debian_directory)
sys.exit(1)
main(options)