1#!/usr/bin/env python3
2
3"""
4librepo - download packages
5"""
6
7import os
8import os.path
9import time
10import librepo
11
12CACHE = "fastestmirror.cache"
13
14LIBREPOPKG = "librepo-1.2.1-2.fc20.x86_64.rpm"
15LAMEPKG = "lame-3.99.5-2.fc19.x86_64.rpm"
16
17if __name__ == "__main__":
18
19    # Setup logging
20    def debug_function(msg, _):
21        print(msg)
22    #librepo.set_debug_log_handler(debug_function)
23
24    # Remove packages if already exists
25    def remove_pkg(filename):
26        if os.path.exists(filename):
27            os.remove(filename)
28    remove_pkg(LIBREPOPKG)
29    remove_pkg(LAMEPKG)
30
31    # Prepare list of targets
32    packages = []
33
34    # Prepare first target
35    h1 = librepo.Handle()
36    h1.metalinkurl = "https://mirrors.fedoraproject.org/metalink?repo=fedora-20&arch=x86_64"
37    h1.repotype = librepo.YUMREPO
38    h1.fastestmirror = True
39    h1.fastestmirrorcache = CACHE
40    target = librepo.PackageTarget("Packages/l/"+LIBREPOPKG, handle=h1)
41    packages.append(target)
42
43    # Prepare second target
44    h2 = librepo.Handle()
45    h2.mirrorlisturl = "http://mirrors.rpmfusion.org/mirrorlist?repo=free-fedora-19&arch=x86_64"
46    h2.repotype = librepo.YUMREPO
47    h2.fastestmirror = True
48    h2.fastestmirrorcache = CACHE
49    target = librepo.PackageTarget(LAMEPKG, handle=h2)
50    packages.append(target)
51
52    t = time.time()
53    librepo.download_packages(packages)
54    print("Download duration: {0}s\n".format((time.time() - t)))
55
56    for target in packages:
57        print("### %s: %s" % (target.local_path, target.err or "OK"))
58        print("Local path:        ", target.local_path)
59        print("Error:             ", target.err)
60        print()
61