本地同步pypi源两种方案

1. 使用bandersnatch

1. 要求pip3
自行依照系统安装
2. 安装
pip3 install bandersnatch
3. 初始化配置文件
bandersnatch mirror
4.修改配置文件
cat /etc/bandersnatch.conf|grep -v "^;"|grep -v "^$"
[mirror]
directory = /home/pypi
master = https://pypi.python.org
timeout = 10
workers = 10
hash-index = false
stop-on-error = false
delete-packages = true
5.开启同步
bandersnatch mirror

2. 使用pip download方式

 > 主要是下载所有的包然后自建索引

生成文件名脚本

import requests
import bs4
htext=requests.get("https://pypi.tuna.tsinghua.edu.cn/simple/").text
bobj=bs4.BeautifulSoup(htext)
with open("requirement.txt","w+") as f:    
    for a in bobj.find_all('a'):
        # print(a.string)
        f.write(a.string+'\n')

下载脚本

[root@ks ~]# vim 01_load_package.sh
#!/bin/bash
#pips=http://pypi.doubanio.com/
file=/root/requirement.txt

# 初始化下载环境(清华镜像源)
echo " Initialize download environment "
mkdir /root/.pip/
cat << END > /.pip/pip11.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = https://pypi.tuna.tsinghua.edu.cn
END

while read line 
do
    mkdir /data/local_pip/$line
    pip download $line -d /data/local_pip/$line/
done < $file

echo "pip is already。"

建立索引脚本

[root@master ~]# vim 02_make_init_index.sh
#!/bin/bash  

# set -x  

if [ $# -ne 1 ]; then
    echo "Usage: $0 packages_dir"  
    exit 1
fi

[ ! -d $1 ] && echo "Error: you should provide a directory." && exit 1

dest=$1
dest=${dest%/}

if ! echo $dest |grep -q "^/"; then
    echo "Error: please use the absolute path."  
    exit 1
fi

if ! ls $dest | egrep -q "(gz|zip)$"; then
   echo "Note: nothing need to do."  
   exit 0
fi

#---------------------------------------------
TOPDIR=$(cd $(dirname "$0") && pwd)
tmpdir=`mktemp -d`
#---------------------------------------------

for i in `ls ${dest}/{*.gz,*.zip} 2>/dev/null`
do
    rm -rf $tmpdir/*
    cp $i $tmpdir
    cd $tmpdir
    package_arch_name=`ls`
    if echo $package_arch_name | grep -q "gz$"; then
        tar xf $package_arch_name
        gz_suffix=1
    else
        unzip $package_arch_name
        gz_suffix=0
    fi
    rm -rf $package_arch_name
    package_name=`ls`
    cd $package_name
    if ls |grep -q "egg-info"; then
        python setup.py egg_info
        python setup.py build
        cd ..
        if [ $gz_suffix -eq 1 ]; then
            tar czf $package_arch_name $package_name
        else
            zip -r $package_arch_name $package_name
        fi
        rm -rf $i
        cp $package_arch_name $dest/
    fi
    cd $TOPDIR
done

rm -rf $tmpdir

启用pip服务脚本

#!/bin/bash
## 安装pypiserver
pip install pypiserver
## 设置开机自启动
cat << EOF >> /etc/rc.local 
pypi-server /data/localpip &>/var/log/pypi-server.log &
exit 0
EOF

## 加载rc.local,会执行上边的命令
/usr/bin/bash /etc/rc.local

测试

[root@ks~]# cat .pip/pip.conf 
[global]
trusted-host = 172.16.1.99
index-url = http://172.16.1.99:8080/simple

引用:
https://www.cnblogs.com/answerThe/p/11321740.html
https://www.jianshu.com/p/4bc6a2c59395

posted @ 2020-12-19 17:32  lvusyy  阅读(2710)  评论(0编辑  收藏  举报