1#!/usr/bin/env python3
2
3"""
4librepo - example of usage
5"""
6
7import os
8import sys
9import librepo
10import pprint
11
12DESTDIR = "downloaded_metadata"
13
14if __name__ == "__main__":
15    h = librepo.Handle()
16    r = librepo.Result()
17
18    # Correct repotype is important. Without repotype
19    # metalink parser doesn't know suffix which should
20    # be stripped off from the mirrors urls.
21    h.setopt(librepo.LRO_REPOTYPE, librepo.LR_YUMREPO)
22
23    # Set local mirrorlist file as mirrorlist
24    if os.path.isfile(os.path.join(DESTDIR, "mirrorlist")):
25        h.mirrorlist = os.path.join(DESTDIR, "mirrorlist")
26    elif os.path.isfile(os.path.join(DESTDIR, "metalink.xml")):
27        h.mirrorlist = os.path.join(DESTDIR, "metalink.xml")
28    else:
29        print("No mirrorlist of downloaded repodata available")
30        sys.exit(0)
31
32    # Download only the mirrorlist during perform() call.
33    h.setopt(librepo.LRO_FETCHMIRRORS, True)
34
35    h.perform(r)
36
37    print("Urls in mirrorlist:")
38    print(h.mirrors)
39    print("Metalink file content:")
40    pprint.pprint(h.metalink)
41