Welcome to mirror list, hosted at ThFree Co, Russian Federation.

repodata_parsing.py « python « examples - github.com/rpm-software-management/createrepo_c.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 518962a97d4720e0095a321819a5ef970eea12b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env python

import os
import os.path

import createrepo_c as cr

REPO_PATH = "repo/"


def print_package_info(pkg):
    def print_pcors(lst, requires=False):
        for item in lst:
            print("    Name: %s" % item[cr.PCOR_ENTRY_NAME])
            print("    Flags: %s" % item[cr.PCOR_ENTRY_FLAGS])
            print("    Epoch: %s" % item[cr.PCOR_ENTRY_EPOCH])
            print("    Version: %s" % item[cr.PCOR_ENTRY_VERSION])
            print("    Release: %s" % item[cr.PCOR_ENTRY_RELEASE])
            if requires:
                print("    Pre: %s" % item[cr.PCOR_ENTRY_PRE])
            print("    +-----------------------------------+")

    def print_files(lst):
        for item in lst:
            print("    Name: %s" % item[cr.FILE_ENTRY_NAME])
            print("    Path: %s" % item[cr.FILE_ENTRY_PATH])
            print("    Type: %s" % item[cr.FILE_ENTRY_TYPE])
            print("    +-----------------------------------+")

    def print_changelogs(lst):
        for item in lst:
            print("    Author: %s" % item[cr.CHANGELOG_ENTRY_AUTHOR])
            print("    Date: %s" % item[cr.CHANGELOG_ENTRY_DATE])
            print("    Changelog: %s" % item[cr.CHANGELOG_ENTRY_CHANGELOG])
            print("    +-----------------------------------+")

    print("+=======================================+")
    print("  %s" % pkg.name)
    print("+=======================================+")
    print("NEVRA: %s" % pkg.nevra())
    print("NVRA: %s" % pkg.nvra())
    print("Name: %s" % pkg.name)
    print("Checksum (pkgId): %s" % pkg.pkgId)
    print("Checksum type: %s" % pkg.checksum_type)
    print("Arch: %s" % pkg.arch)
    print("Version: %s" % pkg.version)
    print("Epoch: %s" % pkg.epoch)
    print("Release: %s" % pkg.release)
    print("Summary: %s" % pkg.summary)
    print("Description: %s" % pkg.description)
    print("URL: %s" % pkg.url)
    print("Time file: %s" % pkg.time_file)
    print("Time build: %s" % pkg.time_build)
    print("License: %s" % pkg.rpm_license)
    print("Vendor: %s" % pkg.rpm_vendor)
    print("Group: %s" % pkg.rpm_group)
    print("Buildhost: %s" % pkg.rpm_buildhost)
    print("Source RPM: %s" % pkg.rpm_sourcerpm)
    print("Header start: %s" % pkg.rpm_header_start)
    print("Header end: %s" % pkg.rpm_header_end)
    print("Packager: %s" % pkg.rpm_packager)
    print("Size package: %s" % pkg.size_package)
    print("Size installed: %s" % pkg.size_installed)
    print("Size archive: %s" % pkg.size_archive)
    print("Location href: %s" % pkg.location_href)
    print("Location base: %s" % pkg.location_base)
    print("Requires:")
    print_pcors(pkg.requires, requires=True)
    print("Provides:")
    print_pcors(pkg.provides)
    print("Conflicts:")
    print_pcors(pkg.conflicts)
    print("Obsoletes:")
    print_pcors(pkg.obsoletes)
    print("Files:")
    print_files(pkg.files)
    print("Changelogs:")
    print_changelogs(pkg.changelogs)

def first_method():
    """Use of this method is discouraged."""
    md = cr.Metadata()
    md.locate_and_load_xml(REPO_PATH)
    for key in md.keys():
        pkg = md.get(key)
        print_package_info(pkg)

