apt-get.sh code
I have something working now, theres heaps it doesnt do, so i will just
say what it does.
Only tried under bash, can do update or install
Update reads sources.list, fetch Package.gz using wget or cp to
/var/state/apt/lists/ and changes the filename.
Install accepts multiple package names as arguments, searches files in
/var/state/apt/lists in the order specified in sources.list to see if
package is available, fetch package from coresponding site to
/var/cache/apt/archive then uses dpkg to install the package.
Doesnt do dependency checking yet.
It is attached, i will put it into cvs tommorow, i guess i will put it
in /debian-installer/tools
Glenn
#!/bin/sh
# Partitial implementation of debians apt-get command
# GPL Glenn McGrath
# Shell commands used
#
# BUGS/LIMITATIONS
# update: always fetches rather than checking if the file to download is newer
# doesnt fetch Release
SRCLIST="/etc/apt/sources.list"
LISTDIR="/var/state/apt/lists/"
HOSTTYPE="i386"
SITE_LIST=
PKG_LIST=
fetchFile() {
local METHOD=`echo $1|cut -d ':' -f 1`
case $(echo $1| cut -d ':' -f 1) in
ftp)
echo "uri "$1
wget -nv $1
;;
http)
wget -nv $1
;;
file)
cp $1 .
;;
*)
echo "unknown fetch method"
esac
}
update() {
for SITE in $SITE_LIST
do
cd $LISTDIR
if [ -e "Packages.gz" ]
then
rm Packages.gz
fi
fetchFile $SITE".gz"
if [ -s "Packages.gz" ]
then
gunzip Packages.gz
FILE=`echo $SITE| sed 's/^.*:\/*//'|sed 's/\//_/g'`
mv -f Packages $FILE
fi
done
}
checkDeps() {
# check dependencies of $*, if necessary add dependencies to $PKG_LIST and try again
echo "dependency checks not implemented yet"
}
parseLine() {
local TYPE=$1
local URI=`echo $2 |sed 's/\/$//'`
local DIST=$3
shift 3
local SECTIONS=$*
local NEWSITE=
for SECTION in $SECTIONS
do
NEWSITE=`echo "/dists/"$DIST"/"$SECTION"/binary-"$HOSTTYPE"/Packages" | sed 's/\/\//\//g'`
SITE_LIST=$SITE_LIST" "$URI$NEWSITE
done
}
parseSourceList() {
LASTLINENO=`wc -l <$SRCLIST`
CURLINENO="1"
while [ $CURLINENO -le $LASTLINENO ]
do
CURLINE=`head -n $CURLINENO $SRCLIST | tail -n 1 -`
VALIDLINE=`echo $CURLINE |sed -n '/^deb /p'`
if [ "$VALIDLINE" != "" ]
then
parseLine $VALIDLINE
fi
CURLINENO=`expr $CURLINENO + 1`
done
}
install() {
local TMPFILE="/tmp/pkgentry.tmp"
local FETCHDIR="/var/cache/apt/archives/"
PKG_LIST=`echo $* |cut -d ' ' -f 2-`
checkDeps $PKG_LIST
for PKGNAME in $PKG_LIST
do
if [ -e $TMPFILE ]
then
rm -f $TMPFILE
fi
for SITE in $SITE_LIST
do
FILE=`echo $SITE| sed 's/^.*:\/*//'|sed 's/\//_/g'`
FETCH=`echo $FILE | sed 's/.*:\/*//' | sed 's/_dists.*//'`
FETCH_HOST=`echo $FETCH | cut -d '_' -f 1`
FETCH_PATH=`echo $FETCH | cut -d '_' -f 2-`
sed -n /^Package:."$PKGNAME"$/,/^\s*$/p $LISTDIR$FILE > $TMPFILE
PKGPATH=`grep "Filename: " $TMPFILE |cut -d ' ' -f 2`
for SITE in $SITE_LIST
do
SITE_HOST=`echo $SITE| sed 's/.*:\/*//'|cut -d '/' -f 1`
SITE_PATH=`echo $SITE| sed 's/.*:\/*//'|cut -d '/' -f 2-|sed 's/\//_/g'|sed 's/_dists.*//'`
if [ "$SITE_HOST" = "$FETCH_HOST" ] && [ "$SITE_PATH" = "$FETCH_PATH" ]
then
FILENAME=`echo $SITE"/"$PKGPATH | sed 's/^.*\///'`
cd $FETCHDIR"/partial"
URI=`echo $SITE|sed 's/\/dists.*//'`
fetchFile $URI"/"$PKGPATH
if [ -s $FILENAME ]
then
mv $FILENAME $FETCHDIR
cd $FETCHDIR
dpkg -i $FILENAME
break 2
else
echo "failed to fetch "$FILENAME
fi
fi
done
done
done
}
usage() {
echo "apt-get [update|install]"
}
# Main functionality starts here
parseSourceList
case $1 in
update)
update
;;
install)
if [ "$2" = "" ]
then
echo "You need to specify a package"
usage
else
install $*
fi
;;
*)
usage
esac
Reply to: