Skip to content

Instantly share code, notes, and snippets.

@dceoy
Created February 15, 2018 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dceoy/6ff0d63b767cfe1dc17ab782c1a62234 to your computer and use it in GitHub Desktop.
Save dceoy/6ff0d63b767cfe1dc17ab782c1a62234 to your computer and use it in GitHub Desktop.
[Shell] Download Python and R packages
#!/usr/bin/env bash
#
# Usage:
# ./download_pkg.sh \
# --py-pkg ./py_requirements.txt \
# --r-pkg ./r_requirements.txt \
# --py-dir ./src/py \
# --r-dir ./src/r
#
# Options:
# -h, --help Print usage
# --py-pkg Read a text file and download listed Python packages in that
# --r-pkg Read a text file and download listed R packages in that
# --py-dir Set a destination directory for Python packages [default: ./py]
# --r-dir Set a destination directory for R packages [default: ./r]
#
# Description:
# A script to download sources of Python and R packages
set -e
# Set default values
SCRIPT_PATH="${0}"
PY_DIR='./py'
R_DIR='./r'
PY_TXT=false
R_TXT=false
function print_usage {
sed -ne '1,2d; /^#/!q; s/^#$/# /; s/^# //p;' ${SCRIPT_PATH}
}
# Parse options
while [[ -n "${1}" ]]; do
case "${1}" in
'-h' | '--help' )
print_usage && exit 0
;;
'--debug' )
set -x && shift 1
;;
'--py-pkg' )
PY_TXT="${2}" && shift 2
;;
'--py-dir' )
PY_DIR="${2}" && shift 2
;;
'--r-pkg' )
R_TXT="${2}" && shift 2
;;
'--r-dir' )
R_DIR="${2}" && shift 2
;;
* )
print_usage && exit 1
;;
esac
done
set -u
# Check requirements
pip --version
R --version
# Download Python packages
if [[ -f "${PY_TXT}" ]]; then
[[ -d "${PY_DIR}" ]] || mkdir -p ${PY_DIR}
pip download --dest ${PY_DIR} --requirement ${PY_TXT}
echo '
Install Python packages:
$ pip install /path/to/py-dir/*'
fi
# Download R packages
if [[ -f "${R_TXT}" ]]; then
[[ -d "${R_DIR}" ]] || mkdir -p ${R_DIR}
R -q -e "download.packages(read.table('${R_TXT}')[, 1], destdir = '${R_DIR}', type = 'source', repos = 'https://cran.rstudio.com/');"
echo '
Install R packages:
$ R CMD INSTALL /path/to/r-dir/*'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment