OLD | NEW |
---|---|
(Empty) | |
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 | |
3 # found in the LICENSE file. | |
4 | |
5 import logging | |
6 import re | |
7 import json | |
8 | |
9 from memcache import MemcacheAdd, MemcacheGet | |
10 from fetch import Fetch | |
11 | |
12 class BranchUtility(object): | |
13 """Utility class for dealing with different doc branches. | |
14 """ | |
15 def __init__(self): | |
Aaron Boodman
2012/05/22 22:45:02
You could simplify this somewhat by passing the ur
cduvall
2012/05/24 00:15:40
Done.
| |
16 self.url = 'http://omahaproxy.appspot.com/json' | |
17 | |
18 def SetURL(self, url): | |
19 self.url = url | |
20 | |
21 def _FetchURL(self, url): | |
22 return Fetch(url) | |
23 | |
24 def GetChannelNameFromURL(self, url): | |
Aaron Boodman
2012/05/22 22:45:02
I guess it's really GetChannelNameFromPath(), righ
cduvall
2012/05/24 00:15:40
Done.
| |
25 if url.startswith('dev'): | |
Aaron Boodman
2012/05/22 22:45:02
startswith('dev/') ... same for the other branches
Aaron Boodman
2012/05/22 22:45:02
first_part = path.split('/')[0]
if first_part in [
cduvall
2012/05/24 00:15:40
Done.
| |
26 return 'dev' | |
27 if url.startswith('beta'): | |
28 return 'beta' | |
29 if url.startswith('trunk'): | |
30 return 'trunk' | |
31 return 'stable' | |
32 | |
33 def GetBranchNumberForURL(self, url): | |
34 """Returns an empty string if the branch number cannot be found. | |
35 """ | |
36 channel_name = self.GetChannelNameFromURL(url) | |
37 if channel_name == 'trunk': | |
38 return 'trunk' | |
Aaron Boodman
2012/05/22 22:45:02
Still seems weird to accept trunk as an argument,
cduvall
2012/05/24 00:15:40
I don't really like accepting trunk paths as an ar
| |
39 | |
40 result = MemcacheGet(url, 'urls') | |
Aaron Boodman
2012/05/22 22:45:02
With my change above, you wouldn't need this memca
cduvall
2012/05/24 00:15:40
Done.
| |
41 if result is not None: | |
42 return result | |
43 logging.info('Branch number cache miss: ' + url) | |
44 fetch_data = self._FetchURL(self.url) | |
45 | |
46 version_json = json.loads(fetch_data) | |
47 branch_numbers = {} | |
48 for entry in version_json: | |
49 if entry['os'] == 'cf': | |
Aaron Boodman
2012/05/22 22:45:02
On second thought, let's be more robust and have a
cduvall
2012/05/24 00:15:40
Done.
| |
50 continue | |
51 for version in entry['versions']: | |
52 if version['channel'] != channel_name: | |
53 continue | |
54 if version['true_branch'] not in branch_numbers: | |
55 branch_numbers[version['true_branch']] = 0 | |
56 else: | |
57 branch_numbers[version['true_branch']] += 1 | |
58 | |
59 sorted_list = [x for x in branch_numbers.iteritems()] | |
60 sorted_list.sort(key = lambda x: x[1]) | |
61 sorted_list.reverse() | |
62 | |
63 branch_number = '' | |
64 if len(sorted_list) > 0: | |
65 branch_number, _ = sorted_list[0] | |
66 | |
67 if not MemcacheAdd(url, branch_number, 'urls'): | |
68 logging.error('Memcache set failed.') | |
69 | |
70 return branch_number | |
OLD | NEW |