Stop python icon bouncing in the dock while running browser benchmarks.
[WebKit.git] / Tools / Scripts / webkitpy / benchmark_runner / utils.py
1 #!/usr/bin/env python
2
3 import imp
4 import inspect
5 import logging
6 import os
7 import signal
8 import shutil
9 import sys
10
11
12 _log = logging.getLogger(__name__)
13
14
15 # Borrow following code from stackoverflow
16 # Link: http://stackoverflow.com/questions/11461356/issubclass-returns-flase-on-the-same-class-imported-from-different-paths
17 def is_subclass(child, parent_name):
18     return inspect.isclass(child) and parent_name in [cls.__name__ for cls in inspect.getmro(child)]
19
20
21 def load_subclasses(dirname, base_class_name, base_class_file, loader):
22     filelist = [base_class_file] + [f for f in os.listdir(dirname) if f.endswith('.py') and f not in ['__init__.py', base_class_file]]
23     for filename in filelist:
24         module_name = os.path.splitext(filename)[0]
25         module = imp.load_source(module_name, os.path.join(dirname, filename))
26         for item_name in dir(module):
27             item = getattr(module, item_name)
28             if is_subclass(item, base_class_name):
29                 loader(item)
30
31
32 def get_path_from_project_root(relative_path_to_project_root):
33     # Choose the directory containing current file as start point,
34     # compute relative path base on the parameter,
35     # and return an absolute path
36     return os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), relative_path_to_project_root))
37
38
39 def force_remove(path):
40     try:
41         shutil.rmtree(path)
42     except Exception as error:
43         # Directory/file does not exist or privilege issue, just ignore it
44         _log.info("Error removing %s: %s" % (path, error))
45         pass
46
47
48 def write_defaults(domain, key, value):
49     # Returns whether the key in the domain is updated
50     from Foundation import NSUserDefaults
51     defaults = NSUserDefaults.standardUserDefaults()
52     defaults_for_domain = defaults.persistentDomainForName_(domain)
53     if not defaults_for_domain:
54         return False
55     old_value = defaults_for_domain.get(key)
56     if old_value == value:
57         return False
58     mutable_defaults_for_domain = defaults_for_domain.mutableCopy()
59     mutable_defaults_for_domain[key] = value
60     defaults.setPersistentDomain_forName_(mutable_defaults_for_domain, domain)
61     defaults.synchronize()
62     return True
63
64
65 # Borrow this code from
66 # 'http://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish'
67 class TimeoutError(Exception):
68     pass
69
70
71 class timeout:
72
73     def __init__(self, seconds=1, error_message='Timeout'):
74         self.seconds = seconds
75         self.error_message = error_message
76
77     def handle_timeout(self, signum, frame):
78         raise TimeoutError(self.error_message)
79
80     def __enter__(self):
81         signal.signal(signal.SIGALRM, self.handle_timeout)
82         signal.alarm(self.seconds)
83
84     def __exit__(self, type, value, traceback):
85         signal.alarm(0)