2020-08-04 移植Python环境中的包

2020-08-05  本文已影响0人  YPCHEN1992

py_setup.sh

#!/usr/bin/env bash

# Function
function usage() {
    echo "SYNOPSIS:"
    echo "    This Bash Script Was Written To Install Python Packages."
    echo "USAGE:"
    echo "    py_setup.sh requirement.yml"
    echo "YML:"
    echo "    pip freeze > requirement.yml"
    exit 1
}

# Check input requirement.txt
if [[ $# != 1 ]]; then
    usage
fi

if [[ ! -e $1 ]]; then
    echo "[ERROR] Invalid requirement.yml"
    usage
fi

# Round 1
for package in $(cat $1)
do
    pip install ${package} -i https://pypi.tuna.tsinghua.edu.cn/simple/
    if [[ $? -eq 0 ]]; then
        echo ${package} >> py_setup.installed.txt
    else
        echo ${package} | sed 's/=.*$//' >>  py_setup.1round.failed.txt
    fi
done

# Round 2
if [[ ! -e py_setup.1round.failed.txt ]]; then
    echo 'Python environment setup done!'
    exit 0
else
    for packeage in $(cat py_setup.1round.failed.txt)
    do
        pip install ${package}
        if [[ $? -eq 0 ]]; then
            echo ${package} >> py_setup.installed.txt
        else
            echo ${package} | sed 's/=.*$//' >>  py_setup.2round.failed.txt
        fi
    done
fi

# Final check
if [[ ! -e py_setup.2round.failed.txt ]]; then
    echo 'Python environment setup done!'
else
    echo "[ERROR] Failed to indtall some package in py_setup.1round.failed.txt"
fi
exit 0

有待修改

上一篇下一篇

猜你喜欢

热点阅读