Skip to content

Instantly share code, notes, and snippets.

@b-long
Forked from jimhester/has_external_connection.sh
Created October 15, 2015 23:47
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 b-long/f80a39e1f6e9804d2ab6 to your computer and use it in GitHub Desktop.
Save b-long/f80a39e1f6e9804d2ab6 to your computer and use it in GitHub Desktop.
Bash function to detect if an R command uses an external connection of anykind. Designed to be used with R CMD check
#!/bin/bash
# Bash function to detect if an R command uses an external connection of
# any kind. Designed to be used with R CMD check
function has_external_connection {
DIR=$(mktemp -d)
# Use a local repository so we don't have to make an external connection for
# `available.packages()`
mkdir -p $DIR/src/contrib
wget -q -O $DIR/src/contrib/PACKAGES 'https://cran.rstudio.com/src/contrib/PACKAGES'
RPROFILE=$DIR/RProfile
echo "options(repos = c(CRAN='file://$DIR'))" > $RPROFILE
# Write trace results to outfile
OUTFILE=$(tempfile -d $DIR)
# Run the command using strace and trace only network system calls
strace -E R_PROFILE_USER=$RPROFILE -f -qq -o $OUTFILE -e trace=network "$@"
# Filter out
# 1. child process start and exit signals
# 2. local socket connections
# 3. Connection continuation lines
grep -q -E -v \
-e "^[0-9]+ +[+-]{3}" \
-e "[AP]F_LOCAL" \
-e "^[0-9]+ +<\.\.\. connect resumed" \
$OUTFILE
return $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment