Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / tools / cygprofile / cygprofile_utils.py
blob4851df97cf25f3acc5c521873dc1cfa2268083ff
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, level=logging.WARNING):
16 self._warnings = 0
17 self._max_warnings = max_warnings
18 self._level = level
20 def Write(self, message):
21 """Prints a warning if fewer than max_warnings have already been printed."""
22 if self._warnings < self._max_warnings:
23 logging.log(self._level, message)
24 self._warnings += 1
26 def WriteEnd(self, message):
27 """Once all warnings have been printed, use this to print the number of
28 elided warnings."""
29 if self._warnings > self._max_warnings:
30 logging.log(self._level, '%d more warnings for: %s' % (
31 self._warnings - self._max_warnings, message))
34 def DetectArchitecture(default='arm'):
35 """Detects the architecture by looking for target_arch in GYP_DEFINES.
36 If not not found, returns default.
37 """
38 gyp_defines = os.environ.get('GYP_DEFINES', '')
39 match = re.match('target_arch=(\S+)', gyp_defines)
40 if match and len(match.groups()) == 1:
41 return match.group(1)
42 else:
43 return default
46 def InvertMapping(x_to_ys):
47 """Given a map x -> [y1, y2...] returns inverse mapping y->[x1, x2...]."""
48 y_to_xs = {}
49 for x, ys in x_to_ys.items():
50 for y in ys:
51 y_to_xs.setdefault(y, []).append(x)
52 return y_to_xs
55 def GetObjDir(libchrome):
56 """Get the path to the obj directory corresponding to the given libchrome.
58 Assumes libchrome is in for example .../Release/lib/libchrome.so and object
59 files are in .../Release/obj.
60 """
61 # TODO(azarchs): Pass obj path in explicitly where needed rather than relying
62 # on the above assumption.
63 return os.path.abspath(os.path.join(
64 os.path.dirname(libchrome), '../obj'))