import apt
import glob
import os.path
import tempfile
def _get_local_sources_list():
sources_list = glob.glob('/etc/apt/sources.list.d/*.list')
sources_list.append('/etc/apt/sources.list')
sources = ''
for source in sources_list:
with open(source) as f:
sources += f.read()
return sources
rootdir = tempfile.mkdtemp()
sources = _get_local_sources_list()
os.makedirs(os.path.join(rootdir, 'etc', 'apt'), exist_ok=True)
srcfile = os.path.join(rootdir, 'etc', 'apt', 'sources.list')
with open(srcfile, 'w') as f:
f.write(sources)
# Do not install recommends
apt.apt_pkg.config.set('Apt::Install-Recommends', 'False')
for key in 'Dir::Etc::Trusted', 'Dir::Etc::TrustedParts':
apt.apt_pkg.config.set(key, apt.apt_pkg.config.find_file(key))
apt_cache = apt.Cache(rootdir=rootdir, memonly=True)
apt.apt_pkg.config.clear("APT::Update::Post-Invoke-Success")
progress = apt.progress.text.AcquireProgress()
apt_cache.update(sources_list=srcfile, fetch_progress=progress)
apt_cache.open()
apt_cache['wget'].mark_install()
downloaddir = tempfile.mkdtemp()
apt.apt_pkg.config.set("Dir::Cache::Archives", downloaddir)
apt_cache.fetch_archives(progress=progress)