| 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 LoadComponentsDatabaseFile(filename): | 248 def LoadComponentsDatabaseFile(filename): |
| 222 """ Loads a components database file. """ | 249 """ Loads a components database file. """ |
| 223 with open(filename) as database: | 250 with open(filename) as database: |
| 224 return eval(database.read()) | 251 return eval(database.read()) |
| 225 | 252 |
| 226 | 253 |
| 227 def GetComponentsDatabaseBase(database_file): | 254 def GetComponentsDatabaseBase(database_file): |
| 228 """ Gets the base folder of a components database file. """ | 255 """ Gets the base folder of a components database file. """ |
| 229 # Currently the database file is assuming properties sit in its parent folder. | 256 # Currently the database file is assuming properties sit in its parent folder. |
| 230 base = os.path.join(os.path.split(os.path.abspath(database_file))[0], '..') | 257 base = os.path.join(os.path.split(os.path.abspath(database_file))[0], '..') |
| 231 return os.path.abspath(base) | 258 return os.path.abspath(base) |
| 232 | 259 |
| 233 | 260 |
| 234 if __name__ == '__main__': | 261 if __name__ == '__main__': |
| 235 print "Google Factory Tool Common Library." | 262 print "Google Factory Tool Common Library." |
| OLD | NEW |