Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / findit / chromium_deps.py
1 # Copyright (c) 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.
4
5 import base64
6 import https
7 import utils
8
9
10 DEPS_FILE_URL_SVN = 'https://src.chromium.org/chrome/trunk/src/DEPS?p=%s'
11 DEPS_FILE_URL_GIT = (
12     'https://chromium.googlesource.com/chromium/src/+/%s/DEPS?format=text')
13
14
15 class _VarImpl(object):
16
17   def __init__(self, local_scope):
18     self._local_scope = local_scope
19
20   def Lookup(self, var_name):
21     if var_name in self._local_scope.get('vars', {}):
22       return self._local_scope['vars'][var_name]
23     raise Exception('Var is not defined: %s' % var_name)
24
25
26 def _ParseDEPS(content):
27   """Parse the DEPS file of chromium."""
28   local_scope = {}
29   var = _VarImpl(local_scope)
30   global_scope = {
31       'Var': var.Lookup,
32       'deps': {},
33       'deps_os': {},
34       'include_rules': [],
35       'skip_child_includes': [],
36       'hooks': [],
37   }
38   exec(content, global_scope, local_scope)
39
40   local_scope.setdefault('deps', {})
41   local_scope.setdefault('deps_os', {})
42
43   return (local_scope['deps'], local_scope['deps_os'])
44
45
46 def _GetComponentName(path):
47   """Return the component name of a path."""
48   host_dirs = [
49       'src/chrome/browser/resources/',
50       'src/chrome/test/data/layout_tests/',
51       'src/media/',
52       'src/sdch/',
53       'src/testing/',
54       'src/third_party/WebKit/',
55       'src/third_party/',
56       'src/tools/',
57       'src/',
58   ]
59   components_renamed = {
60       'webkit': 'blink',
61   }
62
63   for host_dir in host_dirs:
64     if path.startswith(host_dir):
65       path = path[len(host_dir):]
66       name = path.split('/')[0].lower()
67       if name in components_renamed:
68         return components_renamed[name].lower()
69       else:
70         return name.lower()
71
72   # Unknown path, return the whole path as component name.
73   return '_'.join(path.split('/'))
74
75
76 def _GetContentOfDEPS(chromium_revision):
77   if utils.IsGitHash(chromium_revision):
78     url = DEPS_FILE_URL_GIT
79   else:
80     url = DEPS_FILE_URL_SVN
81   return https.SendRequest(url % chromium_revision)
82
83
84 def GetChromiumComponents(chromium_revision,
85                           os_platform='unix',
86                           deps_file_downloader=_GetContentOfDEPS):
87   """Return a list of components used by Chrome of the given revision.
88
89   Args:
90     chromium_revision: The revision of the Chrome build.
91     os_platform: The target platform of the Chrome build, eg. win, mac, etc.
92     deps_file_downloader: A function that takes the chromium_revision as input,
93                           and returns the content of the DEPS file. The returned
94                           content is assumed to be trusted input and will be
95                           evaluated as python code.
96
97   Returns:
98     A map from component path to parsed component name, repository URL,
99     repository type and revision.
100   """
101   if os_platform.lower() == 'linux':
102     os_platform = 'unix'
103
104   is_git_hash = utils.IsGitHash(chromium_revision)
105
106   # Download the content of DEPS file in chromium.
107   deps_content = deps_file_downloader(chromium_revision)
108
109   # Googlesource git returns text file encoded in base64, so decode it.
110   if is_git_hash:
111     deps_content = base64.b64decode(deps_content)
112
113   all_deps = {}
114
115   # Parse the content of DEPS file.
116   deps, deps_os = _ParseDEPS(deps_content)
117   all_deps.update(deps)
118   if os_platform is not None:
119     all_deps.update(deps_os.get(os_platform, {}))
120
121   # Figure out components based on the dependencies.
122   components = {}
123   for component_path in all_deps:
124     name = _GetComponentName(component_path)
125     repository, revision = all_deps[component_path].split('@')
126     is_git_hash = utils.IsGitHash(revision)
127     if repository.startswith('/'):
128       # TODO(stgao): Use git repo after chromium moves to git.
129       repository = 'https://src.chromium.org/chrome%s' % repository
130     if is_git_hash:
131       repository_type = 'git'
132     else:
133       repository_type = 'svn'
134     if not component_path.endswith('/'):
135       component_path += '/'
136     components[component_path] = {
137         'path': component_path,
138         'name': name,
139         'repository': repository,
140         'repository_type': repository_type,
141         'revision': revision
142     }
143
144   # Add chromium as a component, depending on the repository type.
145   if is_git_hash:
146     repository = 'https://chromium.googlesource.com/chromium/src/'
147     repository_type = 'git'
148   else:
149     repository = 'https://src.chromium.org/chrome/trunk'
150     repository_type = 'svn'
151
152   components['src/'] = {
153       'path': 'src/',
154       'name': 'chromium',
155       'repository': repository,
156       'repository_type': repository_type,
157       'revision': chromium_revision
158   }
159
160   return components
161
162
163 def GetChromiumComponentRange(old_revision,
164                               new_revision,
165                               os_platform='unix',
166                               deps_file_downloader=_GetContentOfDEPS):
167   """Return a list of components with their revision ranges.
168
169   Args:
170     old_revision: The old revision of a Chrome build.
171     new_revision: The new revision of a Chrome build.
172     os_platform: The target platform of the Chrome build, eg. win, mac, etc.
173     deps_file_downloader: A function that takes the chromium_revision as input,
174                           and returns the content of the DEPS file. The returned
175                           content is assumed to be trusted input and will be
176                           evaluated as python code.
177
178   Returns:
179     A map from component path to its parsed regression and other information.
180   """
181   # Assume first revision is the old revision.
182   old_components = GetChromiumComponents(old_revision, os_platform,
183                                          deps_file_downloader)
184   new_components = GetChromiumComponents(new_revision, os_platform,
185                                          deps_file_downloader)
186
187   components = {}
188   for path in new_components:
189     new_component = new_components[path]
190     old_revision = None
191     if path in old_components:
192       old_component = old_components[path]
193       old_revision = old_component['revision']
194     components[path] = {
195         'path': path,
196         'rolled': new_component['revision'] != old_revision,
197         'name': new_component['name'],
198         'old_revision': old_revision,
199         'new_revision': new_component['revision'],
200         'repository': new_component['repository'],
201         'repository_type': new_component['repository_type']
202     }
203
204   return components
205
206
207 if __name__ == '__main__':
208   import json
209   print json.dumps(GetChromiumComponents(
210       'b4b1aea80b25a3b2f7952c9d95585e880421ef2b'), sort_keys=True, indent=2)