Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / tools / cygprofile / cygprofile_utils.py
blobc1c1d8790546f3d7a877eac813bd791c876c5256
1 #!/usr/bin/python
2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Common utilites used by cygprofile scripts.
7 """
9 import logging
10 import os
11 import re
13 class WarningCollector(object):
14 """Collects warnings, but limits the number printed to a set value."""
15 def __init__(self, max_warnings):
16 self._warnings = 0
17 self._max_warnings = max_warnings
19 def Write(self, message):
20 """Print a warning if fewer than max_warnings have already been printed."""
21 if self._warnings < self._max_warnings:
22 logging.warning(message)
23 self._warnings += 1
25 def WriteEnd(self, message):
26 """Once all warnings have been printed, use this to print the number of
27 elided warnings."""
28 if self._warnings > self._max_warnings:
29 logging.warning('%d more warnings for: %s' % (
30 self._warnings - self._max_warnings, message))
33 def DetectArchitecture(default='arm'):
34 """Detects the architecture by looking for target_arch in GYP_DEFINES.
35 If not not found, returns default.
36 """
37 gyp_defines = os.environ.get('GYP_DEFINES', '')
38 match = re.match('target_arch=(\S+)', gyp_defines)
39 if match and len(match.groups()) == 1:
40 return match.group(1)
41 else:
42 return default
45 def InvertMapping(x_to_ys):
46 """Given a map x -> [y1, y2...] returns inverse mapping y->[x1, x2...]."""
47 y_to_xs = {}
48 for x, ys in x_to_ys.items():
49 for y in ys:
50 y_to_xs.setdefault(y, []).append(x)
51 return y_to_xs
54 def GetObjDir(libchrome):
55 """Get the path to the obj directory corresponding to the given libchrome.
57 Assumes libchrome is in for example .../Release/lib/libchrome.so and object
58 files are in .../Release/obj.
59 """
60 # TODO(azarchs): Pass obj path in explicitly where needed rather than relying
61 # on the above assumption.
62 return os.path.abspath(os.path.join(
63 os.path.dirname(libchrome), '../obj'))