Last active
August 29, 2015 13:56
-
-
Save apokalyptik/9263626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Please note: This script is likely very brittle... | |
# | |
ORIGIN=$1 | |
OUT_DIR=$2 | |
if [ "$2" == "" ] || [ "$1" == "" ]; then echo "Please specify origin requirements file and output directory"; exit 4; fi | |
if [ ! -f "$ORIGIN" ]; then echo "$1 doesnt seem to be a file"; exit 1; fi | |
if [ -e "$OUT_DIR" ]; then echo "$2 exists, cowardly refusing to overwrite"; exit 2; fi | |
mkdir -p $OUT_DIR | |
if [ $? -ne 0 ]; then echo "Failed creating directory $2"; exit 3; fi | |
# We need pip >= 1.3 (i think) to get dependencies from the "pip show" command | |
pip install --upgrade pip==1.5.4 1>/dev/null 2>/dev/null | |
if [ $? -ne 0 ]; then echo "Could not run 'pip install pip==1.5.4'"; exit 6; fi | |
function resolve() { | |
for d in $(pip show $1 | grep Requires: | cut -d' ' -f2- | sed -r -e 's/, */ /g' | grep -Ev '^$'); do | |
echo $FOUND | grep -q '#'$d'#' | |
if [ $? -eq 0 ]; then continue; fi | |
echo -e "\t\tdependency: $d" 1>&2 | |
FOUND="$FOUND#$d#" | |
echo "$d" | |
resolve $d | |
done | |
} | |
function resolve_reqs() { | |
echo "Resolving top level requirements..." 1>&2 | |
export FOUND="" | |
for i in $(cat $ORIGIN | cut -d= -f1); do | |
echo $FOUND | grep -q '#'$i'#' | |
if [ $? -eq 0 ]; then | |
echo -e "\tfound $i but we have already added it elsewhere" 1>&2 | |
continue | |
fi | |
echo -e "\tfound $i" 1>&2 | |
FOUND="$FOUND#$i#" | |
resolve $i | |
echo $i | |
done | |
} | |
function download_reqs() { | |
# Step 2... Compute a list of all dependencies | |
P="" | |
for w in $(resolve_reqs); do | |
let found=$found+1 | |
# Step 3... Download all of those dependencies | |
WANT=$(grep "$w==" $ORIGIN) | |
if [ "$WANT" = "" ]; then WANT=$(pip freeze 2>/dev/null | grep "$w=="); fi | |
P="$P $WANT" | |
done | |
echo -e "\n\npip install --download $OUT_DIR --no-install $P\n\n" 1>&2 | |
for F in $(pip install --download $OUT_DIR --no-install $P 2>/dev/null | grep -E '^\s*Saved\s' | sed -r -e 's/^\s*Saved\s*//g'); do | |
echo $(basename $F) | |
done | |
} | |
# Step 1... to do introspection deeply (dependencies) we need to actually install all this crap | |
pip install --upgrade --requirement $ORIGIN 1>/dev/null 2>/dev/null | |
export found=0 | |
# Step 4... Write out new requirements file | |
download_reqs > $OUT_DIR/local-requirements | |
# Step 5... A little bit of sanity | |
if [ $(ls -1 $OUT_DIR | grep -v local-requirements | wc -l) -ne $found ]; then | |
echo "Something went wrong. Expected $found downloaded files and got $(ls -1 $OUT_DIR | grep -v local-requirements | wc -l)" | |
exit 5 | |
fi | |
echo "Okay... You should now be able to copy" | |
echo "$OUT_DIR" | |
echo "to another machine, and then (from inside" | |
echo "the directory) run the following:" | |
echo | |
echo -e "\tpip install --upgrade -r ./local-requirements\n\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given the requirements file
running the following
produces this output
And a ./out directory containing:
where the local-requirements file looks like: