a script to download package sources
by giomat from LinuxQuestions.org on (#6H5C1)
I was looking for a quick way to download all the source files of a slackware package, something like "apt-get source" in debian, but it seems the common suggestion is to do manual downloads from the packages source page in a slackware mirror, so I made a script to do all these downloads given just the package name.
The tricky part was figuring out the series of the requested package to compose the corresponding url, luckily slackpkg info provides it with a bit of scraping.
For now kde packages are not supported since the kde series source directory has a quite unusual layout, any tips to improve the script would be much appreciated
Code:#!/bin/bash
#
# this script downloads the source directory of a slackware package
set -e
PKG_NAME="$1"
if [ -z "$PKG_NAME" ]; then
echo "Please provide a package name as argument"
exit 1
fi
# determine architecture
ARCH=$(uname -m)
if [ "$ARCH" == "x86_64" ]; then
ARCH="64"
else
ARCH=""
fi
# read slackware version
source "/etc/os-release"
if [ "$VERSION_CODENAME" == "current" ]; then
SLACK_VERSION="slackware$ARCH-current"
else
SLACK_VERSION="slackware$ARCH-15.0"
fi
# build url to source directory
SRC_URL="https://mirrors.slackware.com/slackware/$SLACK_VERSION/source"
# get package series using slackpkg info
PKG_SERIES=$(/usr/sbin/slackpkg info "$PKG_NAME" | grep "PACKAGE LOCATION" | sed 's/^.*\///')
if [ "$PKG_NAME" == "kernel" ]; then
PKG_SERIES="k"
fi
if [ -z "$PKG_SERIES" ]; then
echo "package not found"
exit 1
fi
if [ "$PKG_SERIES" == "kde" ]; then
echo "kde packages not yet supported :("
exit 1
fi
# handle linux kernel special case
# TODO: download also kernel-configs and slack-desc folders
if [ "$PKG_NAME" == "kernel" ]; then
SRC_URL="$SRC_URL/$PKG_SERIES"
else
SRC_URL="$SRC_URL/$PKG_SERIES/$PKG_NAME"
fi
echo "Downloading $PKG_NAME source and slackbuild from $SRC_URL"
# optionally append the package version to the directory name where files will be downloaded
#PKG_VER=$(/usr/sbin/slackpkg info "$PKG_NAME" | grep "PACKAGE NAME" | awk -F"-x86_64" '{ print $1 }' | awk -F"-" '{ print $NF }')
#mkdir -p "$PKG_NAME-$PKG_VER" && cd "$PKG_NAME-$PKG_VER"
# create a directory to store the package source files
mkdir -p "$PKG_NAME" && cd "$PKG_NAME"
# find links to source files inside page html (a bit ugly)
# TODO: use ftp to download source directory
PKG_PAGE=$(wget -qO- "$SRC_URL")
FILES_LIST=$(echo "$PKG_PAGE" | grep Details | sed -n 's/.*href="\([^"]*\).*/\1/p' | sed 's/.mirrorlist//g')
if [ "$PKG_NAME" == "kernel" ]; then
FILES_LIST="kernel-configs\nslack-desc\n$FILES_LIST"
fi
# download all source files
for file in $(echo -en "$FILES_LIST"); do
wget -q --show-progress "$SRC_URL/$file"
done
The tricky part was figuring out the series of the requested package to compose the corresponding url, luckily slackpkg info provides it with a bit of scraping.
For now kde packages are not supported since the kde series source directory has a quite unusual layout, any tips to improve the script would be much appreciated
Code:#!/bin/bash
#
# this script downloads the source directory of a slackware package
set -e
PKG_NAME="$1"
if [ -z "$PKG_NAME" ]; then
echo "Please provide a package name as argument"
exit 1
fi
# determine architecture
ARCH=$(uname -m)
if [ "$ARCH" == "x86_64" ]; then
ARCH="64"
else
ARCH=""
fi
# read slackware version
source "/etc/os-release"
if [ "$VERSION_CODENAME" == "current" ]; then
SLACK_VERSION="slackware$ARCH-current"
else
SLACK_VERSION="slackware$ARCH-15.0"
fi
# build url to source directory
SRC_URL="https://mirrors.slackware.com/slackware/$SLACK_VERSION/source"
# get package series using slackpkg info
PKG_SERIES=$(/usr/sbin/slackpkg info "$PKG_NAME" | grep "PACKAGE LOCATION" | sed 's/^.*\///')
if [ "$PKG_NAME" == "kernel" ]; then
PKG_SERIES="k"
fi
if [ -z "$PKG_SERIES" ]; then
echo "package not found"
exit 1
fi
if [ "$PKG_SERIES" == "kde" ]; then
echo "kde packages not yet supported :("
exit 1
fi
# handle linux kernel special case
# TODO: download also kernel-configs and slack-desc folders
if [ "$PKG_NAME" == "kernel" ]; then
SRC_URL="$SRC_URL/$PKG_SERIES"
else
SRC_URL="$SRC_URL/$PKG_SERIES/$PKG_NAME"
fi
echo "Downloading $PKG_NAME source and slackbuild from $SRC_URL"
# optionally append the package version to the directory name where files will be downloaded
#PKG_VER=$(/usr/sbin/slackpkg info "$PKG_NAME" | grep "PACKAGE NAME" | awk -F"-x86_64" '{ print $1 }' | awk -F"-" '{ print $NF }')
#mkdir -p "$PKG_NAME-$PKG_VER" && cd "$PKG_NAME-$PKG_VER"
# create a directory to store the package source files
mkdir -p "$PKG_NAME" && cd "$PKG_NAME"
# find links to source files inside page html (a bit ugly)
# TODO: use ftp to download source directory
PKG_PAGE=$(wget -qO- "$SRC_URL")
FILES_LIST=$(echo "$PKG_PAGE" | grep Details | sed -n 's/.*href="\([^"]*\).*/\1/p' | sed 's/.mirrorlist//g')
if [ "$PKG_NAME" == "kernel" ]; then
FILES_LIST="kernel-configs\nslack-desc\n$FILES_LIST"
fi
# download all source files
for file in $(echo -en "$FILES_LIST"); do
wget -q --show-progress "$SRC_URL/$file"
done