Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(341)

Side by Side Diff: chrome/common/extensions/docs/server2/branch_utility.py

Issue 13470005: Refactor the devserver to make it easier to control caching (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cduvall, rebase Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 import json 5 import json
6 import logging 6 import logging
7
8 import object_store
9 import operator 7 import operator
10 8
9 from object_store_creator import ObjectStoreCreator
10
11 class BranchUtility(object): 11 class BranchUtility(object):
12 def __init__(self, base_path, fetcher, object_store): 12 def __init__(self, fetch_url, fetcher, object_store=None):
13 self._base_path = base_path 13 self._fetch_url = fetch_url
14 self._fetcher = fetcher 14 self._fetcher = fetcher
15 if object_store is None:
16 object_store = ObjectStoreCreator(BranchUtility).Create()
15 self._object_store = object_store 17 self._object_store = object_store
16 18
17 @staticmethod 19 @staticmethod
18 def GetAllBranchNames(): 20 def GetAllBranchNames():
19 return ['stable', 'beta', 'dev', 'trunk'] 21 return ['stable', 'beta', 'dev', 'trunk']
20 22
21 def GetAllBranchNumbers(self): 23 def GetAllBranchNumbers(self):
22 return [(branch, self.GetBranchNumberForChannelName(branch)) 24 return [(branch, self.GetBranchNumberForChannelName(branch))
23 for branch in BranchUtility.GetAllBranchNames()] 25 for branch in BranchUtility.GetAllBranchNames()]
24 26
25 def SplitChannelNameFromPath(self, path): 27 @staticmethod
28 def SplitChannelNameFromPath(path):
26 """Splits the channel name out of |path|, returning the tuple 29 """Splits the channel name out of |path|, returning the tuple
27 (channel_name, real_path). If the channel cannot be determined then returns 30 (channel_name, real_path). If the channel cannot be determined then returns
28 (None, path). 31 (None, path).
29 """ 32 """
30 if '/' in path: 33 if '/' in path:
31 first, second = path.split('/', 1) 34 first, second = path.split('/', 1)
32 else: 35 else:
33 first, second = (path, '') 36 first, second = (path, '')
34 if first in ['trunk', 'dev', 'beta', 'stable']: 37 if first in ['trunk', 'dev', 'beta', 'stable']:
35 return (first, second) 38 return (first, second)
36 return (None, path) 39 return (None, path)
37 40
38 def GetBranchNumberForChannelName(self, channel_name): 41 def GetBranchNumberForChannelName(self, channel_name):
39 """Returns the branch number for a channel name. 42 """Returns the branch number for a channel name.
40 """ 43 """
41 if channel_name == 'trunk': 44 if channel_name == 'trunk':
42 return 'trunk' 45 return 'trunk'
43 46
44 branch_number = self._object_store.Get(channel_name + '.' + self._base_path, 47 branch_number = self._object_store.Get(channel_name).Get()
45 object_store.BRANCH_UTILITY).Get()
46 if branch_number is not None: 48 if branch_number is not None:
47 return branch_number 49 return branch_number
48 50
49 try: 51 try:
50 version_json = json.loads(self._fetcher.Fetch(self._base_path).content) 52 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content)
51 except Exception as e: 53 except Exception as e:
52 # This can happen if omahaproxy is misbehaving, which we've seen before. 54 # This can happen if omahaproxy is misbehaving, which we've seen before.
53 # Quick hack fix: just serve from trunk until it's fixed. 55 # Quick hack fix: just serve from trunk until it's fixed.
54 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' 56 logging.error('Failed to fetch or parse branch from omahaproxy: %s! '
55 'Falling back to "trunk".' % e) 57 'Falling back to "trunk".' % e)
56 return 'trunk' 58 return 'trunk'
57 59
58 branch_numbers = {} 60 branch_numbers = {}
59 for entry in version_json: 61 for entry in version_json:
60 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: 62 if entry['os'] not in ['win', 'linux', 'mac', 'cros']:
61 continue 63 continue
62 for version in entry['versions']: 64 for version in entry['versions']:
63 if version['channel'] != channel_name: 65 if version['channel'] != channel_name:
64 continue 66 continue
65 branch = version['version'].split('.')[2] 67 branch = version['version'].split('.')[2]
66 if branch not in branch_numbers: 68 if branch not in branch_numbers:
67 branch_numbers[branch] = 0 69 branch_numbers[branch] = 0
68 else: 70 else:
69 branch_numbers[branch] += 1 71 branch_numbers[branch] += 1
70 72
71 sorted_branches = sorted(branch_numbers.iteritems(), 73 sorted_branches = sorted(branch_numbers.iteritems(),
72 None, 74 None,
73 operator.itemgetter(1), 75 operator.itemgetter(1),
74 True) 76 True)
75 self._object_store.Set(channel_name + '.' + self._base_path, 77 self._object_store.Set(channel_name, sorted_branches[0][0])
76 sorted_branches[0][0],
77 object_store.BRANCH_UTILITY)
78 78
79 return sorted_branches[0][0] 79 return sorted_branches[0][0]
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698