wrap-and-sort: Split Control into separate Python file.

This commit is contained in:
Benjamin Drung 2010-12-23 17:18:42 +01:00
parent 401dd27276
commit fec33a6a41
3 changed files with 62 additions and 23 deletions

4
debian/copyright vendored
View File

@ -188,13 +188,13 @@ Files: doc/sponsor-patch.1,
suspicious-source, suspicious-source,
ubuntutools/builder.py, ubuntutools/builder.py,
ubuntutools/config.py, ubuntutools/config.py,
ubuntutools/control.py,
ubuntutools/logger.py, ubuntutools/logger.py,
ubuntutools/question.py, ubuntutools/question.py,
ubuntutools/sponsor_patch/*, ubuntutools/sponsor_patch/*,
ubuntutools/test/*, ubuntutools/test/*,
wrap-and-sort wrap-and-sort
Copyright: Copyright: 2010, Benjamin Drung <bdrung@ubuntu.com>
2010, Benjamin Drung <bdrung@ubuntu.com>
2010, Evan Broder <evan@ebroder.net> 2010, Evan Broder <evan@ebroder.net>
2010, Stefano Rivera <stefanor@ubuntu.com> 2010, Stefano Rivera <stefanor@ubuntu.com>
License: ISC License: ISC

54
ubuntutools/control.py Normal file
View File

@ -0,0 +1,54 @@
#
# control.py - Represents a debian/control file
#
# 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 os
import debian.deb822
class Control(object):
"""Represents a debian/control file"""
def __init__(self, filename):
assert os.path.isfile(filename), "%s does not exist." % (filename)
self.filename = filename
sequence = open(filename)
self.paragraphs = list()
for paragraph in debian.deb822.Deb822.iter_paragraphs(sequence):
self.paragraphs.append(paragraph)
def get_source_paragraph(self):
"""Returns the source paragraph of the control file."""
if self.paragraphs:
return self.paragraphs[0]
else:
return None
def save(self, filename=None):
"""Saves the control file."""
if filename:
self.filename = filename
content = u"\n".join([x.dump() for x in self.paragraphs])
control_file = open(self.filename, "w")
control_file.write(content.encode("utf-8"))
control_file.close()
def strip_trailing_spaces(self):
"""Strips all trailing spaces from the control file."""
for paragraph in self.paragraphs:
for item in paragraph:
lines = paragraph[item].split("\n")
paragraph[item] = "\n".join([l.rstrip() for l in lines])

View File

@ -15,13 +15,14 @@
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
import debian.deb822
import glob import glob
import optparse import optparse
import os import os
import os.path import os.path
import sys import sys
from ubuntutools.control import Control
CONTROL_LIST_FIELDS = ( CONTROL_LIST_FIELDS = (
"Breaks", "Breaks",
"Build-Depends", "Build-Depends",
@ -36,25 +37,7 @@ CONTROL_LIST_FIELDS = (
"Xb-Npp-MimeType", "Xb-Npp-MimeType",
) )
class Control(object): class WrapAndSortControl(Control):
def __init__(self, filename, cleanup):
assert os.path.isfile(filename), "%s does not exist." % (filename)
self.filename = filename
sequence = open(filename)
if cleanup:
sequence = [l.rstrip() for l in 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([x.dump() for x in self.paragraphs])
f = open(self.filename, "w")
f.write(content.encode("utf-8"))
f.close()
def wrap_and_sort(self, wrap_always, short_indent, sort_paragraphs, def wrap_and_sort(self, wrap_always, short_indent, sort_paragraphs,
keep_first): keep_first):
for paragraph in self.paragraphs: for paragraph in self.paragraphs:
@ -145,7 +128,9 @@ def main(options):
for control_file in control_files: for control_file in control_files:
if options.verbose: if options.verbose:
print control_file print control_file
control = Control(control_file, options.cleanup) control = WrapAndSortControl(control_file)
if options.cleanup:
control.strip_trailing_spaces()
control.wrap_and_sort(options.wrap_always, options.short_indent, control.wrap_and_sort(options.wrap_always, options.short_indent,
options.sort_binary_packages, options.keep_first) options.sort_binary_packages, options.keep_first)
control.save() control.save()