* wrap-and-sort:

- Correct typo in options --wrap-allways -> --wrap-always
  - Sort debian/install as well as debian/*.install
  - Add one-space-indentation option: --short-indent
  - Remove null-entry from trailing comma in sorted lists
This commit is contained in:
Stefano Rivera 2010-11-24 20:16:19 +02:00
commit d28b99753e
2 changed files with 137 additions and 113 deletions

7
debian/changelog vendored
View File

@ -2,8 +2,13 @@ ubuntu-dev-tools (0.107) UNRELEASED; urgency=low
* edit-patch: Detect quilt patch-system in 3.0 (quilt) packages without any * edit-patch: Detect quilt patch-system in 3.0 (quilt) packages without any
patches yet. patches yet.
* wrap-and-sort:
- Correct typo in options --wrap-allways -> --wrap-always
- Sort debian/install as well as debian/*.install
- Add one-space-indentation option: --short-indent
- Remove null-entry from trailing comma in sorted lists
-- Stefano Rivera <stefanor@ubuntu.com> Wed, 24 Nov 2010 17:58:18 +0200 -- Stefano Rivera <stefanor@ubuntu.com> Wed, 24 Nov 2010 20:02:21 +0200
ubuntu-dev-tools (0.106) experimental; urgency=low ubuntu-dev-tools (0.106) experimental; urgency=low

View File

@ -21,143 +21,162 @@ import os
import sys import sys
CONTROL_LIST_FIELDS = ( CONTROL_LIST_FIELDS = (
"Breaks", "Breaks",
"Build-Depends", "Build-Depends",
"Build-Depends-Indep", "Build-Depends-Indep",
"Conflicts", "Conflicts",
"Depends", "Depends",
"Enhances", "Enhances",
"Provides", "Provides",
"Recommends", "Recommends",
"Replaces", "Replaces",
"Suggests", "Suggests",
"Xb-Npp-MimeType", "Xb-Npp-MimeType",
) )
class Control(object): class Control(object):
def __init__(self, filename, cleanup): def __init__(self, filename, cleanup):
assert os.path.isfile(filename), "%s does not exist." % (filename) assert os.path.isfile(filename), "%s does not exist." % (filename)
self.filename = filename self.filename = filename
sequence = open(filename) sequence = open(filename)
if cleanup: if cleanup:
sequence = map(lambda l: l.rstrip(), sequence.readlines()) sequence = map(lambda l: l.rstrip(), sequence.readlines())
self.paragraphs = list() self.paragraphs = list()
for paragraph in debian.deb822.Deb822.iter_paragraphs(sequence): for paragraph in debian.deb822.Deb822.iter_paragraphs(sequence):
self.paragraphs.append(paragraph) self.paragraphs.append(paragraph)
def save(self, filename=None): def save(self, filename=None):
if filename: if filename:
self.filename = filename self.filename = filename
content = u"\n".join(map(lambda x: x.dump(), self.paragraphs)) content = u"\n".join(map(lambda x: x.dump(), self.paragraphs))
f = open(self.filename, "w") f = open(self.filename, "w")
f.write(content.encode("utf-8")) f.write(content.encode("utf-8"))
f.close() f.close()
def wrap_and_sort(self, wrap_allways=True): def wrap_and_sort(self, wrap_always, short_indent):
for paragraph in self.paragraphs: for paragraph in self.paragraphs:
for field in CONTROL_LIST_FIELDS: for field in CONTROL_LIST_FIELDS:
if field in paragraph: if field in paragraph:
self._wrap_field(paragraph, field, wrap_allways) self._wrap_field(paragraph, field, wrap_always,
if "Uploaders" in paragraph: short_indent)
self._wrap_field(paragraph, "Uploaders", wrap_allways, False) if "Uploaders" in paragraph:
self._wrap_field(paragraph, "Uploaders", wrap_always,
short_indent, False)
def _wrap_field(self, control, entry, wrap_allways, sort=True): def _wrap_field(self, control, entry, wrap_always, short_indent, sort=True):
packages = map(lambda x: x.strip(), control[entry].split(",")) packages = map(lambda x: x.strip(), control[entry].split(","))
if sort: if sort:
packages = sort_list(packages) packages = sort_list(packages)
# Not explicitly disallowed by Policy but known to break QA tools:
if "" in packages:
packages.remove("")
lenght = len(entry) + 2 * len(packages) + sum(map(len, packages)) length = len(entry) + 2 * len(packages) + sum(map(len, packages))
if wrap_allways or lenght > 80: if wrap_always or length > 80:
indentation = " " * (len(entry) + 2) indentation = " "
packages_with_indention = map(lambda x: indentation + x, packages) if not short_indent:
control[entry] = ",\n".join(packages_with_indention).strip() indentation *= len(entry) + 2
else: packages_with_indention = map(lambda x: indentation + x, packages)
control[entry] = ", ".join(packages).strip() packages_with_indention = ",\n".join(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): class Install(object):
def __init__(self, filename): def __init__(self, filename):
self.open(filename) self.open(filename)
def open(self, filename): def open(self, filename):
assert os.path.isfile(filename), "%s does not exist." % (filename) assert os.path.isfile(filename), "%s does not exist." % (filename)
self.filename = filename self.filename = filename
self.content = open(filename).readlines() self.content = open(filename).readlines()
def save(self, filename=None): def save(self, filename=None):
if filename: if filename:
self.filename = filename self.filename = filename
f = open(self.filename, "w") f = open(self.filename, "w")
f.write("".join(self.content)) f.write("".join(self.content))
f.close() f.close()
def sort(self): def sort(self):
self.content = sorted(self.content) self.content = sorted(self.content)
def remove_trailing_whitespaces(filename): def remove_trailing_whitespaces(filename):
assert os.path.isfile(filename), "%s does not exist." % (filename) assert os.path.isfile(filename), "%s does not exist." % (filename)
content = open(filename).read().rstrip() + "\n" content = open(filename).read().rstrip() + "\n"
lines = content.split("\n") lines = content.split("\n")
lines = map(lambda l: l.rstrip(), lines) lines = map(lambda l: l.rstrip(), lines)
new_content = "\n".join(lines) new_content = "\n".join(lines)
f = open(filename, "w") f = open(filename, "w")
f.write(new_content) f.write(new_content)
f.close() f.close()
def sort_list(l): def sort_list(l):
normal = filter(lambda x: not x.startswith("${"), l) normal = filter(lambda x: not x.startswith("${"), l)
param = filter(lambda x: x.startswith("${"), l) param = filter(lambda x: x.startswith("${"), l)
return sorted(normal) + sorted(param) return sorted(normal) + sorted(param)
def main(script_name, cleanup, wrap_allways, verbose=False): def main(script_name, cleanup, wrap_always, short_indent, verbose=False):
if not os.path.isdir("debian"): if not os.path.isdir("debian"):
print >> sys.stderr, "%s: Error: No debian directory found." % \ print >> sys.stderr, "%s: Error: No debian directory found." % \
(script_name) (script_name)
sys.exit(1) sys.exit(1)
control_files = filter(os.path.isfile, control_files = filter(os.path.isfile,
["debian/control", "debian/control.in"]) ["debian/control", "debian/control.in"])
for control_file in control_files: for control_file in control_files:
if verbose: if verbose:
print control_file print control_file
control = Control(control_file, cleanup) control = Control(control_file, cleanup)
control.wrap_and_sort(wrap_allways) control.wrap_and_sort(wrap_always, short_indent)
control.save() control.save()
copyright_files = filter(os.path.isfile, copyright_files = filter(os.path.isfile,
["debian/copyright", "debian/copyright.in"]) ["debian/copyright", "debian/copyright.in"])
for copyright_file in copyright_files: for copyright_file in copyright_files:
if verbose: if verbose:
print copyright_file print copyright_file
remove_trailing_whitespaces(copyright_file) remove_trailing_whitespaces(copyright_file)
for install_file in sorted(glob.glob("debian/*.install")): install_files = sorted(glob.glob("debian/*.install"))
if verbose: if os.path.isfile("debian/install"):
print install_file install_files.insert(0, "debian/install")
install = Install(install_file) for install_file in install_files:
install.sort() if verbose:
install.save() print install_file
install = Install(install_file)
install.sort()
install.save()
if __name__ == "__main__": if __name__ == "__main__":
script_name = os.path.basename(sys.argv[0]) script_name = os.path.basename(sys.argv[0])
usage = "%s [options]" % (script_name) usage = "%s [options]" % (script_name)
epilog = "See %s(1) for more info." % (script_name) epilog = "See %s(1) for more info." % (script_name)
parser = optparse.OptionParser(usage=usage, epilog=epilog) parser = optparse.OptionParser(usage=usage, epilog=epilog)
parser.add_option("-a", "--wrap-allways", parser.add_option("-a", "--wrap-always",
help="wrap lists even if they fit into one 80 character long line", help="wrap lists even if they fit into one 80 character long line",
dest="wrap_allways", action="store_true", default=False) dest="wrap_always", action="store_true", default=False)
parser.add_option("-n", "--no-cleanup", help="don't cleanup whitespaces", parser.add_option("-s", "--short-indent",
dest="cleanup", action="store_false", default=True) help="only indent wrapped lines by one space (default is in-line "
parser.add_option("-v", "--verbose", help="print more information", "with the field name)",
dest="verbose", action="store_true", default=False) dest="short_indent", 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() (options, args) = parser.parse_args()
if len(args) != 0: if len(args) != 0:
print >> sys.stderr, "%s: Error: %s: %s" % (script_name, print >> sys.stderr, "%s: Error: %s: %s" % (script_name,
"Unsupported additional parameters specified", ", ".join(args)) "Unsupported additional parameters specified", ", ".join(args))
sys.exit(1) sys.exit(1)
main(script_name, options.cleanup, options.wrap_allways, options.verbose) main(script_name, options.cleanup, options.wrap_always,
options.short_indent, options.verbose)