1 # Copyright 2014 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.
5 from operator
import itemgetter
8 from data_source
import DataSource
9 from docs_server_utils
import MarkLast
10 from extensions_paths
import BROWSER_API_PATHS
, BROWSER_CHROME_EXTENSIONS
11 from future
import All
12 from path_util
import Join
, Split
15 _COMMENT_START_MARKER
= '#'
16 _CORE_OWNERS
= 'Core Extensions/Apps Owners'
21 def ParseOwnersFile(content
, randomize
):
22 '''Returns a tuple (owners, notes), where
23 |owners| is a list of dicts formed from the owners in |content|,
24 |notes| is a string formed from the comments in |content|.
27 return [], 'Use one of the ' + _CORE_OWNERS
+ '.'
30 for line
in content
.splitlines():
33 if line
.startswith(_COMMENT_START_MARKER
):
34 notes
.append(line
[len(_COMMENT_START_MARKER
):].lstrip())
36 # TODO(ahernandez): Mark owners no longer on the project.
37 owners
.append({'email': line
, 'username': line
[:line
.find('@')]})
38 # Randomize the list so owners toward the front of the list aren't
39 # diproportionately inundated with reviews.
41 random
.shuffle(owners
)
43 return owners
, '\n'.join(notes
)
46 class OwnersDataSource(DataSource
):
47 def __init__(self
, server_instance
, _
, randomize
=True):
48 self
._host
_fs
= server_instance
.host_file_system_provider
.GetMaster()
49 self
._cache
= server_instance
.object_store_creator
.Create(OwnersDataSource
)
50 self
._owners
_fs
= server_instance
.compiled_fs_factory
.Create(
51 self
._host
_fs
, self
._CreateAPIEntry
, OwnersDataSource
)
52 self
._randomize
= randomize
54 def _CreateAPIEntry(self
, path
, content
):
55 '''Creates a dict with owners information for an API, specified
58 owners
, notes
= ParseOwnersFile(content
, self
._randomize
)
59 api_name
= Split(path
)[-2][:-1]
67 def _CollectOwnersData(self
):
68 '''Walks through the file system, collecting owners data from
71 def collect(api_owners
):
72 if api_owners
is not None:
75 # Get API owners from every OWNERS file that exists.
77 for root
in BROWSER_API_PATHS
:
78 for base
, dirs
, _
in self
._host
_fs
.Walk(root
, depth
=1):
80 owners_file
= Join(root
, base
, dir_
, _OWNERS
)
82 self
._owners
_fs
.GetFromFile(owners_file
, skip_not_found
=True))
84 # Add an entry for the core extensions/apps owners.
85 def fix_core_owners(entry
):
86 entry
['apiName'] = _CORE_OWNERS
90 owners_file
= Join(BROWSER_CHROME_EXTENSIONS
, _OWNERS
)
91 api_owners
.append(self
._owners
_fs
.GetFromFile(owners_file
).Then(
93 def sort_and_cache(api_owners
):
94 api_owners
.sort(key
=itemgetter('apiName'))
95 self
._cache
.Set('api_owners', api_owners
)
97 return All(api_owners
).Then(sort_and_cache
)
98 return self
._cache
.Get('api_owners').Then(collect
)
102 'apis': self
._CollectOwnersData
()
106 return self
._CollectOwnersData
()