smut@google.com | 5f0788b | 2015-06-09 00:04:51 +0000 | [diff] [blame] | 1 | #!/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 | |
| 8 | Usage: |
| 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 | |
| 18 | import argparse |
| 19 | import json |
| 20 | import urlparse |
| 21 | import os |
| 22 | import sys |
| 23 | |
| 24 | from third_party import httplib2 |
| 25 | |
| 26 | import auth |
| 27 | |
| 28 | |
| 29 | BUILDBUCKET_URL = 'https://cr-buildbucket.appspot.com' |
| 30 | PUT_BUILD_URL = urlparse.urljoin( |
| 31 | BUILDBUCKET_URL, |
| 32 | '_ah/api/buildbucket/v1/builds', |
| 33 | ) |
| 34 | |
| 35 | |
| 36 | def 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.com | 1da6b03 | 2015-06-09 00:16:03 +0000 | [diff] [blame] | 46 | '-b', |
smut@google.com | 5f0788b | 2015-06-09 00:04:51 +0000 | [diff] [blame] | 47 | '--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( |
smut@google.com | c1ae89e | 2015-06-22 23:06:44 +0000 | [diff] [blame^] | 55 | '-c', |
| 56 | '--changes', |
| 57 | help='A flie to load a JSON list of changes dicts from.', |
| 58 | ) |
| 59 | put_parser.add_argument( |
smut@google.com | 5f0788b | 2015-06-09 00:04:51 +0000 | [diff] [blame] | 60 | '-n', |
| 61 | '--builder-name', |
| 62 | help='The builder to schedule the build on.', |
| 63 | required=True, |
| 64 | ) |
| 65 | put_parser.add_argument( |
| 66 | '-p', |
| 67 | '--properties', |
| 68 | help='A file to load a JSON dict of properties from.', |
| 69 | ) |
| 70 | args = parser.parse_args() |
| 71 | # TODO(smut): When more commands are implemented, refactor this. |
| 72 | assert args.command == 'put' |
| 73 | |
smut@google.com | c1ae89e | 2015-06-22 23:06:44 +0000 | [diff] [blame^] | 74 | changes = [] |
| 75 | if args.changes: |
| 76 | try: |
| 77 | with open(args.changes) as fp: |
| 78 | changes.extend(json.load(fp)) |
| 79 | except (TypeError, ValueError): |
| 80 | sys.stderr.write('%s contained invalid JSON list.\n' % args.changes) |
| 81 | raise |
| 82 | |
smut@google.com | 5f0788b | 2015-06-09 00:04:51 +0000 | [diff] [blame] | 83 | properties = {} |
| 84 | if args.properties: |
| 85 | try: |
| 86 | with open(args.properties) as fp: |
| 87 | properties.update(json.load(fp)) |
| 88 | except (TypeError, ValueError): |
| 89 | sys.stderr.write('%s contained invalid JSON dict.\n' % args.properties) |
| 90 | raise |
| 91 | |
| 92 | authenticator = auth.get_authenticator_for_host( |
| 93 | BUILDBUCKET_URL, |
| 94 | auth.make_auth_config(use_oauth2=True), |
| 95 | ) |
| 96 | http = authenticator.authorize(httplib2.Http()) |
| 97 | http.force_exception_to_status_code = True |
| 98 | response, content = http.request( |
| 99 | PUT_BUILD_URL, |
| 100 | 'PUT', |
| 101 | body=json.dumps({ |
| 102 | 'bucket': args.bucket, |
| 103 | 'parameters_json': json.dumps({ |
| 104 | 'builder_name': args.builder_name, |
smut@google.com | c1ae89e | 2015-06-22 23:06:44 +0000 | [diff] [blame^] | 105 | 'changes': changes, |
smut@google.com | 5f0788b | 2015-06-09 00:04:51 +0000 | [diff] [blame] | 106 | 'properties': properties, |
| 107 | }), |
| 108 | }), |
| 109 | headers={'Content-Type': 'application/json'}, |
| 110 | ) |
| 111 | |
| 112 | if args.verbose: |
| 113 | print content |
| 114 | |
| 115 | return response.status != 200 |
| 116 | |
| 117 | |
| 118 | if __name__ == '__main__': |
| 119 | sys.exit(main(sys.argv)) |