sfproxy.py (3477B)
1 #!/usr/bin/env python3 2 # a local cache in front of the public repo on sourceforge, for builds. 3 # 4 # tools/repo/sfproxy.py [PORT] (8091 if you don't say) 5 # PULL=http://127.0.0.1:8091 tools/pkg/build.sh tyrmel 6 # sudo REPO=http://127.0.0.1:8091 EDITION=xfce ./build.sh 7 # 8 # sourceforge sends every file to a random mirror now, with a token in the link, so there's 9 # no fast mirror to pin, and some of them crawl or stall and apk waits on them forever. 10 # this fetches each file once with curl, gives up on a mirror that's slower than 50 KB/s for 11 # 15 seconds and asks again (which gets a different one), and keeps what it got in 12 # ~/.cache/chamesle-sf. apk checks every package's signature itself, so the cache can't 13 # hand it anything it wouldn't take from sourceforge. 14 import os, subprocess, sys, threading, time 15 from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler 16 17 UPSTREAM = os.environ.get('UPSTREAM', 'https://downloads.sourceforge.net/project/chameslerepos') 18 CACHE = os.environ.get('SF_CACHE', os.path.expanduser('~/.cache/chamesle-sf')) 19 PORT = int(sys.argv[1]) if len(sys.argv) > 1 else 8091 20 INDEX_AGE = 600 # the index changes, packages don't: refetch it after ten minutes 21 locks = {} 22 locks_lock = threading.Lock() 23 24 25 def lock_for(path): 26 with locks_lock: 27 return locks.setdefault(path, threading.Lock()) 28 29 30 def fetch(rel, dest): 31 tmp = dest + '.part' 32 for attempt in range(8): 33 r = subprocess.run(['curl', '-fsSL', '--connect-timeout', '15', '--speed-limit', '51200', 34 '--speed-time', '15', '-o', tmp, UPSTREAM + '/' + rel]) 35 if r.returncode == 0: 36 os.replace(tmp, dest) 37 return True 38 # 22 is a 404 or similar: asking again won't help 39 if r.returncode == 22: 40 break 41 print('sfproxy: %s: try %d failed (curl %d), again' % (rel, attempt + 1, r.returncode), flush=True) 42 time.sleep(min(2 * (attempt + 1), 10)) 43 if os.path.exists(tmp): 44 os.remove(tmp) 45 return False 46 47 48 class Handler(BaseHTTPRequestHandler): 49 def do_GET(self): 50 self.serve(True) 51 52 def do_HEAD(self): 53 self.serve(False) 54 55 def serve(self, body): 56 rel = self.path.split('?', 1)[0].lstrip('/') 57 if not rel or '..' in rel.split('/'): 58 self.send_error(404) 59 return 60 dest = os.path.join(CACHE, rel) 61 os.makedirs(os.path.dirname(dest), exist_ok=True) 62 with lock_for(dest): 63 fresh = os.path.exists(dest) and (not rel.endswith('.adb') or time.time() - os.path.getmtime(dest) < INDEX_AGE) 64 if not fresh and not fetch(rel, dest) and not os.path.exists(dest): 65 self.send_error(404) 66 return 67 size = os.path.getsize(dest) 68 self.send_response(200) 69 self.send_header('Content-Length', str(size)) 70 self.send_header('Content-Type', 'application/octet-stream') 71 self.end_headers() 72 if body: 73 with open(dest, 'rb') as f: 74 while True: 75 chunk = f.read(1 << 20) 76 if not chunk: 77 break 78 self.wfile.write(chunk) 79 80 def log_message(self, fmt, *args): 81 pass 82 83 84 if __name__ == '__main__': 85 os.makedirs(CACHE, exist_ok=True) 86 print('sfproxy: http://127.0.0.1:%d -> %s, cache in %s' % (PORT, UPSTREAM, CACHE), flush=True) 87 ThreadingHTTPServer(('127.0.0.1', PORT), Handler).serve_forever()