wrap-and-sort: New script to wrap long lines and sort items in packaging

files.
This commit is contained in:
Benjamin Drung 2010-09-17 04:44:10 +02:00
parent 403a1a9412
commit 0327c3816d
5 changed files with 221 additions and 1 deletions

4
debian/changelog vendored
View File

@ -16,6 +16,8 @@ ubuntu-dev-tools (0.102) UNRELEASED; urgency=low
to a @lists.ubuntu.com address.
* sponsor-patch: New script to download a patch from a Launchpad bug, patch
the source package, build, check and uploads it (to Ubuntu or a PPA).
* wrap-and-sort: New script to wrap long lines and sort items in packaging
files.
[ Stefano Rivera ]
* update-maintainer: Correctly update the Maintainer field to the new Ubuntu
@ -29,7 +31,7 @@ ubuntu-dev-tools (0.102) UNRELEASED; urgency=low
[ Michael Bienia ]
* Add 'natty' to recognized names and make it the default.
-- Michael Bienia <geser@ubuntu.com> Wed, 15 Sep 2010 09:20:28 +0200
-- Benjamin Drung <bdrung@ubuntu.com> Fri, 17 Sep 2010 04:42:53 +0200
ubuntu-dev-tools (0.101) unstable; urgency=low

1
debian/control vendored
View File

@ -94,3 +94,4 @@ Description: useful tools for Ubuntu developers
- update-maintainer - script to update maintainer field in ubuntu packages.
- what-patch - determines what patch system, if any, a source package is
using.
- wrap-and-sort - wrap long lines and sort items in packaging files.

53
doc/wrap-and-sort.1 Normal file
View File

@ -0,0 +1,53 @@
.\" Copyright (c) 2010, Benjamin Drung <bdrung@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.
.\"
.TH WRAP\-AND\-SORT 1 "September 2010" "ubuntu-dev-tools"
.SH NAME
wrap-and-sort \- wrap long lines and sort items in packaging files
.SH SYNOPSIS
.B wrap-and-sort
[\fIoptions\fR]
.SH DESCRIPTION
\fBwrap\-and\-sort\fP wraps the package lists in Debian control files. By
default the lists will only split into multiple lines if the entries are longer
than 80 characters. \fBwrap\-and\-sort\fP sorts the package lists in Debian
control files and all .install files. Beside that \fBwrap\-and\-sort\fP removes
trailing spaces in these files.
.PP
This script should be run in the root of a Debian package tree. It searches for
debian/control, debian/control.in, debian/copyright, debian/copyright.in,
and debian/*.install.
.SH OPTIONS
.TP
\fB\-h\fR, \fB\-\-help\fR
show this help message and exit
.TP
\fB\-a\fR, \fB\-\-wrap\-allways\fR
wrap all package lists in the Debian control file
even if the entries are shorter than 80 characters and could fit in one line
line
.TP
\fB\-n\fR, \fB\-\-no\-cleanup\fR
do not remove trailing whitespaces
.TP
\fB\-v\fR, \fB\-\-verbose\fR
print all files that are touched
.SH AUTHORS
\fBwrap\-and\-sort\fP and this manpage has been written by
Benjamin Drung <bdrung@ubuntu.com>.
.PP
Both are released under the ISC license.

View File

@ -51,6 +51,7 @@ setup(name='ubuntu-dev-tools',
'ubuntu-iso',
'update-maintainer',
'what-patch',
'wrap-and-sort',
],
packages=['ubuntutools',
'ubuntutools/lp',

163
wrap-and-sort Executable file
View File

@ -0,0 +1,163 @@
#!/usr/bin/python
#
# Copyright (C) 2010, Benjamin Drung <bdrung@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 debian.deb822
import glob
import optparse
import os
import sys
CONTROL_LIST_FIELDS = (
"Breaks",
"Build-Depends",
"Build-Depends-Indep",
"Conflicts",
"Depends",
"Enhances",
"Provides",
"Recommends",
"Replaces",
"Suggests",
"Xb-Npp-MimeType",
)
class Control(object):
def __init__(self, filename, cleanup):
assert os.path.isfile(filename), "%s does not exist." % (filename)
self.filename = filename
sequence = open(filename)
if cleanup:
sequence = map(lambda l: l.rstrip(), sequence.readlines())
self.paragraphs = list()
for paragraph in debian.deb822.Deb822.iter_paragraphs(sequence):
self.paragraphs.append(paragraph)
def save(self, filename=None):
if filename:
self.filename = filename
content = u"\n".join(map(lambda x: x.dump(), self.paragraphs))
f = open(self.filename, "w")
f.write(content.encode("utf-8"))
f.close()
def wrap_and_sort(self, wrap_allways=True):
for paragraph in self.paragraphs:
for field in CONTROL_LIST_FIELDS:
if field in paragraph:
self._wrap_field(paragraph, field, wrap_allways)
if "Uploaders" in paragraph:
self._wrap_field(paragraph, "Uploaders", wrap_allways, False)
def _wrap_field(self, control, entry, wrap_allways, sort=True):
packages = map(lambda x: x.strip(), control[entry].split(","))
if sort:
packages = sort_list(packages)
lenght = len(entry) + 2 * len(packages) + sum(map(len, packages))
if wrap_allways or lenght > 80:
indentation = " " * (len(entry) + 2)
packages_with_indention = map(lambda x: indentation + x, packages)
control[entry] = ",\n".join(packages_with_indention).strip()
else:
control[entry] = ", ".join(packages).strip()
class Install(object):
def __init__(self, filename):
self.open(filename)
def open(self, filename):
assert os.path.isfile(filename), "%s does not exist." % (filename)
self.filename = filename
self.content = open(filename).readlines()
def save(self, filename=None):
if filename:
self.filename = filename
f = open(self.filename, "w")
f.write("".join(self.content))
f.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().rstrip() + "\n"
lines = content.split("\n")
lines = map(lambda l: l.rstrip(), lines)
new_content = "\n".join(lines)
f = open(filename, "w")
f.write(new_content)
f.close()
def sort_list(l):
normal = filter(lambda x: not x.startswith("${"), l)
param = filter(lambda x: x.startswith("${"), l)
return sorted(normal) + sorted(param)
def main(script_name, cleanup, wrap_allways, verbose=False):
if not os.path.isdir("debian"):
print >> sys.stderr, "%s: Error: No debian directory found." % \
(script_name)
sys.exit(1)
control_files = filter(os.path.isfile,
["debian/control", "debian/control.in"])
for control_file in control_files:
if verbose:
print control_file
control = Control(control_file, cleanup)
control.wrap_and_sort(wrap_allways)
control.save()
copyright_files = filter(os.path.isfile,
["debian/copyright", "debian/copyright.in"])
for copyright_file in copyright_files:
if verbose:
print copyright_file
remove_trailing_whitespaces(copyright_file)
for install_file in sorted(glob.glob("debian/*.install")):
if verbose:
print install_file
install = Install(install_file)
install.sort()
install.save()
if __name__ == "__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-allways",
help="wrap lists even if they fit into one 80 character long line",
dest="wrap_allways", 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("-v", "--verbose", help="print more information",
dest="verbose", action="store_true", default=False)
(options, args) = parser.parse_args()
if len(args) != 0:
print >> sys.stderr, "%s: Error: %s: %s" % (script_name,
"Unsupported additional parameters specified", ", ".join(args))
sys.exit(1)
main(script_name, options.cleanup, options.wrap_allways, options.verbose)