def second_method():
    """Preferred method for repodata parsing.

    Important callbacks for repodata parsing:

    newpkgcb
    --------
    Via newpkgcb (Package callback) you could directly
    affect if the current package element should be parsed
    or not. This decision could be based on
    three values that are available as attributtes
    in the <package> element. This values are:
     - pkgId (package checksum)
     - name (package name)
     - arch (package architecture)
    (Note: This is applicable only for filelists.xml and other.xml,
     primary.xml doesn't contain this information in <package> element)

    If newpkgcb returns a package object, the parsed data
    will be loaded to this package object. If it returns a None,
    package element is skiped.

    This could help you to reduce a memory requirements because
    non wanted packages could be skiped without need to
    store them into the memory.

    If no newpkgcb is specified, default callback returning
    a new package object is used.

    pkgcb
    -----
    Callback called when a <package> element parsing is done.
    Its argument is a package object that has been previously
    returned by the newpkgcb.
    This function should return True if parsing should continue
    or False if parsing should be interrupted.

    Note: Both callbacks are optional, BUT at least one
          MUST be used (newpkgcb or pkgcb)!

    warningcb
    ---------
    Warning callbacks is called when a non-fatal oddity of prased XML
    is detected.
    If True is returned, parsing continues. If return value is False,
    parsing is terminated.
    This callback is optional.
    """

    primary_xml_path   = None
    filelists_xml_path = None
    other_xml_path     = None

    #
    # repomd.xml parsing
    #

    # Parse repomd.xml to get paths (1. Method - Repomd object based)
    #   Pros: Easy to use
    repomd = cr.Repomd(os.path.join(REPO_PATH, "repodata/repomd.xml"))

    # Parse repomd.xml (2. Method - Parser based)
    #   Pros: Warning callback could be specified
    def warningcb(warning_type, message):
        """Optional callback for warnings about
        wierd stuff and formatting in XML.

        :param warning_type: Integer value. One from
                             the XML_WARNING_* constants.
        :param message: String message.
        """
        print("PARSER WARNING: %s" % message)
        return True

    repomd2 = cr.Repomd()
    cr.xml_parse_repomd(os.path.join(REPO_PATH, "repodata/repomd.xml"),
                                     repomd2, warningcb)

    # Get stuff we need
    #   (repomd or repomd2 could be used, both have the same values)
    for record in repomd.records:
        if record.type == "primary":
            primary_xml_path = record.location_href
        elif record.type == "filelists":
            filelists_xml_path = record.location_href
        elif record.type == "other":
            other_xml_path = record.location_href


    #
    # Main XML metadata parsing (primary, filelists, other)
    #

    packages = {}

    def pkgcb(pkg):
        # Called when whole package entry in xml is parsed
        packages[pkg.pkgId] = pkg

    def newpkgcb(pkgId, name, arch):
        # Called when new package entry is encountered
        # And only opening <package> element is parsed
        # This function has to return a package to which
        # parsed data will be added or None if this package
        # should be skiped.
        return packages.get(pkgId, None)

    # Option do_files tells primary parser to skip <file> element of package.
    # If you plan to parse filelists.xml after the primary.xml, always
    # set do_files to False.
    cr.xml_parse_primary(os.path.join(REPO_PATH, primary_xml_path),
                         pkgcb=pkgcb,
                         do_files=False,
                         warningcb=warningcb)

    cr.xml_parse_filelists(os.path.join(REPO_PATH, filelists_xml_path),
                           newpkgcb=newpkgcb,
                           warningcb=warningcb)

    cr.xml_parse_other(os.path.join(REPO_PATH, other_xml_path),
                       newpkgcb=newpkgcb,
                       warningcb=warningcb)

    for pkg in packages.values():
        print_package_info(pkg)

def third_method():
    """Parsing main metadata types (primary, filelists, other) at the same time.
    This approach significantly reduces memory footprint because we don't need
    to keep all the packages in memory, user can handle them one by one.

    The API reflects xml_parse_primary/filelists/other except that it handles
    all of them at the same time.

    """
    def warningcb(warning_type, message):
        print("PARSER WARNING: %s" % message)
        return True

    repomd = cr.Repomd()
    cr.xml_parse_repomd(os.path.join(REPO_PATH, "repodata/repomd.xml"), repomd, warningcb)

    primary_xml_path   = None
    filelists_xml_path = None
    other_xml_path     = None
    for record in repomd.records:
        if record.type == "primary":
            primary_xml_path = os.path.join(REPO_PATH, record.location_href)
        elif record.type == "filelists":
            filelists_xml_path = os.path.join(REPO_PATH, record.location_href)
        elif record.type == "other":
            other_xml_path = os.path.join(REPO_PATH, record.location_href)

    #
    # Main XML metadata parsing (primary, filelists, other)
    #

    def pkgcb(pkg):
        # Called when whole package entry from all 3 metadata xml files is parsed
        print_package_info(pkg)

    cr.xml_parse_main_metadata_together(primary_xml_path,
                                        filelists_xml_path,
                                        other_xml_path,
                                        None,
                                        pkgcb,
                                        warningcb)

if __name__ == "__main__":
    print('"All in one shot" method:')
    first_method()

    print()

    print("Callback based method:")
    second_method()

    print()

    print("Streaming callback based method:")
    third_method()