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 'traceconv': 'tools/traceconv', 26 'trace_processor': 'tools/trace_processor', 27} 28 29 30class RedirectHandler(webapp2.RequestHandler): 31 32 def get(self): 33 self.error(301) 34 self.response.headers['Location'] = 'https://www.perfetto.dev/' 35 36 37class GitilesMirrorHandler(webapp2.RequestHandler): 38 39 def get(self, resource): 40 self.response.headers['Content-Type'] = 'text/plain' 41 resource = resource.lower() 42 if resource not in RESOURCES: 43 self.error(404) 44 self.response.out.write('Resource "%s" not found' % resource) 45 return 46 47 url = BASE % RESOURCES[resource] 48 contents = memcache.get(url) 49 if not contents or self.request.get('reload'): 50 result = urlfetch.fetch(url) 51 if result.status_code != 200: 52 memcache.delete(url) 53 self.response.set_status(result.status_code) 54 self.response.write( 55 'http error %d while fetching %s' % (result.status_code, url)) 56 return 57 contents = base64.b64decode(result.content) 58 memcache.set(url, contents, time=3600) # 1h 59 self.response.headers['Content-Disposition'] = \ 60 'attachment; filename="%s"' % resource 61 self.response.write(contents) 62 63 64app = webapp2.WSGIApplication([ 65 ('/', RedirectHandler), 66 ('/([a-zA-Z0-9_.-]+)', GitilesMirrorHandler), 67], 68 debug=True) 69