搭建私有 Pypi 源仓库

当说到 pip 镜像源时,网上总推荐使用 pypiserver、bandersnatch 搭建个人源和pypi镜像源。前者只能先下载后使用,后者会下载全部的库文件。之前我使用 bandersnatch,不经意间磁盘的占用已经远远超过300G,但实际使用中个人远远用不了那么多。因此,我最终选择了 Devpi 作为长久使用的方案。

Pip2pi

部署

  • 使用 PIP 进行安装
    # pip install pip2pi
    
  • 使用 Git 进行安装
    # git clone https://github.com/wolever/pip2pi
    # cd pip2pi
    # python setup.py install
    

创建存放 Python 库的文件夹

  • 创建并进入该文件夹
    # mkdir /pypi && cd /pypi
    

下载 Python 库

  • 下载单个 Python 库
    # pip2tgz . requests==2.22.0
    
  • 批量下载 Python 库
    # pip2tgz . -r list/requirements.txt
    

创建索引

  • 创建索引,注意该文件夹内不能有多余的其他文件
    # dir2pi .
    
  • 更新索引
    pip2acmeco uliweb=0.2.6 
    pip2acmeco -r list/requirements.txt
    

Nginx 配置前端反向代理

  • 将配置写入文件
    # cat << EOF > /etc/nginx/conf.d/pypi
    server {
    listen 5401;
    root /pypi;
    access_log logs/doge2.pypi.access.log main;
    location /{
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    limit_rate_after 5m;
    limit_rate 900k;
    }
    }
    EOF
    
  • 测试并重启 Nginx
    # nginx -t && systemctl restart nginx
    

使用

  • pip 方法
    # pip install requests -i http://10.64.128.208:5104/simple --trusted-host 10.64.128.208
    
  • easy_install 方法
    # easy_install -i http://10.64.128.208:5104/simple requests --trusted-host 10.64.128.208
    

Bandersnatch

安装及配置

  1. PIP 安装
    # pip install bandersnatch
    
  2. 生成配置文件
    # bandersnatch mirror
    
  3. 修改配置文件 /etc/bandersnatch.conf
    [mirror]
    ; The directory where the mirror data will be stored.
    directory = /srv/pypi
    ; The PyPI server which will be mirrored.
    ; master = https://testpypi.python.org
    ; scheme for PyPI server MUST be https
    master = https://pypi.python.org
    

下载源及同步源

  1. 下载所有的包到本地
    # bandersnatch -c /etc/bandersnatch.conf mirror
    
  2. 更新本地的包
    # bandersnatch -c /etc/bandersnatch.conf mirror
    

配置 NGiNX

  • 配置 NGiNX 的静态文件服务器
    server {
     listen *:80;
     server_name pypi_server;
     root /srv/pypi/web;
     autoindex on;
     charset utf-8;
    }
    

Devpi

Devpi 较为简单,因此可以做成 Docker 镜像文件方便迁移使用。
  • Dockerfile
    FROM python:3
    MAINTAINER  becivells <becivells@gmail.com> 
    RUN pip install devpi devpi-web
    RUN mkdir /devpi
    VOLUME /devpi
    EXPOSE 3141
    ADD run.sh /
    CMD ["/bin/bash","/run.sh"]
    
  • run.sh
    #!/bin/bash
    set -e
    set -x
    export DEVPI_SERVERDIR=/devpi
    [[ -f $DEVPI_SERVERDIR/.serverversion ]] || initialize=yes
    if [[ $initialize = yes ]]; then
     devpi-server --port 3141 --serverdir $DEVPI_SERVERDIR --init
    fi
    devpi-server --start --host 0.0.0.0 --port 3141 --serverdir $DEVPI_SERVERDIR
    
  • 运行
    # docker run -d -p 3141:3141 -v /docker/devpi:/devpi devpi
    

题外话

这样搭建完成之后,每次下载 Python 库的时候都需要追加 --trusted-host 参数,非常繁琐,因此最好的办法是追加一些配置,让下载变得一劳永逸。

Windows

  • 文件命名为 pip.ini 并存放在 %APPDATA%\pip\pip.ini%HOME%\pip\pip.ini
    [global]
    timeout = 60
    index-url = http://10.64.128.208:5104/simple
    trusted-host = 10.64.128.208:5104
    

Linux

  • 文件命名为 pip.conf 并存放在 $HOME/.config/pip/pip.conf$HOME/.pip/pip.conf
    [global]
    timeout = 60
    index-url = http://10.64.128.208:5104/simple
    trusted-host = 10.64.128.208:5104
    

参考资料

评论