-
Notifications
You must be signed in to change notification settings - Fork 3
/
apt-mirror-web
executable file
·156 lines (130 loc) · 5.08 KB
/
apt-mirror-web
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/python
import errno
import fcntl
import hashlib
import mimetypes
import os
import Queue
import select
import subprocess
import tempfile
import cherrypy
import pycurl
DIR = os.path.abspath(os.path.dirname(__file__))
DISTS_DIR = os.path.join(DIR, "dists")
CACHE_DIR = os.path.join(DIR, "cache")
def cat_blob(obj):
try:
subprocess.check_call(['git', 'cat-file', '-e', obj],
cwd=DISTS_DIR)
return subprocess.Popen(['git', 'cat-file',
'blob',
obj],
cwd=DISTS_DIR,
stdout=subprocess.PIPE)
except subprocess.CalledProcessError:
raise cherrypy.HTTPError(404)
class Controller(object):
def dists(self, timestamp, path):
cherrypy.response.headers['Content-Type'] = 'text/plain'
if path.endswith('.gz'):
cherrypy.response.headers['Content-Type'] = 'application/x-gzip'
elif path.endswith('.bz2'):
cherrypy.response.headers['Content-Type'] = 'application/x-bzip2'
p = subprocess.Popen(['git', 'rev-list',
'--min-age=%s' % timestamp.encode('utf-8'),
'-1',
'HEAD'],
stdout=subprocess.PIPE,
cwd=DISTS_DIR)
commit, _ = p.communicate()
commit = commit.strip()
if not commit:
raise cherrypy.HTTPError(404)
try:
return cat_blob('%s:%s' % (commit, path)).stdout
except cherrypy.HTTPError:
if path.endswith('.gz'):
p = cat_blob('%s:%s' % (commit, path[:-len('.gz')]))
return subprocess.Popen(['gzip', '-cn9'],
stdin=p.stdout,
stdout=subprocess.PIPE).stdout
if path.endswith('.bz2'):
p = cat_blob('%s:%s' % (commit, path[:-len('.bz2')]))
return subprocess.Popen(['bzip2', '-c9'],
stdin=p.stdout,
stdout=subprocess.PIPE).stdout
raise cherrypy.HTTPError(404)
def pool(self, filename, **kwargs):
t = mimetypes.guess_type(filename)
cherrypy.response.headers['Content-Type'] = t[0] or 'text/plain'
h = hashlib.sha1(filename).hexdigest()
cache_path = os.path.join(CACHE_DIR, h[0], h[1], h[2:])
try:
os.makedirs(os.path.dirname(cache_path))
except OSError, e:
if e.errno != errno.EEXIST:
raise
if os.path.exists(cache_path):
for l in open(cache_path, 'rb'):
yield l
else:
with tempfile.NamedTemporaryFile(dir=os.path.dirname(cache_path),
prefix=(os.path.basename(cache_path) + '.'),
delete=False) as f:
url = 'https://launchpad.net/ubuntu/+archive/primary/+files/' + filename.encode('utf-8')
q = Queue.Queue()
m = pycurl.CurlMulti()
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.FAILONERROR, 1)
def write(data):
q.put(data)
f.write(data)
c.setopt(pycurl.WRITEFUNCTION, write)
m.add_handle(c)
while True:
while True:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
while not q.empty():
yield q.get()
if not num_handles:
break
select.select(*m.fdset())
if c.getinfo(pycurl.HTTP_CODE) == 404:
os.unlink(f.name)
raise cherrypy.HTTPError(404)
else:
f.flush()
os.fsync(f.fileno())
f.close()
os.rename(f.name, cache_path)
def catchall(self, **kwargs):
return ("Hello. It looks like you're trying to browse the Ubuntu apt "
"mirror. Don't do that")
d = cherrypy.dispatch.RoutesDispatcher()
c = Controller()
d.connect('dists',
'/{timestamp}/dists/{path:.*?}',
c,
action='dists')
d.connect('pool',
'/{timestamp}/pool/{component}/{prefix:(lib)?[a-z]}/{spackage}/{filename}',
c,
action='pool')
d.connect('catchall',
'/{path:.*?}',
c,
action='catchall')
conf = {
'/': {'request.dispatch': d},
}
if __name__ == '__main__':
cherrypy.config.update({'server.socket_host': '0.0.0.0',
'server.socket_port': 9999,
'log.screen': True})
app = cherrypy.tree.mount(None, config=conf)
cherrypy.server.start()