File: chromeos_device_trigger.py

package info (click to toggle)
chromium 73.0.3683.75-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,792,156 kB
  • sloc: cpp: 13,473,466; ansic: 1,577,080; python: 898,539; javascript: 655,737; xml: 341,883; asm: 306,070; java: 289,969; perl: 80,911; objc: 67,198; sh: 43,184; cs: 27,853; makefile: 12,092; php: 11,064; yacc: 10,373; tcl: 8,875; ruby: 3,941; lex: 1,800; pascal: 1,473; lisp: 812; awk: 41; jsp: 39; sed: 19; sql: 3
file content (118 lines) | stat: -rwxr-xr-x 4,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python
# Copyright 2019 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Custom swarming trigger script for ChromeOS device tests.

CrOS device tests are unique in that the device OS they prefer to run on is
continuously changing. The LKGM file, checked into src at
//chromeos/CHROMEOS_LKGM, represents the ChromeOS version Chrome's ToT aims
to be compatible with. Therefore, a CrOS test for Chrome ideally targets a
device running the LKGM.

Since the LKGM file gets updated frequently (~daily), we can't reasonably
hardcode the LKGM in the test specs. So this special trigger script will read
the current LKGM (at the time of trigger) and append that to the task's
dimensions. If such a device isn't available in time, the task will fallback
to one running any OS.
"""

import argparse
import os
import re
import sys

import base_test_triggerer


SRC_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(
  __file__))))
LKGM_FILE_PATH = os.path.join(SRC_DIR, 'chromeos', 'CHROMEOS_LKGM')
# Should match something that looks like "12345.0.0".
LKGM_RE = re.compile(r'\d+\.\d+\.\d+')


def read_current_lkgm():
  if not os.path.exists(LKGM_FILE_PATH):
    sys.stderr.write('LKGM file not present at %s\n' % LKGM_FILE_PATH)
    return None

  with open(LKGM_FILE_PATH) as f:
    lkgm = f.read().strip()

  if not LKGM_RE.match(lkgm):
    sys.stderr.write('Unknown format of LKGM: %s\n' % lkgm)
    return None

  # Just the major version should be sufficient.
  return lkgm.split('.')[0]


def parse_args():
  # This script will do nothing but inspect and tweak the dimension args to
  # `swarming.py trigger`. So let's pull just those out.
  parser = argparse.ArgumentParser(description=__doc__)
  parser.add_argument(
      '-d', '--dimension', default=[], action='append', nargs=2,
      dest='dimensions',
      help='Dimensions to filter on. Duplicated from the `swarming.py trigger` '
           'command. Parsed here to ensure `device_os` is not added.'
  )
  parser.add_argument(
      '--optional-dimension', default=[], action='append', nargs=3,
      dest='optional_dimensions',
      help='Optional dimensions which will result in additional task slices. '
           'Duplicated from the `swarming.py trigger` command.')
  parser.add_argument(
      '--primary-expiration', type=int, default=600,
      help='How long to wait (in seconds) for an available bot in the primary '
           'task slice.')
  args, additional_args = parser.parse_known_args()

  if additional_args[0] != 'trigger':
    parser.error(
        'This script is only supported for `swarming.py trigger` invocations.')

  for k, _ in args.dimensions:
    if k == 'device_os':
      parser.error(
          'Must not specify the device_os dimension when using this script. '
          '(It will be added automatically.)')

  # It might be a valid use-case to include optional-dimensions in the initial
  # invocation. But it'd be difficult to integrate them into what we're doing
  # here. So let's just ensure there aren't any.
  if args.optional_dimensions:
    parser.error(
        'Must not specify optional dimensions when using this script.')

  return args, additional_args


def main():
  args, additional_args = parse_args()

  current_lkgm = read_current_lkgm()
  if not current_lkgm:
    return 1

  new_args = additional_args[:1]
  # Insert our modified dimension args in between the 1st and 2nd args of the
  # initial `swarming.py` invocation. This avoids the presence of the special
  # `--` arg from causing swarming.py to ignore them.
  for k, v in args.dimensions:
    new_args.extend(['--dimension', k, v])
  new_args.extend([
      '--optional-dimension',
      'device_os',
      current_lkgm,
      str(args.primary_expiration),
  ])
  new_args += additional_args[1:]

  return base_test_triggerer.BaseTestTriggerer().run_swarming(new_args, True)


if __name__ == '__main__':
  sys.exit(main())