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

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

Issue 12996003: Dynamically generate a heading for Extension Docs API pages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Minor changes Created 7 years, 7 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
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 import operator 7 import operator
8 8
9 from appengine_wrappers import GetAppVersion 9 from appengine_wrappers import GetAppVersion
10 from object_store_creator import ObjectStoreCreator 10 from object_store_creator import ObjectStoreCreator
11 11
12 class BranchUtility(object): 12 class BranchUtility(object):
13 def __init__(self, fetch_url, fetcher, object_store=None): 13 def __init__(self, fetch_url, fetcher, object_store=None):
14 self._fetch_url = fetch_url 14 self._fetch_url = fetch_url
15 self._fetcher = fetcher 15 self._fetcher = fetcher
16 if object_store is None: 16 if object_store is None:
17 object_store = (ObjectStoreCreator.SharedFactory(GetAppVersion()) 17 object_store = (ObjectStoreCreator.SharedFactory(GetAppVersion())
18 .Create(BranchUtility).Create()) 18 .Create(BranchUtility).Create())
19 self._object_store = object_store 19 self._object_store = object_store
20 20
21 @staticmethod 21 @staticmethod
22 def GetAllBranchNames(): 22 def GetAllBranchNames():
23 return ['stable', 'beta', 'dev', 'trunk'] 23 return ['stable', 'beta', 'dev', 'trunk']
24 24
25 def GetAllBranchNumbers(self): 25 def GetAllBranchNumbers(self):
26 return [(branch, self.GetBranchNumberForChannelName(branch)) 26 return [(branch, self.GetChannelInfoForChannelName(branch).branch)
27 for branch in BranchUtility.GetAllBranchNames()] 27 for branch in BranchUtility.GetAllBranchNames()]
28 28
29 def GetAllVersionNumbers(self):
30 return [self.GetChannelInfoForChannelName(branch).version
31 for branch in self.GetAllBranchNames()]
32
33 def GetChannelInfoForAllChannels(self):
34 return [self.GetChannelInfoForChannelName(branch)
35 for branch in self.GetAllBranchNames()]
36
29 @staticmethod 37 @staticmethod
30 def SplitChannelNameFromPath(path): 38 def SplitChannelNameFromPath(path):
31 """Splits the channel name out of |path|, returning the tuple 39 """Splits the channel name out of |path|, returning the tuple
32 (channel_name, real_path). If the channel cannot be determined then returns 40 (channel_name, real_path). If the channel cannot be determined then returns
33 (None, path). 41 (None, path).
34 """ 42 """
35 if '/' in path: 43 if '/' in path:
36 first, second = path.split('/', 1) 44 first, second = path.split('/', 1)
37 else: 45 else:
38 first, second = (path, '') 46 first, second = (path, '')
39 if first in ['trunk', 'dev', 'beta', 'stable']: 47 if first in ['trunk', 'dev', 'beta', 'stable']:
40 return (first, second) 48 return (first, second)
41 return (None, path) 49 return (None, path)
42 50
43 def GetBranchNumberForChannelName(self, channel_name): 51 def GetChannelInfoForChannelName(self, channel):
not at google - send to devlin 2013/04/30 18:34:19 Ok I think that the "ForChannelName" is tautologic
epeterson 2013/05/13 02:38:10 Done.
44 """Returns the branch number for a channel name. 52 class ChannelInfo(object):
53 def __init__(self, name, branch, version):
54 self.name = name
55 self.branch = branch
56 self.version = version
57
58 return ChannelInfo(channel,
59 self._ExtractFromVersionJson(channel, 'branch'),
60 self._ExtractFromVersionJson(channel, 'version'))
61
62 def _ExtractFromVersionJson(self, channel_name, data_type):
63 """Returns the branch or version number for a channel name.
45 """ 64 """
46 if channel_name == 'trunk': 65 if channel_name == 'trunk':
47 return 'trunk' 66 return 'trunk'
48 67
49 branch_number = self._object_store.Get(channel_name).Get() 68 data = self._object_store.Get('%s.%s' % (channel_name, data_type)).Get()
50 if branch_number is not None: 69 if data is not None:
51 return branch_number 70 return data
52 71
53 try: 72 try:
54 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content) 73 version_json = json.loads(self._fetcher.Fetch(self._fetch_url).content)
55 except Exception as e: 74 except Exception as e:
56 # This can happen if omahaproxy is misbehaving, which we've seen before. 75 # This can happen if omahaproxy is misbehaving, which we've seen before.
57 # Quick hack fix: just serve from trunk until it's fixed. 76 # Quick hack fix: just serve from trunk until it's fixed.
58 logging.error('Failed to fetch or parse branch from omahaproxy: %s! ' 77 logging.error('Failed to fetch or parse branch from omahaproxy: %s! '
59 'Falling back to "trunk".' % e) 78 'Falling back to "trunk".' % e)
60 return 'trunk' 79 return 'trunk'
61 80
62 branch_numbers = {} 81 numbers = {}
63 for entry in version_json: 82 for entry in version_json:
64 if entry['os'] not in ['win', 'linux', 'mac', 'cros']: 83 if entry['os'] not in ['win', 'linux', 'mac', 'cros']:
65 continue 84 continue
66 for version in entry['versions']: 85 for version in entry['versions']:
67 if version['channel'] != channel_name: 86 if version['channel'] != channel_name:
68 continue 87 continue
69 branch = version['version'].split('.')[2] 88 if data_type == 'branch':
70 if branch not in branch_numbers: 89 number = version['version'].split('.')[2]
71 branch_numbers[branch] = 0 90 elif data_type == 'version':
91 number = version['version'].split('.')[0]
92 if number not in numbers:
93 numbers[number] = 0
72 else: 94 else:
73 branch_numbers[branch] += 1 95 numbers[number] += 1
74 96
75 sorted_branches = sorted(branch_numbers.iteritems(), 97 sorted_numbers = sorted(numbers.iteritems(),
76 None, 98 None,
77 operator.itemgetter(1), 99 operator.itemgetter(1),
78 True) 100 True)
79 self._object_store.Set(channel_name, sorted_branches[0][0]) 101 self._object_store.Set('%s.%s' % (channel_name, data_type),
102 sorted_numbers[0][0])
80 103
81 return sorted_branches[0][0] 104 return sorted_numbers[0][0]
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698