OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 # | 5 # |
6 # This utility allows for easy update to chromium os tree status, with proper | 6 # This utility allows for easy update to chromium os tree status, with proper |
7 # password protected authorization. | 7 # password protected authorization. |
8 # | 8 # |
9 # Example usage: | 9 # Example usage: |
10 # ./set_tree_status.py "a quoted space separated message." | 10 # ./set_tree_status.py [options] "a quoted space separated message." |
11 | 11 |
12 import getpass | 12 import getpass |
13 import optparse | |
13 import os | 14 import os |
14 import sys | 15 import sys |
15 import urllib | 16 import urllib |
16 | 17 |
17 CHROMEOS_STATUS_SERVER = 'https://chromiumos-status.appspot.com' | 18 CHROMEOS_STATUS_SERVER = 'https://chromiumos-status.appspot.com' |
18 | 19 |
19 | 20 |
20 def get_status(): | 21 def get_status(): |
21 response = urllib.urlopen(CHROMEOS_STATUS_SERVER + '/current?format=raw') | 22 response = urllib.urlopen(CHROMEOS_STATUS_SERVER + '/current?format=raw') |
22 return response.read() | 23 return response.read() |
23 | 24 |
24 | 25 |
25 def get_pwd(): | 26 def get_pwd(): |
26 password_file = os.path.join('/home', getpass.getuser(), | 27 password_file = os.path.join('/home', getpass.getuser(), |
27 '.status_password.txt') | 28 '.status_password.txt') |
28 if os.path.isfile(password_file): | 29 if os.path.isfile(password_file): |
29 return open(password_file, 'r').read().strip() | 30 return open(password_file, 'r').read().strip() |
30 return getpass.getpass() | 31 return getpass.getpass() |
31 | 32 |
32 | 33 |
33 def post_status(message): | 34 def post_status(force, message): |
34 status = get_status() | 35 if not force: |
35 if 'tree is closed' in status.lower(): | 36 status = get_status() |
36 print >> sys.stderr, 'Tree is already closed for some other reason.' | 37 if 'tree is closed' in status.lower(): |
37 print >> sys.stderr, status | 38 print >> sys.stderr, 'Tree is already closed for some other reason.' |
38 return -1 | 39 print >> sys.stderr, status |
40 return -1 | |
39 data = { | 41 data = { |
40 'message': message, | 42 'message': message, |
41 'username': getpass.getuser(), | 43 'username': getpass.getuser(), |
42 'password': get_pwd(), | 44 'password': get_pwd(), |
43 } | 45 } |
44 urllib.urlopen(CHROMEOS_STATUS_SERVER + '/status', urllib.urlencode(data)) | 46 try: |
Dale Curtis
2011/04/07 18:01:33
No need to catch anything here really, the excepti
ericli
2011/04/07 18:08:28
Done.
| |
47 urllib.urlopen(CHROMEOS_STATUS_SERVER + '/status', | |
48 urllib.urlencode(data)) | |
49 except IOError, ioex: | |
50 if ioex.errno == 'socket error': | |
51 logging.warn('Remote server is not available') | |
52 return -1 | |
53 else: | |
54 raise | |
45 return 0 | 55 return 0 |
46 | 56 |
47 | 57 |
48 if __name__ == '__main__': | 58 if __name__ == '__main__': |
49 if len(sys.argv) != 2: | 59 parser = optparse.OptionParser("%prog [options] quoted_messages") |
Dale Curtis
2011/04/07 18:01:33
quoted_message
ericli
2011/04/07 18:08:28
Done.
| |
50 print('Usage: set_tree_status.py "message"') | 60 parser.add_option('--noforce', |
51 sys.exit(1) | 61 dest='force', action='store_false', |
52 | 62 default=True, |
53 sys.exit(post_status(sys.argv[1])) | 63 help='Force closing tree action.') |
Dale Curtis
2011/04/07 18:01:33
Test should be 'Don't close the tree if it's alrea
ericli
2011/04/07 18:08:28
Done.
| |
64 options, args = parser.parse_args() | |
65 if not args: | |
66 print >> sys.stderr, 'missing tree close message.' | |
67 sys.exit(post_status(options.force, args[0])) | |
OLD | NEW |