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( |
| 46 | '--bucket', |
| 47 | help=( |
| 48 | 'The bucket to schedule the build on. Typically the master name, e.g.' |
| 49 | ' master.tryserver.chromium.linux.' |
| 50 | ), |
| 51 | required=True, |
| 52 | ) |
| 53 | put_parser.add_argument( |
| 54 | '-n', |
| 55 | '--builder-name', |
| 56 | help='The builder to schedule the build on.', |
| 57 | required=True, |
| 58 | ) |
| 59 | put_parser.add_argument( |
| 60 | '-p', |
| 61 | '--properties', |
| 62 | help='A file to load a JSON dict of properties from.', |
| 63 | ) |
| 64 | args = parser.parse_args() |
| 65 | # TODO(smut): When more commands are implemented, refactor this. |
| 66 | assert args.command == 'put' |
| 67 | |
| 68 | properties = {} |
| 69 | if args.properties: |
| 70 | try: |
| 71 | with open(args.properties) as fp: |
| 72 | properties.update(json.load(fp)) |
| 73 | except (TypeError, ValueError): |
| 74 | sys.stderr.write('%s contained invalid JSON dict.\n' % args.properties) |
| 75 | raise |
| 76 | |
| 77 | authenticator = auth.get_authenticator_for_host( |
| 78 | BUILDBUCKET_URL, |
| 79 | auth.make_auth_config(use_oauth2=True), |
| 80 | ) |
| 81 | http = authenticator.authorize(httplib2.Http()) |
| 82 | http.force_exception_to_status_code = True |
| 83 | response, content = http.request( |
| 84 | PUT_BUILD_URL, |
| 85 | 'PUT', |
| 86 | body=json.dumps({ |
| 87 | 'bucket': args.bucket, |
| 88 | 'parameters_json': json.dumps({ |
| 89 | 'builder_name': args.builder_name, |
| 90 | 'properties': properties, |
| 91 | }), |
| 92 | }), |
| 93 | headers={'Content-Type': 'application/json'}, |
| 94 | ) |
| 95 | |
| 96 | if args.verbose: |
| 97 | print content |
| 98 | |
| 99 | return response.status != 200 |
| 100 | |
| 101 | |
| 102 | if __name__ == '__main__': |
| 103 | sys.exit(main(sys.argv)) |