OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Google Factory Tools Common Library | 7 """Google Factory Tools Common Library |
8 | 8 |
9 This module includes several common utilities for Google Factory Tools | 9 This module includes several common utilities for Google Factory Tools |
10 A detailed description of gft_common. | 10 A detailed description of gft_common. |
11 """ | 11 """ |
12 | 12 |
13 import os | 13 import os |
14 import subprocess | 14 import subprocess |
15 import sys | 15 import sys |
16 import tempfile | 16 import tempfile |
| 17 import threading |
17 import time | 18 import time |
18 | 19 |
19 | 20 |
20 ######################################################################## | 21 ######################################################################## |
21 # Global Variables | 22 # Global Variables |
22 | 23 |
23 | 24 |
24 _debug = False | 25 _debug = False |
25 _verbose = False | 26 _verbose = False |
26 _log_path = None | 27 _log_path = None |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 with open(filename, "rb") as opened_file: | 208 with open(filename, "rb") as opened_file: |
208 return opened_file.read() | 209 return opened_file.read() |
209 | 210 |
210 | 211 |
211 def WriteFile(filename, data): | 212 def WriteFile(filename, data): |
212 """ Writes one file and exit. """ | 213 """ Writes one file and exit. """ |
213 with open(filename, "w") as opened_file: | 214 with open(filename, "w") as opened_file: |
214 opened_file.write(data) | 215 opened_file.write(data) |
215 | 216 |
216 | 217 |
| 218 def ThreadSafe(f): |
| 219 """ Decorator for functions that need synchronoization. """ |
| 220 lock = threading.Lock() |
| 221 def threadsafe_call(*args): |
| 222 try: |
| 223 lock.acquire() |
| 224 return f(*args) |
| 225 finally: |
| 226 lock.release() |
| 227 return threadsafe_call |
| 228 |
| 229 |
| 230 def Memorize(f): |
| 231 """ Decorator for functions that need memorization. """ |
| 232 memorize_data = {} |
| 233 def memorize_call(*args): |
| 234 index = repr(args) |
| 235 if index in memorize_data: |
| 236 value = memorize_data[index] |
| 237 # DebugMsg('Memorize: using cached value for: %s %s' % (repr(f), index)) |
| 238 return value |
| 239 value = f(*args) |
| 240 memorize_data[index] = value |
| 241 return value |
| 242 return memorize_call |
| 243 |
217 ######################################################################## | 244 ######################################################################## |
218 # Components Databases | 245 # Components Databases |
219 | 246 |
220 | 247 |
221 def ExpandComponentsDatabaseMacro(data, database_file): | 248 def ExpandComponentsDatabaseMacro(data, database_file): |
222 """ Processes and expands macros in a component database. """ | 249 """ Processes and expands macros in a component database. """ |
223 allow_macro_fields = ( | 250 allow_macro_fields = ( |
224 'data_bitmap_fv' | 251 'data_bitmap_fv' |
225 ) | 252 ) |
226 macros = { | 253 macros = { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 | 310 |
284 def GetComponentsDatabaseBase(database_file): | 311 def GetComponentsDatabaseBase(database_file): |
285 """ Gets the base folder of a components database file. """ | 312 """ Gets the base folder of a components database file. """ |
286 # Currently the database file is assuming properties sit in its parent folder. | 313 # Currently the database file is assuming properties sit in its parent folder. |
287 base = os.path.join(os.path.split(os.path.abspath(database_file))[0], '..') | 314 base = os.path.join(os.path.split(os.path.abspath(database_file))[0], '..') |
288 return os.path.abspath(base) | 315 return os.path.abspath(base) |
289 | 316 |
290 | 317 |
291 if __name__ == '__main__': | 318 if __name__ == '__main__': |
292 print "Google Factory Tool Common Library." | 319 print "Google Factory Tool Common Library." |
OLD | NEW |