Skip to content

Commit 4216b74

Browse files
committed
Add rustup cache
1 parent 960ccf5 commit 4216b74

5 files changed

Lines changed: 155 additions & 2 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# repocache
2-
Universal caching and proxying repository server for pypi/maven/npm/yum, speedup local using.
2+
Universal caching and proxying repository server for pypi/maven/npm/yum/rustup, speedup local using.
33

44
## How to startup?
55
```bash
@@ -84,6 +84,14 @@ trusted-host=127.0.0.1:5000
8484
index-url=http://127.0.0.1:5000/pypi/simple
8585
```
8686

87+
## How to use repocache as Rustup?
88+
```bash
89+
export RUSTUP_DIST_SERVER=http://127.0.0.1:5000/rust/default
90+
91+
curl http://127.0.0.1:5000/rust/rustup.sh | sh
92+
```
93+
94+
8795
## Settting file
8896
See [default.cfg](default.cfg)
8997

default.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,6 @@ url = http://mirrors.huaweicloud.com/centos/7.9.2009/sclo/x86_64/rh
9595
User-Agent = urlgrabber/3.10 yum/3.4.3
9696
# https://www.softwarecollections.org/en/scls/rhscl/rh-python36/
9797
# http://mirrorlist.centos.org/?arch=x86_64&release=7&repo=sclo-rh
98+
99+
[rust.upstream.default]
100+
url = https://static.rust-lang.org

repocache/rust.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
import json
2+
import logging
3+
4+
from flask import render_template, request, make_response, url_for
5+
from flask.helpers import send_file
6+
from tornado.util import ObjectDict
7+
from werkzeug.exceptions import NotFound
8+
9+
from repocache.modular_view import ModularView, expose
10+
from repocache.vendor import Vendor
11+
12+
logger = logging.getLogger(__name__)
13+
14+
15+
def create_upstream(name, section):
16+
'''create dict from section of ConfigParser'''
17+
tail = name[len('rust.upstream.'):]
18+
return ObjectDict(name=tail, **section)
19+
20+
21+
# RUSTUP_DIST_SERVER=https://static.rust-lang.org
22+
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
23+
24+
25+
class RustupRepository(ModularView, Vendor):
26+
@expose("/")
27+
def index(self):
28+
'''TODO: list cached packages'''
29+
return self.upstreams
30+
31+
@expose("/index.html")
32+
def index_html(self):
33+
return render_template('rust/index.html')
34+
35+
@expose("/rustup.sh")
36+
def sh(self):
37+
body = render_template('rust/rustup.sh')
38+
39+
resp = make_response(body)
40+
resp.headers.set('Content-Type', 'text/plain')
41+
return resp
42+
43+
@expose("/<string:un>")
44+
def root(self, un):
45+
ud = self.upstreams.get(un)
46+
if not ud:
47+
raise NotFound
48+
return ud
49+
50+
@expose("/<string:un>/<path:filepath>")
51+
def file(self, un, filepath):
52+
# http://192.168.1.2:5000/rust/default/dist/channel-rust-stable.toml.sha256
53+
ud = self.upstreams.get(un)
54+
if ud is None:
55+
raise NotFound
56+
57+
ud = ObjectDict(ud)
58+
url = ud.url
59+
del ud['url']
60+
61+
f = self.fetch_or_load_binary(
62+
f'rust/{un}/{filepath}',
63+
fetch_handle=lambda: self.fetch(f'{url}/{filepath}', **ud),
64+
)
65+
66+
if filepath.endswith('.xz'):
67+
mimetype = 'application/x-tar'
68+
else:
69+
mimetype = 'application/octet-stream'
70+
return send_file(f, mimetype=mimetype)
71+
72+
@expose("/<string:un>/-/v1/search")
73+
def search(self, un):
74+
ud = self.upstreams.get(un)
75+
if ud is None:
76+
raise NotFound
77+
78+
resp = self.fetch(f'{ud.url}/-/v1/search', params=request.args, **ud)
79+
return resp.content
80+
81+
# -/rust/v1/security/audits/quick
82+
@expose("/<string:un>/-/<path:left>", methods=('POST',))
83+
def other(self, un, left):
84+
print('post:', request)
85+
return ''
86+
87+
def __init__(self, config):
88+
ModularView.__init__(
89+
self,
90+
name='rust',
91+
url_prefix='/rust',
92+
)
93+
94+
self.upstreams = {} # upstream name => Dict
95+
96+
for section_name in config:
97+
if section_name.startswith('rust.upstream.'):
98+
u = create_upstream(section_name, config[section_name])
99+
self.upstreams[u.name] = u
100+
101+
def _fetch(self, un, path):
102+
ud = self.upstreams.get(un)
103+
104+
if ud is None:
105+
raise NotFound
106+
107+
url = '{}/{}'.format(
108+
ud.url,
109+
path,
110+
)
111+
112+
ud = ObjectDict(ud)
113+
del ud['url']
114+
115+
return self.fetch(url, **ud)
116+
117+
def ensure_package(self, un, name):
118+
# TODO: local
119+
jd = self.fetch_or_load_json(
120+
f'rust/{un}/{name}.json',
121+
fetch_handle=lambda: self._fetch(un, name),
122+
)
123+
124+
# replace `dist.tarball` as local url
125+
for v, vd in jd.get('versions').items():
126+
# if un in
127+
ud = self.upstreams.get(un)
128+
prefix = f'{ud.url}/{name}/-/'
129+
pos = vd.dist.tarball.find(prefix)
130+
if pos == 0:
131+
filename = vd.dist.tarball[pos + len(prefix):]
132+
vd.dist._url = vd.dist.tarball
133+
vd.dist.tarball = url_for(
134+
'npm.package_file',
135+
un=un,
136+
name=name,
137+
filename=filename,
138+
_external=True,
139+
)
140+
return jd

repocache/server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import repocache.pypi as pypi
88
import repocache.yum as yum
99
import repocache.npm as npm
10+
import repocache.rust as rust
1011

1112
mod = Blueprint(
1213
'server',
@@ -27,6 +28,7 @@ def __init__(self, config):
2728
'mvn': maven.Maven(config),
2829
'yum': yum.YumRepository(config),
2930
'npm': npm.NpmRepository(config),
31+
'rust': rust.RustupRepository(config),
3032
}
3133
for _, p in vendors.items():
3234
self.register_blueprint(p.create_blueprint())

repocache/vendor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def fetch(self, url, params=None, **kw):
6565
auth = requests.auth.HTTPBasicAuth(kw['user'], kw['password'])
6666
args['auth'] = auth
6767

68-
logger.debug(' GET %s args: %r to: %s\nretry: %s user-agent: %s', url, kw,
68+
logger.debug(' GET %s args: %r to: %s\n retry: %s user-agent: %s', url, kw,
6969
args, retry, ua)
7070

7171
while retry > 0:

0 commit comments

Comments
 (0)