Commit 910d6e19 authored by Jelmer Vernooij's avatar Jelmer Vernooij

Add add_dependency function; try to preserve whitespace as best as possible.

parent 60f992a6
......@@ -17,6 +17,7 @@
"""Utility functions for dealing with control files."""
import collections
from io import BytesIO
from debian.changelog import Version
......@@ -181,16 +182,61 @@ def ensure_minimum_version(relationstr, package, minimum_version):
changed = True
if not found:
changed = True
relations.append(
(' ' if len(relations) > 0 else '',
[PkgRelation(name=package, version=('>=', minimum_version))],
''))
_add_dependency(
relations,
[PkgRelation(name=package, version=('>=', minimum_version))])
if changed:
return format_relations(relations)
# Just return the original; we don't preserve all formatting yet.
return relationstr
def _add_dependency(relations, relation):
"""Add a dependency to a depends line.
Args:
relations: existing list of relations
relation: New relation
Returns:
Nothing
"""
if len(relations) == 0:
head_whitespace = ''
elif len(relations) == 1:
head_whitespace = (relations[0][0] or " ") # Best guess
else:
ws = collections.defaultdict(lambda: 0)
for r in relations[1:]:
ws[r[0]] += 1
if len(ws) == 1:
head_whitespace = list(ws.keys())[0]
else:
head_whitespace = relations[-1][0] # Best guest
if len(relations) == 0:
tail_whitespace = ''
else:
tail_whitespace = relations[-1][2]
relations[-1] = relations[-1][:2] + ('', )
relations.append((head_whitespace, relation, tail_whitespace))
def add_dependency(relationstr, relation):
"""Add a dependency to a depends line.
Args:
relationstr: existing relations line
relation: New relation
Returns:
Nothing
"""
relations = parse_relations(relationstr)
if isinstance(relation, str):
relation = PkgRelation.parse(relation)
_add_dependency(relations, relation)
return format_relations(relations)
def drop_dependency(relationstr, package):
"""Drop a dependency from a depends line.
......
......@@ -23,6 +23,7 @@ from breezy.tests import (
)
from lintian_brush.control import (
add_dependency,
can_preserve_deb822,
drop_dependency,
ensure_minimum_version,
......@@ -216,3 +217,30 @@ class DropDependencyTests(TestCase):
self.assertEqual(
' dh-autoreconf',
drop_dependency('debhelper (>= 9), dh-autoreconf', 'debhelper'))
class AddDependencyTests(TestCase):
def test_added(self):
self.assertEqual(
'debhelper (>= 9), dh-autoreconf',
add_dependency('debhelper (>= 9)', 'dh-autoreconf'))
self.assertEqual(
'debhelper (>= 9), ${misc:Depends}',
add_dependency('debhelper (>= 9)', '${misc:Depends}'))
def test_indentation(self):
self.assertEqual("""foo,
bar,
blah""", add_dependency("""foo,
bar""", 'blah'))
self.assertEqual("""foo,
bar,
blah""", add_dependency("""foo,
bar""", 'blah'))
self.assertEqual("""foo,
bar,
blah
""", add_dependency("""foo,
bar
""", 'blah'))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment