~ubuntu-branches/ubuntu/lucid/landscape-client/lucid-updates

« back to all changes in this revision

Viewing changes to landscape/package/skeleton.py

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack
  • Date: 2012-04-10 14:28:48 UTC
  • mfrom: (1.1.27)
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: package-import@ubuntu.com-20120410142848-7xsy4g2xii7y7ntc
ImportĀ upstreamĀ versionĀ 12.04.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from landscape.lib.hashlib import sha1
2
2
 
 
3
import apt_pkg
 
4
 
3
5
 
4
6
PACKAGE   = 1 << 0
5
7
PROVIDES  = 1 << 1
91
93
    return skeleton
92
94
 
93
95
build_skeleton.inited = False
 
96
 
 
97
 
 
98
def relation_to_string(relation_tuple):
 
99
    """Convert an apt relation to a string representation.
 
100
 
 
101
    @param relation_tuple: A tuple, (name, version, relation). version
 
102
        and relation can be the empty string, if the relation is on a
 
103
        name only.
 
104
 
 
105
    Returns something like "name > 1.0"
 
106
    """
 
107
    name, version, relation_type = relation_tuple
 
108
    relation_string = name
 
109
    if relation_type:
 
110
        relation_string += " %s %s" % (relation_type, version)
 
111
    return relation_string
 
112
 
 
113
 
 
114
def parse_record_field(record, record_field, relation_type,
 
115
                       or_relation_type=None):
 
116
    """Parse an apt C{Record} field and return skeleton relations
 
117
 
 
118
    @param record: An C{apt.package.Record} instance with package information.
 
119
    @param record_field: The name of the record field to parse.
 
120
    @param relation_type: The deb relation that can be passed to
 
121
        C{skeleton.add_relation()}
 
122
    @param or_relation_type: The deb relation that should be used if
 
123
        there is more than one value in a relation.
 
124
    """
 
125
    relations = set()
 
126
    values = apt_pkg.parse_depends(record.get(record_field, ""))
 
127
    for value in values:
 
128
        value_strings = [relation_to_string(relation) for relation in value]
 
129
        value_relation_type = relation_type
 
130
        if len(value_strings) > 1:
 
131
            value_relation_type = or_relation_type
 
132
        relation_string = " | ".join(value_strings)
 
133
        relations.add((value_relation_type, relation_string))
 
134
    return relations
 
135
 
 
136
 
 
137
def build_skeleton_apt(version, with_info=False, with_unicode=False):
 
138
    """Build a package skeleton from an apt package.
 
139
 
 
140
    @param version: An instance of C{apt.package.Version}
 
141
    @param with_info: Whether to extract extra information about the
 
142
        package, like description, summary, size.
 
143
    @param with_unicode: Whether the C{name} and C{version} of the
 
144
        skeleton should be unicode strings.
 
145
    """
 
146
    name, version_string = version.package.name, version.version
 
147
    if with_unicode:
 
148
        name, version_string = unicode(name), unicode(version_string)
 
149
    skeleton = PackageSkeleton(DEB_PACKAGE, name, version_string)
 
150
    relations = set()
 
151
    relations.update(parse_record_field(
 
152
        version.record, "Provides", DEB_PROVIDES))
 
153
    relations.add((
 
154
        DEB_NAME_PROVIDES,
 
155
        "%s = %s" % (version.package.name, version.version)))
 
156
    relations.update(parse_record_field(
 
157
        version.record, "Pre-Depends", DEB_REQUIRES, DEB_OR_REQUIRES))
 
158
    relations.update(parse_record_field(
 
159
        version.record, "Depends", DEB_REQUIRES, DEB_OR_REQUIRES))
 
160
 
 
161
    relations.add((
 
162
        DEB_UPGRADES, "%s < %s" % (version.package.name, version.version)))
 
163
 
 
164
    relations.update(parse_record_field(
 
165
        version.record, "Conflicts", DEB_CONFLICTS))
 
166
    relations.update(parse_record_field(
 
167
        version.record, "Breaks", DEB_CONFLICTS))
 
168
    skeleton.relations = sorted(relations)
 
169
 
 
170
    if with_info:
 
171
        skeleton.section = version.section
 
172
        skeleton.summary = version.summary
 
173
        skeleton.description = version.description
 
174
        skeleton.size = version.size
 
175
        if version.installed_size > 0:
 
176
            skeleton.installed_size = version.installed_size
 
177
        if with_unicode:
 
178
            skeleton.section = skeleton.section.decode("utf-8")
 
179
            skeleton.summary = skeleton.summary.decode("utf-8")
 
180
            skeleton.description = skeleton.description.decode("utf-8")
 
181
    return skeleton