blob: f86f2cda75c9e03e6688c2b9887b02d06b26d8ff [file] [log] [blame]
Eric Boren63765172017-10-16 14:22:47 -04001#!/usr/bin/env python
2
3# Copyright 2017 Google Inc.
4#
5# Use of this source code is governed by a BSD-style license that can be
6# found in the LICENSE file.
7
8
9"""Submit one or more try jobs."""
10
11
12import argparse
13import json
14import os
15import re
16import subprocess
17import sys
Eric Borend5c128b2017-10-17 14:50:26 -040018import tempfile
Eric Boren63765172017-10-16 14:22:47 -040019
20
Eric Borencff9f952017-10-17 09:18:18 -040021BUCKET_SKIA_PRIMARY = 'skia.primary'
Eric Borend5c128b2017-10-17 14:50:26 -040022BUCKET_SKIA_INTERNAL = 'skia.internal'
Eric Boren63765172017-10-16 14:22:47 -040023CHECKOUT_ROOT = os.path.realpath(os.path.join(
24 os.path.dirname(os.path.abspath(__file__)), os.pardir))
Eric Borencff9f952017-10-17 09:18:18 -040025INFRA_BOTS = os.path.join(CHECKOUT_ROOT, 'infra', 'bots')
26JOBS_JSON = os.path.join(INFRA_BOTS, 'jobs.json')
Eric Borend5c128b2017-10-17 14:50:26 -040027REPO_INTERNAL = 'https://skia.googlesource.com/internal_test.git'
28TMP_DIR = os.path.join(tempfile.gettempdir(), 'sktry')
Eric Borencff9f952017-10-17 09:18:18 -040029
30sys.path.insert(0, INFRA_BOTS)
31
32import update_meta_config
Eric Borend5c128b2017-10-17 14:50:26 -040033import utils
34
35
36def get_jobs(repo):
37 """Obtain the list of jobs from the given repo."""
38 # Maintain a copy of the repo in the temp dir.
39 if not os.path.isdir(TMP_DIR):
40 os.mkdir(TMP_DIR)
41 with utils.chdir(TMP_DIR):
42 dirname = repo.split('/')[-1]
43 if not os.path.isdir(dirname):
44 subprocess.check_call([
45 utils.GIT, 'clone', '--mirror', repo, dirname])
46 with utils.chdir(dirname):
47 subprocess.check_call([utils.GIT, 'remote', 'update'])
48 jobs = json.loads(subprocess.check_output([
49 utils.GIT, 'show', 'master:infra/bots/jobs.json']))
50 return (BUCKET_SKIA_INTERNAL, jobs)
Eric Boren63765172017-10-16 14:22:47 -040051
52
53def main():
54 # Parse arguments.
55 d = 'Helper script for triggering try jobs defined in %s.' % JOBS_JSON
56 parser = argparse.ArgumentParser(description=d)
57 parser.add_argument('--list', action='store_true', default=False,
58 help='Just list the jobs; do not trigger anything.')
Eric Borend5c128b2017-10-17 14:50:26 -040059 parser.add_argument('--internal', action='store_true', default=False,
60 help=('If set, include internal jobs. You must have '
61 'permission to view internal repos.'))
Eric Boren63765172017-10-16 14:22:47 -040062 parser.add_argument('job', nargs='?', default=None,
63 help='Job name or regular expression to match job names.')
64 args = parser.parse_args()
65
Eric Borend5c128b2017-10-17 14:50:26 -040066 # Load and filter the list of jobs.
Eric Borencff9f952017-10-17 09:18:18 -040067 jobs = []
Eric Boren63765172017-10-16 14:22:47 -040068 with open(JOBS_JSON) as f:
Eric Borencff9f952017-10-17 09:18:18 -040069 jobs.append((BUCKET_SKIA_PRIMARY, json.load(f)))
Eric Borend5c128b2017-10-17 14:50:26 -040070 if args.internal:
71 jobs.append(get_jobs(REPO_INTERNAL))
Eric Borencff9f952017-10-17 09:18:18 -040072 jobs.extend(update_meta_config.CQ_INCLUDE_CHROMIUM_TRYBOTS)
Eric Boren63765172017-10-16 14:22:47 -040073 if args.job:
Eric Borenc9080c82017-10-18 12:53:49 -040074 filtered_jobs = []
Eric Borencff9f952017-10-17 09:18:18 -040075 for bucket, job_list in jobs:
76 filtered = [j for j in job_list if re.search(args.job, j)]
77 if len(filtered) > 0:
Eric Borenc9080c82017-10-18 12:53:49 -040078 filtered_jobs.append((bucket, filtered))
79 jobs = filtered_jobs
Eric Boren63765172017-10-16 14:22:47 -040080
81 # Display the list of jobs.
82 if len(jobs) == 0:
83 print 'Found no jobs matching "%s"' % repr(args.job)
84 sys.exit(1)
Eric Borencff9f952017-10-17 09:18:18 -040085 count = 0
86 for bucket, job_list in jobs:
87 count += len(job_list)
88 print 'Found %d jobs:' % count
89 for bucket, job_list in jobs:
90 print ' %s:' % bucket
91 for j in job_list:
92 print ' %s' % j
Eric Boren63765172017-10-16 14:22:47 -040093 if args.list:
94 return
95
Eric Borencbe99c02017-12-11 13:18:52 -050096 if count > 1:
97 # Prompt before triggering jobs.
98 resp = raw_input('\nDo you want to trigger these jobs? (y/n or i for '
99 'interactive): ')
100 print ''
101 if resp != 'y' and resp != 'i':
102 sys.exit(1)
103 if resp == 'i':
104 filtered_jobs = []
105 for bucket, job_list in jobs:
106 new_job_list = []
107 for j in job_list:
108 incl = raw_input(('Trigger %s? (y/n): ' % j))
109 if incl == 'y':
110 new_job_list.append(j)
111 if len(new_job_list) > 0:
112 filtered_jobs.append((bucket, new_job_list))
113 jobs = filtered_jobs
Eric Boren63765172017-10-16 14:22:47 -0400114
115 # Trigger the try jobs.
Eric Borencff9f952017-10-17 09:18:18 -0400116 for bucket, job_list in jobs:
117 cmd = ['git', 'cl', 'try', '-B', bucket]
118 for j in job_list:
119 cmd.extend(['-b', j])
120 try:
121 subprocess.check_call(cmd)
122 except subprocess.CalledProcessError:
123 # Output from the command will fall through, so just exit here rather than
124 # printing a stack trace.
125 sys.exit(1)
Eric Boren63765172017-10-16 14:22:47 -0400126
127
128if __name__ == '__main__':
129 main()