Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The LUCI Authors. All rights reserved. | 2 # Copyright 2013 The LUCI Authors. All rights reserved. |
| 3 # Use of this source code is governed under the Apache License, Version 2.0 | 3 # Use of this source code is governed under the Apache License, Version 2.0 |
| 4 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """Archives a set of files or directories to an Isolate Server.""" | 6 """Archives a set of files or directories to an Isolate Server.""" |
| 7 | 7 |
| 8 __version__ = '0.4.8' | 8 __version__ = '0.4.8' |
| 9 | 9 |
| 10 import base64 | 10 import base64 |
| (...skipping 1200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1211 | 1211 |
| 1212 def __init__(self): | 1212 def __init__(self): |
| 1213 self._lock = threading_utils.LockWithAssert() | 1213 self._lock = threading_utils.LockWithAssert() |
| 1214 # Profiling values. | 1214 # Profiling values. |
| 1215 self._added = [] | 1215 self._added = [] |
| 1216 self._initial_number_items = 0 | 1216 self._initial_number_items = 0 |
| 1217 self._initial_size = 0 | 1217 self._initial_size = 0 |
| 1218 self._evicted = [] | 1218 self._evicted = [] |
| 1219 self._linked = [] | 1219 self._linked = [] |
| 1220 | 1220 |
| 1221 def __contains__(self, digest): | |
| 1222 raise NotImplementedError() | |
| 1223 | |
| 1221 def __enter__(self): | 1224 def __enter__(self): |
| 1222 """Context manager interface.""" | 1225 """Context manager interface.""" |
| 1223 return self | 1226 return self |
| 1224 | 1227 |
| 1225 def __exit__(self, _exc_type, _exec_value, _traceback): | 1228 def __exit__(self, _exc_type, _exec_value, _traceback): |
| 1226 """Context manager interface.""" | 1229 """Context manager interface.""" |
| 1227 return False | 1230 return False |
| 1228 | 1231 |
| 1229 @property | 1232 @property |
| 1230 def added(self): | 1233 def added(self): |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1295 | 1298 |
| 1296 def __init__(self, file_mode_mask=0500): | 1299 def __init__(self, file_mode_mask=0500): |
| 1297 """Args: | 1300 """Args: |
| 1298 file_mode_mask: bit mask to AND file mode with. Default value will make | 1301 file_mode_mask: bit mask to AND file mode with. Default value will make |
| 1299 all mapped files to be read only. | 1302 all mapped files to be read only. |
| 1300 """ | 1303 """ |
| 1301 super(MemoryCache, self).__init__() | 1304 super(MemoryCache, self).__init__() |
| 1302 self._file_mode_mask = file_mode_mask | 1305 self._file_mode_mask = file_mode_mask |
| 1303 self._contents = {} | 1306 self._contents = {} |
| 1304 | 1307 |
| 1308 def __contains__(self, digest): | |
| 1309 with self._lock: | |
| 1310 return digest in self._contents | |
| 1311 | |
| 1305 def cached_set(self): | 1312 def cached_set(self): |
| 1306 with self._lock: | 1313 with self._lock: |
| 1307 return set(self._contents) | 1314 return set(self._contents) |
| 1308 | 1315 |
| 1309 def cleanup(self): | 1316 def cleanup(self): |
| 1310 pass | 1317 pass |
| 1311 | 1318 |
| 1312 def touch(self, digest, size): | 1319 def touch(self, digest, size): |
| 1313 with self._lock: | 1320 with self._lock: |
| 1314 return digest in self._contents | 1321 return digest in self._contents |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1388 # The items that must not be evicted during this run since they were | 1395 # The items that must not be evicted during this run since they were |
| 1389 # referenced. | 1396 # referenced. |
| 1390 self._protected = set() | 1397 self._protected = set() |
| 1391 # Cleanup operations done by self._load(), if any. | 1398 # Cleanup operations done by self._load(), if any. |
| 1392 self._operations = [] | 1399 self._operations = [] |
| 1393 with tools.Profiler('Setup'): | 1400 with tools.Profiler('Setup'): |
| 1394 with self._lock: | 1401 with self._lock: |
| 1395 # self._load() calls self._trim() which initializes self._free_disk. | 1402 # self._load() calls self._trim() which initializes self._free_disk. |
| 1396 self._load() | 1403 self._load() |
| 1397 | 1404 |
| 1405 def __contains__(self, digest): | |
| 1406 with self._lock: | |
| 1407 return digest in self._lru | |
| 1408 | |
| 1398 def __enter__(self): | 1409 def __enter__(self): |
| 1399 return self | 1410 return self |
| 1400 | 1411 |
| 1401 def __exit__(self, _exc_type, _exec_value, _traceback): | 1412 def __exit__(self, _exc_type, _exec_value, _traceback): |
| 1402 with tools.Profiler('CleanupTrimming'): | 1413 with tools.Profiler('CleanupTrimming'): |
| 1403 with self._lock: | 1414 with self._lock: |
| 1404 self._trim() | 1415 self._trim() |
| 1405 | 1416 |
| 1406 logging.info( | 1417 logging.info( |
| 1407 '%5d (%8dkb) added', | 1418 '%5d (%8dkb) added', |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1516 # Report to the server that it failed with more details. We'll want to | 1527 # Report to the server that it failed with more details. We'll want to |
| 1517 # squash them all. | 1528 # squash them all. |
| 1518 on_error.report('Failed to hardlink\n%s -> %s' % (path, dest)) | 1529 on_error.report('Failed to hardlink\n%s -> %s' % (path, dest)) |
| 1519 | 1530 |
| 1520 if file_mode is not None: | 1531 if file_mode is not None: |
| 1521 # Ignores all other bits. | 1532 # Ignores all other bits. |
| 1522 fs.chmod(dest, file_mode & 0500) | 1533 fs.chmod(dest, file_mode & 0500) |
| 1523 with self._lock: | 1534 with self._lock: |
| 1524 self._linked.append(self._lru[digest]) | 1535 self._linked.append(self._lru[digest]) |
| 1525 | 1536 |
| 1537 def item_path(self, digest): | |
|
M-A Ruel
2016/06/08 20:37:39
It's not needed.
nodir
2016/06/08 22:35:32
Done.
| |
| 1538 """Returns a path on disk of the cached item. | |
| 1539 | |
| 1540 Raises: | |
| 1541 CacheMiss if the item is not present in cache. | |
| 1542 """ | |
| 1543 if digest not in self._lru: | |
| 1544 raise CacheMiss(digest) | |
| 1545 return self._path(digest) | |
| 1546 | |
| 1526 def _load(self): | 1547 def _load(self): |
| 1527 """Loads state of the cache from json file.""" | 1548 """Loads state of the cache from json file.""" |
| 1528 self._lock.assert_locked() | 1549 self._lock.assert_locked() |
| 1529 | 1550 |
| 1530 if not os.path.isdir(self.cache_dir): | 1551 if not os.path.isdir(self.cache_dir): |
| 1531 fs.makedirs(self.cache_dir) | 1552 fs.makedirs(self.cache_dir) |
| 1532 else: | 1553 else: |
| 1533 # Make sure the cache is read-only. | 1554 # Make sure the cache is read-only. |
| 1534 # TODO(maruel): Calculate the cost and optimize the performance | 1555 # TODO(maruel): Calculate the cost and optimize the performance |
| 1535 # accordingly. | 1556 # accordingly. |
| (...skipping 751 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2287 dispatcher = subcommand.CommandDispatcher(__name__) | 2308 dispatcher = subcommand.CommandDispatcher(__name__) |
| 2288 return dispatcher.execute(OptionParserIsolateServer(), args) | 2309 return dispatcher.execute(OptionParserIsolateServer(), args) |
| 2289 | 2310 |
| 2290 | 2311 |
| 2291 if __name__ == '__main__': | 2312 if __name__ == '__main__': |
| 2292 subprocess42.inhibit_os_error_reporting() | 2313 subprocess42.inhibit_os_error_reporting() |
| 2293 fix_encoding.fix_encoding() | 2314 fix_encoding.fix_encoding() |
| 2294 tools.disable_buffering() | 2315 tools.disable_buffering() |
| 2295 colorama.init() | 2316 colorama.init() |
| 2296 sys.exit(main(sys.argv[1:])) | 2317 sys.exit(main(sys.argv[1:])) |
| OLD | NEW |