File: examples.rst

package info (click to toggle)
librepo 1.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,028 kB
  • sloc: ansic: 18,802; python: 3,822; xml: 581; sh: 142; makefile: 64
file content (283 lines) | stat: -rw-r--r-- 8,624 bytes parent folder | download | duplicates (4)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
.. _examples:

Examples
========

Librepo usage examples.

[More examples (including C examples)](https://github.com/Tojaj/librepo/tree/master/examples)


Simple download of metadata
---------------------------

::

    """
    Example: Simple download whole repository

    This example uses more "pythonic" way of usage.
    Instead of use setopt() method it uses class properties.

    Use case:
    We have a metalink url of a repository and we
    want do download complete repository metadata.
    """

    import librepo

    # Metalink URL
    METALINK_URL = "https://mirrors.fedoraproject.org/metalink?repo=fedora-18&arch=x86_64"

    # Destination directory (note: This directory must exists!)
    DESTDIR = "downloaded_metadata"

    if __name__ == "__main__":
        h = librepo.Handle()
        r = librepo.Result()
        # Repository with repodata in the rpm-md format
        h.repotype = librepo.LR_YUMREPO
        # Set metalink url
        h.mirrorlist = METALINK_URL
        # Destination directory for metadata
        h.destdir = DESTDIR

        try:
            h.perform(r)
        except librepo.LibrepoException as e:
            # rc - Return code (integer value)
            # msg - Detailed error message (string)
            # general_msg - Error message based on rc (string)
            rc, msg, general_msg  = e
            print "Error: %s" % msg

Metadata localisation
---------------------

::

    """
    Example: Localize metadata files of a local repository

    Use case:
    We have a local repositository in the rpm-md format
    and want to paths to all its metadata files.
    Repomd content is just a bonus.
    """

    import sys
    import librepo
    import pprint

    METADATA_PATH = "downloaded_metadata"

    if __name__ == "__main__":
        h = librepo.Handle()
        r = librepo.Result()
        # Repository with repodata in the rpm-md format
        h.setopt(librepo.LRO_REPOTYPE, librepo.LR_YUMREPO)
        # Path to metadata
        h.setopt(librepo.LRO_URLS, [METADATA_PATH])
        # Do not duplicate (copy) metadata, just localise them
        h.setopt(librepo.LRO_LOCAL, True)

        try:
            h.perform(r)
        except librepo.LibrepoException as e:
            rc, msg, general_msg = e
            print "Error: %s" % msg
            sys.exit(1)

        print "Repomd content:"
        pprint.pprint(r.getinfo(librepo.LRR_YUM_REPOMD))

        print "\nPaths to metadata files:"
        for data_type, path in r.getinfo(librepo.LRR_YUM_REPO).iteritems():
            print "%15s: %s" % (data_type, path)

        sys.exit(0)

Checksum verification
---------------------

.. note::
    Checksum verification is enabled by default. So command
    ``h.setopt(librepo.LRO_CHECKSUM, True)`` is unnecessary, but
    for illustration it is better to write the command anyway.

::

    """
    Example: Verify checksum of local metadata

    Use case:
    We have some incomplete metadata localy.
    They are incomplete because they doesn't
    contain all files specified in repomd.xml.
    They contains only primary.xml and filelists.xml.
    We want to check checksum of this metadata.
    """

    import sys
    import librepo

    METADATA_PATH = "downloaded_metadata"

    if __name__ == "__main__":
        h = librepo.Handle()
        r = librepo.Result()
        # Repository with repodata in the rpm-md format
        h.setopt(librepo.LRO_REPOTYPE, librepo.LR_YUMREPO)
        # Path to the metadata
        h.setopt(librepo.LRO_URLS, [METADATA_PATH])
        # Do not duplicate (copy) the metadata
        h.setopt(librepo.LRO_LOCAL, True)
        # Check checksum of metadata
        h.setopt(librepo.LRO_CHECKSUM, True)
        # Ignore missing metadata files
        h.setopt(librepo.LRO_IGNOREMISSING, True)

        try:
            h.perform(r)
        except librepo.LibrepoException as e:
            rc, msg, general_msg = e
            if rc == librepo.LRE_BADCHECKSUM:
                print "Corrupted metadata! (%s)" % msg
            else:
                print "Other error: %s" % msg
            sys.exit(1)

        print "Metadata are fine!"

More complex download
---------------------

::

    """
    librepo - example of usage
    """

    import os
    import sys
    import shutil
    from pprint import pprint

    import librepo

    DESTDIR = "downloaded_metadata"
    PROGRESSBAR_LEN = 50

    def callback(data, total_to_download, downloaded):
        """Progress callback"""
        if total_to_download <= 0:
            return
        completed = int(downloaded / (total_to_download / PROGRESSBAR_LEN))
        print "[%s%s] %8s/%8s (%s)\r" % ('#'*completed, '-'*(PROGRESSBAR_LEN-completed), int(downloaded), int(total_to_download), data),
        sys.stdout.flush()

    if __name__ == "__main__":
        # Prepare destination directory
        if os.path.exists(DESTDIR):
            if not os.path.isdir(DESTDIR):
                raise IOError("%s is not a directory" % DESTDIR)
            shutil.rmtree(DESTDIR)
        os.mkdir(DESTDIR)

        h = librepo.Handle() # Handle represents a download configuration
        r = librepo.Result() # Result represents an existing/downloaded repository

        # --- Mandatory arguments -------------------------------------------

        # URL of repository or URL of metalink/mirrorlist
        h.setopt(librepo.LRO_URLS, ["http://ftp.linux.ncsu.edu/pub/fedora/linux/releases/17/Everything/i386/os/"])
        #h.setopt(librepo.LRO_MIRRORLIST, "https://mirrors.fedoraproject.org/metalink?repo=fedora-source-17&arch=i386")
        # Note: LRO_URLS and LRO_MIRRORLIST could be set and used simultaneously
        #       and if download from LRO_URLS failed, then mirrorlist is used

        # Type of repository
        h.setopt(librepo.LRO_REPOTYPE, librepo.LR_YUMREPO)

        # --- Optional arguments --------------------------------------------

        # Make download interruptible
        h.setopt(librepo.LRO_INTERRUPTIBLE, True)

        # Destination directory for metadata
        h.setopt(librepo.LRO_DESTDIR, DESTDIR)

        # Check checksum of all files (if checksum is available in repomd.xml)
        h.setopt(librepo.LRO_CHECKSUM, True)

        # Callback to display progress of downloading
        h.setopt(librepo.LRO_PROGRESSCB, callback)

        # Set user data for the callback
        h.setopt(librepo.LRO_PROGRESSDATA, {'test': 'dict', 'foo': 'bar'})

        # Download only filelists.xml, prestodelta.xml
        # Note: repomd.xml is downloaded implicitly!
        # Note: If LRO_YUMDLIST is None -> all files are downloaded
        h.setopt(librepo.LRO_YUMDLIST, ["filelists", "prestodelta"])

        h.perform(r)

        # Get and show results
        pprint (r.getinfo(librepo.LRR_YUM_REPO))
        pprint (r.getinfo(librepo.LRR_YUM_REPOMD))

        # Whoops... I forget to download primary.xml.. Lets fix it!
        # Set LRO_UPDATE - only update existing Result
        h.setopt(librepo.LRO_UPDATE, True)
        h.setopt(librepo.LRO_YUMDLIST, ["primary"])
        h.perform(r)

        # List of mirrors
        # (In this case no mirrorlist is used -> list will contain only one url)
        # Example of access info via attr insted of .getinfo() method
        pprint (h.mirrors)

        # Get and show final results
        pprint (r.getinfo(librepo.LRR_YUM_REPO))
        pprint (r.getinfo(librepo.LRR_YUM_REPOMD))

How to get urls in a local mirrorlist
-------------------------------------

::

    import os
    import sys
    import librepo
    import pprint

    DESTDIR = "downloaded_metadata"

    if __name__ == "__main__":
        h = librepo.Handle()
        r = librepo.Result()

        # Correct repotype is important. Without repotype
        # metalink parser doesn't know suffix which should
        # be stripped off from the mirrors urls.
        h.setopt(librepo.LRO_REPOTYPE, librepo.LR_YUMREPO)

        # Set local mirrorlist file as mirrorlist
        if os.path.isfile(os.path.join(DESTDIR, "mirrorlist")):
            h.mirrorlist = os.path.join(DESTDIR, "mirrorlist")
        elif os.path.isfile(os.path.join(DESTDIR, "metalink.xml")):
            h.mirrorlist = os.path.join(DESTDIR, "metalink.xml")
        else:
            print "No mirrorlist of downloaded repodata available"
            sys.exit(0)

        # Download only the mirrorlist during perform() call.
        h.setopt(librepo.LRO_FETCHMIRRORS, True)

        h.perform(r)

        print "Urls in mirrorlist:"
        print h.mirrors
        print "Metalink file content:"
        pprint.pprint(h.metalink)