summaryrefslogtreecommitdiffstats
path: root/doc/examples/acquire.py
blob: 3d1e31ba6c330c540b64b4e6e8f55381701a616b (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
#!/usr/bin/python3
import os

import apt_pkg

import apt
import apt.progress.text


def get_file(fetcher, uri, destfile):
    # get the file
    af = apt_pkg.AcquireFile(fetcher, uri=uri, descr="sample descr", destfile=destfile)
    print(f"desc_uri: {af.desc_uri} -> {af.destfile}")
    res = fetcher.run()
    if res != fetcher.RESULT_CONTINUE:
        return False
    return True


apt_pkg.init()

# apt_pkg.config.set("Debug::pkgDPkgPM","1");
# apt_pkg.config.set("Debug::pkgPackageManager","1");
# apt_pkg.config.set("Debug::pkgDPkgProgressReporting","1");

cache = apt_pkg.Cache()
depcache = apt_pkg.DepCache(cache)

recs = apt_pkg.PackageRecords(cache)
list = apt_pkg.SourceList()
list.read_main_list()

# show the amount fetch needed for a dist-upgrade
depcache.upgrade(True)
progress = apt.progress.text.AcquireProgress()
fetcher = apt_pkg.Acquire(progress)
pm = apt_pkg.PackageManager(depcache)
pm.get_archives(fetcher, list, recs)
print(f"{apt_pkg.size_to_str(fetcher.fetch_needed)} ({fetcher.fetch_needed})")
actiongroup = apt_pkg.ActionGroup(depcache)
for pkg in cache.packages:
    depcache.mark_keep(pkg)

try:
    os.mkdir("/tmp/pyapt-test")
    os.mkdir("/tmp/pyapt-test/partial")
except OSError:
    pass
apt_pkg.config.set("Dir::Cache::archives", "/tmp/pyapt-test")

pkg = cache["2vcard"]
depcache.mark_install(pkg)

progress = apt.progress.text.AcquireProgress()
fetcher = apt_pkg.Acquire(progress)
# fetcher = apt_pkg.Acquire()
pm = apt_pkg.PackageManager(depcache)

print(pm)
print(fetcher)

get_file(fetcher, "ftp://ftp.debian.org/debian/dists/README", "/tmp/lala")

pm.get_archives(fetcher, list, recs)

for item in fetcher.items:
    print(item)
    if item.status == item.STAT_ERROR:
        print("Some error ocured: '%s'" % item.error_text)
    if not item.complete:
        print("No error, still nothing downloaded (%s)" % item.error_text)
    print()


res = fetcher.run()
print("fetcher.Run() returned: %s" % res)

print("now runing pm.DoInstall()")
res = pm.do_install(1)
print("pm.DoInstall() returned: %s" % res)