É um script bash
em vez de python-apt
, mas eu encontrei dpkg-offline
que contém lógica semelhante. Eu olhei como funciona e consegui criar um script próprio para fazer o download necessário - de forma reproduzível e como um usuário comum não-root:
#!/bin/bash
set -e
die() {
exec >&2
printf '%s\n' "$@"
exit 1
}
test "$UID" != "0" || die \
"Do not run '$0' as root, because it will break your packages."
test -n "$1" || die \
"Usage: $0 <package-dir> [package] ..." \
" package may be followed by _ - + &m etc; like 'aptitude install'" \
" local packages must be listed in package-dir/Packages.gz" \
" downloaded packages are saved to package-dir/ubuntu"
PACKAGE_DIR="$1"; shift
BASE_DIR=$(mktemp -d)
trap 'rm -r "$BASE_DIR"' EXIT
(
cd $BASE_DIR
# Skeleton directory structure
mkdir -p \
etc/apt/preferences.d \
etc/apt/sources.list.d \
var/cache/apt/archives \
var/lib/apt/lists \
var/lib/aptitude \
var/lib/dpkg \
var/log
# Use GPG keys from host, and create empty dpkg status
cp /etc/apt/trusted.gpg etc/apt
touch var/lib/dpkg/status
cat >etc/apt/sources.list <<-EOF
# Locally-built packages
deb file://$PACKAGE_DIR /
# Upstream distribution to pillage
deb http://gb.archive.ubuntu.com/ubuntu/ trusty main restricted universe
EOF
# Don't let user's ~/.aptitude/* influence anything
export HOME="$BASE_DIR"
APT_OPTIONS=(-q2 \
-o "Apt::Architecture=amd64" \
-o "Apt::Install-Recommends=0" \
-o "Dir::Etc=$BASE_DIR/etc/apt" \
-o "Dir::State=$BASE_DIR/var/cache/apt" \
-o "Dir::Cache=$BASE_DIR/var/cache/apt" \
-o "Dir::Cache::Archives=$BASE_DIR/var/cache/apt/archives" \
-o "Dir::Log=$BASE_DIR/var/log" \
-o "Dir::State::Lists=$BASE_DIR/var/lib/apt/lists" \
-o "Dir::State::status=$BASE_DIR/var/lib/dpkg/status" \
-o "DPkg::Options::--admindir=$BASE_DIR/var/lib/dpkg" \
)
APTITUDE="aptitude ${APT_OPTIONS[@]}"
$APTITUDE update
$APTITUDE --download-only -y --allow-untrusted install "$@"
)
# We now have all required package files download into cache; put them
# into output directory.
ln -t "$PACKAGE_DIR/ubuntu" -vf var/cache/apt/archives/*.deb
# Create updated package index
( cd "$PACKAGE_DIR" && dpkg-scanpackages -m . | gzip -c >Packages.gz )
Eu provavelmente poderia usar isso para criar um programa em Python para fazer o mesmo, mas o script de shell provavelmente será adequado para os meus propósitos.