blob: 97829d45b2a91f65a90224325426bd64363637f7 [file] [log] [blame]
smut@google.com5f0788b2015-06-09 00:04:51 +00001#!/usr/bin/env python
2# Copyright (c) 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Tool for interacting with Buildbucket.
7
8Usage:
9 $ depot-tools-auth login https://cr-buildbucket.appspot.com
10 $ buildbucket.py \
11 put \
12 --bucket master.tryserver.chromium.linux \
13 --builder my-builder \
14
15 Puts a build into buildbucket for my-builder on tryserver.chromium.linux.
16"""
17
18import argparse
19import json
20import urlparse
21import os
22import sys
23
24from third_party import httplib2
25
26import auth
27
28
29BUILDBUCKET_URL = 'https://cr-buildbucket.appspot.com'
30PUT_BUILD_URL = urlparse.urljoin(
31 BUILDBUCKET_URL,
32 '_ah/api/buildbucket/v1/builds',
33)
34
35
36def main(argv):
37 parser = argparse.ArgumentParser()
38 parser.add_argument(
39 '-v',
40 '--verbose',
41 action='store_true',
42 )
43 subparsers = parser.add_subparsers(dest='command')
44 put_parser = subparsers.add_parser('put')
45 put_parser.add_argument(
smut@google.com1da6b032015-06-09 00:16:03 +000046 '-b',
smut@google.com5f0788b2015-06-09 00:04:51 +000047 '--bucket',
48 help=(
49 'The bucket to schedule the build on. Typically the master name, e.g.'
50 ' master.tryserver.chromium.linux.'
51 ),
52 required=True,
53 )
54 put_parser.add_argument(
55 '-n',
56 '--builder-name',
57 help='The builder to schedule the build on.',
58 required=True,
59 )
60 put_parser.add_argument(
61 '-p',
62 '--properties',
63 help='A file to load a JSON dict of properties from.',
64 )
65 args = parser.parse_args()
66 # TODO(smut): When more commands are implemented, refactor this.
67 assert args.command == 'put'
68
69 properties = {}
70 if args.properties:
71 try:
72 with open(args.properties) as fp:
73 properties.update(json.load(fp))
74 except (TypeError, ValueError):
75 sys.stderr.write('%s contained invalid JSON dict.\n' % args.properties)
76 raise
77
78 authenticator = auth.get_authenticator_for_host(
79 BUILDBUCKET_URL,
80 auth.make_auth_config(use_oauth2=True),
81 )
82 http = authenticator.authorize(httplib2.Http())
83 http.force_exception_to_status_code = True
84 response, content = http.request(
85 PUT_BUILD_URL,
86 'PUT',
87 body=json.dumps({
88 'bucket': args.bucket,
89 'parameters_json': json.dumps({
90 'builder_name': args.builder_name,
91 'properties': properties,
92 }),
93 }),
94 headers={'Content-Type': 'application/json'},
95 )
96
97 if args.verbose:
98 print content
99
100 return response.status != 200
101
102
103if __name__ == '__main__':
104 sys.exit(main(sys.argv))