-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple-buck-cache-server.py
117 lines (87 loc) · 3.28 KB
/
simple-buck-cache-server.py
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
# -*- coding: utf-8 -*-
import sys
import os
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse
cacheDirAbsPath = None
class NaiveBuckCacheServerRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
print("#GET " + self.path)
if not self.path.startswith('/artifacts/key'):
self.send_response(404)
self.end_headers()
return
parseResult = urlparse(self.path)
key = os.path.basename(parseResult.path)
cacheFileName = '%s.buckcache' % (key)
cacheFileSearchPath = os.path.join(cacheDirAbsPath, cacheFileName)
if not os.path.exists(cacheFileSearchPath):
self.send_response(404)
self.end_headers()
return
print('Cache found for key %s' % (key))
f = open(cacheFileSearchPath, 'rb')
cachedBytes = f.read()
f.close()
self.send_response(200)
self.send_header('Content-type', 'application/octet-stream')
self.end_headers()
self.wfile.write(cachedBytes)
def do_PUT(self):
print("#PUT " + self.path)
if not self.path.startswith('/artifacts/key'):
self.send_response(406)
self.end_headers()
return
self.send_response(202)
self.end_headers()
contentLength = int(self.headers['Content-Length'])
read = 0
# key count
keyCountBytes = self.rfile.read(4)
read += 4
keyCount = int.from_bytes(keyCountBytes, byteorder='big', signed=True)
#print(keyCount)
# read keys
keys = []
for i in range(0, keyCount):
keyLenBytes = self.rfile.read(2)
read += 2
keyLen = int.from_bytes(keyLenBytes, byteorder='big', signed=False)
keyBytes = self.rfile.read(keyLen)
key = keyBytes.decode()
#print('key = ' + key)
keys.append(key)
# data
cacheData = self.rfile.read(contentLength - read)
# save those data
# TODO: optimize the use of cache
# NOTE: key --> intermediate unique key --> data
for key in keys:
cacheFileName = '%s.buckcache' % (key)
cacheFilePath = os.path.join(cacheDirAbsPath, cacheFileName)
if os.path.exists(cacheFilePath):
print('Cache file %s is already there' % (cacheFileName))
f = open(cacheFilePath, 'wb')
f.write(cacheData)
f.close()
print('Cache saved for key %s' % (key))
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: %s <port> <cache-dir>" % (sys.argv[0]))
sys.exit()
port = int(sys.argv[1])
cacheDir = sys.argv[2]
absPath = os.path.abspath(cacheDir)
if not os.path.exists(absPath):
print('Cache dir %s doesn\'t exist, creating...' % (absPath))
try:
os.makedirs(absPath)
except:
print('Failed to create cache dir with path: %s' % (absPath))
sys.exit()
cacheDirAbsPath = absPath
print('Start naive buck cache server on port %d' % (port))
print('Cache dir: %s' % (cacheDirAbsPath))
server = HTTPServer(("0.0.0.0", port), NaiveBuckCacheServerRequestHandler)
server.serve_forever()