Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # coding: utf-8 | |
| 1 # Copyright 2014 The LUCI Authors. All rights reserved. | 2 # Copyright 2014 The LUCI Authors. All rights reserved. |
| 2 # 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 |
| 3 # that can be found in the LICENSE file. | 4 # that can be found in the LICENSE file. |
| 4 | 5 |
| 5 """OS specific utility functions. | 6 """OS specific utility functions. |
| 6 | 7 |
| 7 Includes code: | 8 Includes code: |
| 8 - to declare the current system this code is running under. | 9 - to declare the current system this code is running under. |
| 9 - to run a command on user login. | 10 - to run a command on user login. |
| 10 - to reboot the host. | 11 - to reboot the host. |
| (...skipping 496 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 507 if sys.platform == 'darwin': | 508 if sys.platform == 'darwin': |
| 508 return platforms.osx.get_uptime() | 509 return platforms.osx.get_uptime() |
| 509 if sys.platform == 'win32': | 510 if sys.platform == 'win32': |
| 510 return platforms.win.get_uptime() | 511 return platforms.win.get_uptime() |
| 511 if sys.platform == 'cygwin': | 512 if sys.platform == 'cygwin': |
| 512 # Not important. | 513 # Not important. |
| 513 return 0. | 514 return 0. |
| 514 return platforms.linux.get_uptime() | 515 return platforms.linux.get_uptime() |
| 515 | 516 |
| 516 | 517 |
| 518 def get_named_caches(): | |
| 519 """Returns the list of named caches.""" | |
| 520 # Strictly speaking, this is a layering violation. This data is managed by | |
| 521 # run_isolated.py but this is valuable to expose this as a Swarming bot | |
| 522 # dimensions so ¯\_(ツ)_/¯ | |
| 523 # | |
| 524 # Assumptions: | |
| 525 # - ../__main__.py calls os.chdir(__file__) | |
| 526 # - ../bot_code/bot_main.py specifies | |
| 527 # --named-cache-root os.path.join(botobj.base_dir, 'c') to run_isolated. | |
| 528 # - ../client/named_cache.py behavior | |
| 529 # | |
| 530 # A better implementation would require: | |
| 531 # - Access to bot.Bot instance to query bot.base_dir | |
| 532 # - Access to named_cache.py to load state.json | |
| 533 # - Access to --named-cache-root hardcoded in bot_main.py | |
| 534 # | |
| 535 # but hey, the following code is 5 lines... | |
| 536 try: | |
| 537 with open(os.path.join(os.getcwd(), u'c', u'state.json'), 'rb') as f: | |
|
nodir
2017/05/31 21:03:21
is os.getcwd() needed? wouldn't open() do it for y
M-A Ruel
2017/06/02 22:05:31
Removed, that's true that it was not necessary.
| |
| 538 return sorted(i[0] for i in json.load(f)['items']) | |
| 539 except (IOError, KeyError, OSError): | |
| 540 return [] | |
| 541 | |
| 542 | |
| 517 class AuthenticatedHttpRequestFailure(Exception): | 543 class AuthenticatedHttpRequestFailure(Exception): |
| 518 pass | 544 pass |
| 519 | 545 |
| 520 | 546 |
| 521 def authenticated_http_request(service_account, *args, **kwargs): | 547 def authenticated_http_request(service_account, *args, **kwargs): |
| 522 """Sends an OAuth2-authenticated HTTP request. | 548 """Sends an OAuth2-authenticated HTTP request. |
| 523 | 549 |
| 524 Args: | 550 Args: |
| 525 service_account: Service account to use. For GCE, the name of the service | 551 service_account: Service account to use. For GCE, the name of the service |
| 526 account, otherwise the path to the service account JSON file. | 552 account, otherwise the path to the service account JSON file. |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 842 u'cores': [unicode(get_num_processors())], | 868 u'cores': [unicode(get_num_processors())], |
| 843 u'cpu': [ | 869 u'cpu': [ |
| 844 cpu_type, | 870 cpu_type, |
| 845 cpu_type + u'-' + cpu_bitness, | 871 cpu_type + u'-' + cpu_bitness, |
| 846 ], | 872 ], |
| 847 u'gpu': get_gpu()[0], | 873 u'gpu': get_gpu()[0], |
| 848 u'id': [get_hostname_short()], | 874 u'id': [get_hostname_short()], |
| 849 u'os': [os_name], | 875 u'os': [os_name], |
| 850 u'pool': [u'default'], | 876 u'pool': [u'default'], |
| 851 } | 877 } |
| 878 caches = get_named_caches() | |
| 879 if caches: | |
| 880 dimensions[u'caches'] = caches | |
| 852 if u'avx2' in cpuinfo.get(u'flags', []): | 881 if u'avx2' in cpuinfo.get(u'flags', []): |
| 853 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx2') | 882 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx2') |
| 854 if any(u'avx512' in x for x in cpuinfo.get(u'flags', [])): | 883 if any(u'avx512' in x for x in cpuinfo.get(u'flags', [])): |
| 855 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx512') | 884 dimensions[u'cpu'].append(cpu_type + u'-' + cpu_bitness + u'-avx512') |
| 856 if sys.platform == 'win32': | 885 if sys.platform == 'win32': |
| 857 dimensions[u'os'].extend( | 886 dimensions[u'os'].extend( |
| 858 u'%s-%s' % (os_name, n) for n in platforms.win.get_os_version_names()) | 887 u'%s-%s' % (os_name, n) for n in platforms.win.get_os_version_names()) |
| 859 else: | 888 else: |
| 860 dimensions[u'os'].append(u'%s-%s' % (os_name, get_os_version_number())) | 889 dimensions[u'os'].append(u'%s-%s' % (os_name, get_os_version_number())) |
| 861 if u'none' not in dimensions[u'gpu']: | 890 if u'none' not in dimensions[u'gpu']: |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1204 | 1233 |
| 1205 | 1234 |
| 1206 def trim_rolled_log(name): | 1235 def trim_rolled_log(name): |
| 1207 try: | 1236 try: |
| 1208 for item in glob.iglob('%s.??' % name): | 1237 for item in glob.iglob('%s.??' % name): |
| 1209 os.remove(item) | 1238 os.remove(item) |
| 1210 for item in glob.iglob('%s.???' % name): | 1239 for item in glob.iglob('%s.???' % name): |
| 1211 os.remove(item) | 1240 os.remove(item) |
| 1212 except Exception as e: | 1241 except Exception as e: |
| 1213 logging.exception('trim_rolled_log(%s) failed: %s', name, e) | 1242 logging.exception('trim_rolled_log(%s) failed: %s', name, e) |
| OLD | NEW |