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