path_canonicalizer.py revision 4e180b6a0b4720a9b8e9e959a882386f690f08ff
1# Copyright 2013 The Chromium Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5import logging 6import os 7import posixpath 8import traceback 9 10from branch_utility import BranchUtility 11from file_system import FileNotFoundError 12from third_party.json_schema_compiler.model import UnixName 13import svn_constants 14 15def _SimplifyFileName(file_name): 16 return (posixpath.splitext(file_name)[0] 17 .lower() 18 .replace('.', '') 19 .replace('-', '') 20 .replace('_', '')) 21 22class PathCanonicalizer(object): 23 '''Transforms paths into their canonical forms. Since the dev server has had 24 many incarnations - e.g. there didn't use to be apps/ - there may be old 25 paths lying around the webs. We try to redirect those to where they are now. 26 ''' 27 def __init__(self, compiled_fs_factory, file_system): 28 # Map of simplified API names (for typo detection) to their real paths. 29 def make_public_apis(_, file_names): 30 return dict((_SimplifyFileName(name), name) for name in file_names) 31 self._public_apis = compiled_fs_factory.Create(file_system, 32 make_public_apis, 33 PathCanonicalizer) 34 35 def Canonicalize(self, path): 36 '''Returns the canonical path for |path|, and whether that path is a 37 permanent canonicalisation (e.g. when we redirect from a channel to a 38 channel-less URL) or temporary (e.g. when we redirect from an apps-only API 39 to an extensions one - we may at some point enable it for extensions). 40 ''' 41 class ReturnType(object): 42 def __init__(self, path, permanent): 43 self.path = path 44 self.permanent = permanent 45 46 # Catch incorrect comparisons by disabling ==/!=. 47 def __eq__(self, _): raise NotImplementedError() 48 def __ne__(self, _): raise NotImplementedError() 49 50 # Strip any channel info off it. There are no channels anymore. 51 for channel_name in BranchUtility.GetAllChannelNames(): 52 channel_prefix = channel_name + '/' 53 if path.startswith(channel_prefix): 54 # Redirect now so that we can set the permanent-redirect bit. Channel 55 # redirects are the only things that should be permanent redirects; 56 # anything else *could* change, so is temporary. 57 return ReturnType(path[len(channel_prefix):], True) 58 59 # No further work needed for static. 60 if path.startswith('static/'): 61 return ReturnType(path, False) 62 63 # People go to just "extensions" or "apps". Redirect to the directory. 64 if path in ('extensions', 'apps'): 65 return ReturnType(path + '/', False) 66 67 # The rest of this function deals with trying to figure out what API page 68 # for extensions/apps to redirect to, if any. We see a few different cases 69 # here: 70 # - Unqualified names ("browserAction.html"). These are easy to resolve; 71 # figure out whether it's an extension or app API and redirect. 72 # - but what if it's both? Well, assume extensions. Maybe later we can 73 # check analytics and see which is more popular. 74 # - Wrong names ("apps/browserAction.html"). This really does happen, 75 # damn it, so do the above logic but record which is the default. 76 if path.startswith(('extensions/', 'apps/')): 77 default_platform, reference_path = path.split('/', 1) 78 else: 79 default_platform, reference_path = ('extensions', path) 80 81 try: 82 apps_public = self._public_apis.GetFromFileListing( 83 '/'.join((svn_constants.PUBLIC_TEMPLATE_PATH, 'apps'))).Get() 84 extensions_public = self._public_apis.GetFromFileListing( 85 '/'.join((svn_constants.PUBLIC_TEMPLATE_PATH, 'extensions'))).Get() 86 except FileNotFoundError: 87 # Probably offline. 88 logging.warning(traceback.format_exc()) 89 return ReturnType(path, False) 90 91 simple_reference_path = _SimplifyFileName(reference_path) 92 apps_path = apps_public.get(simple_reference_path) 93 extensions_path = extensions_public.get(simple_reference_path) 94 95 if apps_path is None: 96 if extensions_path is None: 97 # No idea. Just return the original path. It'll probably 404. 98 pass 99 else: 100 path = 'extensions/%s' % extensions_path 101 else: 102 if extensions_path is None: 103 path = 'apps/%s' % apps_path 104 else: 105 assert apps_path == extensions_path 106 path = '%s/%s' % (default_platform, apps_path) 107 108 return ReturnType(path, False) 109