示例#1
0
 def test_relationship_unparsing(self):
     relationship_set = parse_depends('foo, bar(>=1)|baz')
     self.assertEqual(unicode(relationship_set), 'foo, bar (>= 1) | baz')
     self.assertEqual(
         compact(repr(relationship_set)),
         "RelationshipSet(Relationship(name='foo'), AlternativeRelationship(VersionedRelationship(name='bar', operator='>=', version='1'), Relationship(name='baz')))"
     )
示例#2
0
 def test_control_field_parsing(self):
     deb822_package = Deb822([
         'Package: python-py2deb',
         'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
         'Installed-Size: 42'
     ])
     parsed_info = parse_control_fields(deb822_package)
     self.assertEqual(
         parsed_info, {
             'Package':
             'python-py2deb',
             'Depends':
             RelationshipSet(Relationship(name=u'python-deb-pkg-tools'),
                             Relationship(name=u'python-pip'),
                             Relationship(name=u'python-pip-accel')),
             'Installed-Size':
             42
         })
     # Test backwards compatibility with the old interface where `Depends'
     # like fields were represented as a list of strings (shallow parsed).
     parsed_info['Depends'] = [unicode(r) for r in parsed_info['Depends']]
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
     # Test compatibility with fields like `Depends' containing a string.
     parsed_info['Depends'] = deb822_package['Depends']
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
示例#3
0
 def test_control_field_parsing(self):
     deb822_package = Deb822(['Package: python-py2deb',
                              'Depends: python-deb-pkg-tools, python-pip, python-pip-accel',
                              'Installed-Size: 42'])
     parsed_info = parse_control_fields(deb822_package)
     self.assertEqual(parsed_info,
                      {'Package': 'python-py2deb',
                       'Depends': RelationshipSet(
                           Relationship(name=u'python-deb-pkg-tools'),
                           Relationship(name=u'python-pip'),
                           Relationship(name=u'python-pip-accel')),
                       'Installed-Size': 42})
     # Test backwards compatibility with the old interface where `Depends'
     # like fields were represented as a list of strings (shallow parsed).
     parsed_info['Depends'] = [unicode(r) for r in parsed_info['Depends']]
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
     # Test compatibility with fields like `Depends' containing a string.
     parsed_info['Depends'] = deb822_package['Depends']
     self.assertEqual(unparse_control_fields(parsed_info), deb822_package)
def unparse_control_fields(input_fields):
    """
    Convert a :py:class:`dict` returned by :py:func:`parse_control_fields()`
    back into a :py:class:`debian.deb822.Deb822` object.

    Note that fields with an empty value are omitted. This makes it possible to
    delete fields from a control file with :py:func:`patch_control_file()` by
    setting the value of a field to ``None`` in the overrides...

    :param input_fields: A :py:class:`dict` object previously returned by
                         :py:func:`parse_control_fields()`.
    :returns: A :py:class:`debian.deb822.Deb822` object.
    """
    logger.debug("Unparsing %i control fields ..", len(input_fields))
    output_fields = Deb822()
    for name, parsed_value in input_fields.items():
        name = normalize_control_field_name(name)
        if name in DEPENDS_LIKE_FIELDS:
            if isinstance(parsed_value, RelationshipSet):
                # New interface (a RelationshipSet object).
                unparsed_value = unicode(parsed_value)
            elif not isinstance(parsed_value, basestring):
                # Backwards compatibility  with old interface (list of strings).
                unparsed_value = ", ".join(parsed_value)
            else:
                # Compatibility with callers that set one of the Depends-like
                # fields to a string value (which is fine).
                unparsed_value = parsed_value
        elif name == "Installed-Size":
            unparsed_value = str(parsed_value)
        else:
            unparsed_value = parsed_value
        if unparsed_value:
            output_fields[name] = unparsed_value
    logger.debug("Unparsed fields: %r", output_fields)
    return output_fields
示例#5
0
def unparse_control_fields(input_fields):
    """
    Convert a :py:class:`dict` returned by :py:func:`parse_control_fields()`
    back into a :py:class:`debian.deb822.Deb822` object.

    Note that fields with an empty value are omitted. This makes it possible to
    delete fields from a control file with :py:func:`patch_control_file()` by
    setting the value of a field to ``None`` in the overrides...

    :param input_fields: A :py:class:`dict` object previously returned by
                         :py:func:`parse_control_fields()`.
    :returns: A :py:class:`debian.deb822.Deb822` object.
    """
    logger.debug("Unparsing %i control fields ..", len(input_fields))
    output_fields = Deb822()
    for name, parsed_value in input_fields.items():
        name = normalize_control_field_name(name)
        if name in DEPENDS_LIKE_FIELDS:
            if isinstance(parsed_value, RelationshipSet):
                # New interface (a RelationshipSet object).
                unparsed_value = unicode(parsed_value)
            elif not isinstance(parsed_value, basestring):
                # Backwards compatibility  with old interface (list of strings).
                unparsed_value = ', '.join(parsed_value)
            else:
                # Compatibility with callers that set one of the Depends-like
                # fields to a string value (which is fine).
                unparsed_value = parsed_value
        elif name == 'Installed-Size':
            unparsed_value = str(parsed_value)
        else:
            unparsed_value = parsed_value
        if unparsed_value:
            output_fields[name] = unparsed_value
    logger.debug("Unparsed fields: %r", output_fields)
    return output_fields
示例#6
0
 def test_relationship_unparsing(self):
     relationship_set = parse_depends('foo, bar(>=1)|baz')
     self.assertEqual(unicode(relationship_set), 'foo, bar (>= 1) | baz')
     self.assertEqual(compact(repr(relationship_set)), "RelationshipSet(Relationship(name='foo'), AlternativeRelationship(VersionedRelationship(name='bar', operator='>=', version='1'), Relationship(name='baz')))")