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