1# Copyright (C) 2019 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15from google.appengine.api import memcache 16from google.appengine.api import urlfetch 17import webapp2 18 19import base64 20 21BASE = 'https://android.googlesource.com/platform/external/perfetto.git/' \ 22 '+/master/%s?format=TEXT' 23 24RESOURCES = { 25 'tracebox': 'tools/tracebox', 26 'traceconv': 'tools/traceconv', 27 'trace_processor': 'tools/trace_processor', 28} 29 30 31class RedirectHandler(webapp2.RequestHandler): 32 33 def get(self): 34 self.error(301) 35 self.response.headers['Location'] = 'https://www.perfetto.dev/' 36 37 38class GitilesMirrorHandler(webapp2.RequestHandler): 39 40 def get(self, resource): 41 self.response.headers['Content-Type'] = 'text/plain' 42 resource = resource.lower() 43 if resource not in RESOURCES: 44 self.error(404) 45 self.response.out.write('Resource "%s" not found' % resource) 46 return 47 48 url = BASE % RESOURCES[resource] 49 contents = memcache.get(url) 50 if not contents or self.request.get('reload'): 51 result = urlfetch.fetch(url) 52 if result.status_code != 200: 53 memcache.delete(url) 54 self.response.set_status(result.status_code) 55 self.response.write( 56 'http error %d while fetching %s' % (result.status_code, url)) 57 return 58 contents = base64.b64decode(result.content) 59 memcache.set(url, contents, time=3600) # 1h 60 self.response.headers['Content-Disposition'] = \ 61 'attachment; filename="%s"' % resource 62 self.response.write(contents) 63 64 65app = webapp2.WSGIApplication([ 66 ('/', RedirectHandler), 67 ('/([a-zA-Z0-9_.-]+)', GitilesMirrorHandler), 68], 69 debug=True) 70