Allen Li | 24a5ad7 | 2018-03-23 15:52:33 -0700 | [diff] [blame] | 1 | # Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Allen Li | 15b3e21 | 2018-03-23 16:01:17 -0700 | [diff] [blame] | 5 | from __future__ import absolute_import |
| 6 | from __future__ import division |
| 7 | from __future__ import print_function |
| 8 | |
Allen Li | 24a5ad7 | 2018-03-23 15:52:33 -0700 | [diff] [blame] | 9 | """Extra functions for working with files in the Autotest results directory. |
| 10 | |
| 11 | These originate from the Autotest scheduler code and have been moved |
| 12 | here as lucifer takes ownership of running jobs from the scheduler. |
| 13 | """ |
| 14 | |
| 15 | import os |
| 16 | |
| 17 | |
| 18 | def write_status_comment(results_dir, comment): |
| 19 | """Write status comment in status.log. |
| 20 | |
| 21 | @param results_dir: results directory |
| 22 | @param comment: comment string |
| 23 | """ |
| 24 | with open(_status_file(results_dir), 'a') as f: |
| 25 | f.write('INFO\t----\t----\t%s' % (comment,)) |
| 26 | |
| 27 | |
| 28 | def write_host_keyvals(results_dir, hostname, keyvals): |
| 29 | """Write host keyvals to the results directory. |
| 30 | |
| 31 | @param results_dir: results directory |
| 32 | @param hostname: Hostname of host as string |
| 33 | @param keyvals: dict |
| 34 | """ |
| 35 | keyvals_dir = os.path.join(results_dir, 'host_keyvals') |
| 36 | try: |
| 37 | os.makedirs(keyvals_dir) |
| 38 | except OSError: |
| 39 | pass |
| 40 | keyvals_path = os.path.join(keyvals_dir, hostname) |
| 41 | with open(keyvals_path, 'w') as f: |
| 42 | f.write(_format_keyvals(keyvals)) |
| 43 | |
| 44 | |
| 45 | def write_keyvals(results_dir, keyvals): |
| 46 | """Write keyvals to the results directory. |
| 47 | |
| 48 | @param results_dir: results directory |
| 49 | @param keyvals: dict |
| 50 | """ |
| 51 | with open(_keyvals_file(results_dir), 'a') as f: |
| 52 | f.write(_format_keyvals(keyvals)) |
| 53 | |
| 54 | |
| 55 | def _status_file(results_dir): |
| 56 | """Return the path to the status.log file.""" |
| 57 | return os.path.join(results_dir, 'status.log') |
| 58 | |
| 59 | |
| 60 | def _keyvals_file(results_dir): |
| 61 | """Return the path to the keyvals file.""" |
| 62 | return os.path.join(results_dir, 'keyval') |
| 63 | |
| 64 | |
| 65 | def _format_keyvals(keyvals): |
| 66 | """Format a dict of keyvals as a string.""" |
| 67 | return ''.join('%s=%s\n' % (k, v) for k, v in keyvals.iteritems()) |