Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit 8ecfc18

Browse files
committed
Feat.
Feat.
1 parent 6edf2e8 commit 8ecfc18

File tree

5 files changed

+120
-23
lines changed

5 files changed

+120
-23
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.pyc
2+
__pycache__
3+
.venv
4+
dev
5+
cache

Dockerfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ RUN chmod +x /usr/local/bin/dumb-init
55

66
ARG DEVPI_VAR=5.4.0
77

8+
WORKDIR /app
9+
810
ARG MIRROR_URL=https://pypi.tuna.tsinghua.edu.cn/simple/
911
ENV MIRROR_URL=${MIRROR_URL}
1012
ENV PIP_INDEX_URL=${MIRROR_URL}
11-
COPY patch.py /tmp/patch.py
12-
RUN pip install -q --no-cache-dir "devpi-server==${DEVPI_VAR}" && \
13-
export patch_file=$(find /usr/local/lib -name "main.py"| grep devpi_server) && \
14-
python /tmp/patch.py
13+
14+
RUN pip install -q --no-cache-dir "devpi-server==${DEVPI_VAR}"
15+
16+
COPY entrypoint.py entrypoint.py
17+
18+
EXPOSE 3141
1519

1620
ENTRYPOINT ["dumb-init", "--"]
21+
22+
CMD ["python", "entrypoint.py"]

Makefile

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export MIRROR_URL := https://pypi.tuna.tsinghua.edu.cn/simple/
2+
3+
build:
4+
docker build -t pypi-mirror .
5+
6+
7+
up:
8+
docker run -itd --rm \
9+
--name pypi-mirror \
10+
-p 0.0.0.0:8080:3141 \
11+
-v $$(pwd)/cache:/cache \
12+
-e MIRROR_URL=$(MIRROR_URL) \
13+
-e CACHE_DIR=/cache \
14+
pypi-mirror
15+
16+
down:
17+
docker stop pypi-mirror
18+
19+
sh:
20+
docker run -it --rm \
21+
--name pypi-mirror \
22+
--entrypoint /bin/sh \
23+
pypi-mirror
24+
25+
# testcases
26+
test-devpi:
27+
docker run -it --rm \
28+
--name test-pypi-mirror \
29+
-e MIRROR_URL=$(MIRROR_URL) \
30+
-e CACHE_DIR=/cache \
31+
pypi-mirror
32+
33+
test-userdefine-command:
34+
docker run -it --rm \
35+
--name test-pypi-mirror \
36+
-e MIRROR_URL=$(MIRROR_URL) \
37+
-e CACHE_DIR=/cache \
38+
-e COMMAND="--include-mirrored-files --serverdir=/cache --proxy-timeout=10 --host=0.0.0.0" \
39+
pypi-mirror

README.md

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
11
# pypi-mirror
2-
本地 pypi 仓库镜像,可自定义上游镜像站 - local pypi mirror with diy mirror upstream.
2+
本地 pypi 仓库镜像,可自定义上游镜像站 - local pypi mirror with custom mirror upstream.
33

4-
*** 快速搭建 ***
4+
### 快速搭建
5+
6+
`make build up`
7+
8+
或者
59

610
```
7-
docker run -it --rm --name pypi \
8-
-p 0.0.0.0:8080:80 \
9-
-v $(pwd)/cache:/cache \
10-
-e MIRROR_URL=https://pypi.tuna.tsinghua.edu.cn/simple/ \
11-
pylab/pypi-cache \
12-
devpi-server --include-mirrored-files \
13-
--serverdir=/cache \
14-
--proxy-timeout=10 \
15-
--host=0.0.0.0 \
16-
--port=80
11+
docker run -itd --rm \
12+
--name pypi-mirror \
13+
-p 0.0.0.0:8080:3141 \
14+
-v $PWD/cache:/cache \
15+
-e MIRROR_URL=https://pypi.tuna.tsinghua.edu.cn/simple/ \
16+
-e CACHE_DIR=/cache \
17+
pylab/pypi-mirror
1718
1819
```
1920

21+
### 常见的 PyPI 镜像源
22+
23+
+ ` https://pypi.tuna.tsinghua.edu.cn/simple/ `
24+
+ ` https://mirrors.aliyun.com/pypi/simple/ `
25+
+ ` https://pypi.doubanio.com/simple/ `
26+
27+
### 测试
28+
29+
+ 测试 pip 安装
30+
31+
`pip install -i http://localhost:8080/root/pypi devpi-server`
32+
33+
+ 测试 images
34+
35+
`make test-devpi`
36+
37+
`make test-userdefine-command`
38+
2039
---
2140

2241
## devpi 指令

entrypoint.py

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
from os import environ
5-
import devpi_server
4+
from os import environ, path, listdir
65

7-
DEFAULT_MIRROR_URL = "https://pypi.org/simple/"
8-
PATCH_FILE = f"{environ['patch_file']}"
6+
from devpi_server import main as app
7+
from devpi_server.init import init as init_app
98

10-
fin = open(PATCH_FILE, "rt")
9+
split_print = lambda x: print("*" * 80 + "\n" + x + "\n" + "*" * 80)
10+
DEFAULT_MIRROR_URL = "https://pypi.tuna.tsinghua.edu.cn/simple/"
11+
MIRROR_URL = environ.get("MIRROR_URL", DEFAULT_MIRROR_URL)
12+
split_print(f"Current Pypi Mirror: {MIRROR_URL}")
13+
14+
# Hard Patch
15+
COED_MIRROR_URL = "https://pypi.org/simple/"
16+
MAIN_FILE = app.__file__
17+
18+
fin = open(MAIN_FILE, "rt")
1119
data = fin.read()
12-
data = data.replace(f"{DEFAULT_MIRROR_URL}", f"{environ['MIRROR_URL']}")
20+
data = data.replace(f"{COED_MIRROR_URL}", f"{MIRROR_URL}")
1321
fin.close()
1422

15-
fin = open(PATCH_FILE, "wt")
23+
fin = open(MAIN_FILE, "wt")
1624
fin.write(data)
1725
fin.close()
26+
27+
# Soft Patch
28+
app._pypi_ixconfig_default["mirror_url"] = f"{MIRROR_URL}"
29+
30+
CACHE_DIR = environ.get("CACHE_DIR", "/cache")
31+
if not path.exists(CACHE_DIR) or not listdir(CACHE_DIR):
32+
init_command = f"--init " \
33+
f"--serverdir={CACHE_DIR}"
34+
35+
split_print(f"[Run init] command: {init_command}")
36+
init_app(argv=init_command.split(" "))
37+
38+
runtime_command = f"--include-mirrored-files " \
39+
f"--serverdir={CACHE_DIR} " \
40+
f"--proxy-timeout=10 " \
41+
f"--host=0.0.0.0"
42+
43+
runtime_command = environ.get("COMMAND", runtime_command)
44+
split_print(f"[Run devpi] command: {runtime_command}")
45+
app.main(runtime_command.split(" "))

0 commit comments

Comments
 (0)