1#!/usr/bin/env python
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
4# found in the LICENSE file.
5#
6# This utility allows for easy update to chromium os tree status, with proper
7# password protected authorization.
8#
9# Example usage:
10# ./set_tree_status.py [options] "a quoted space separated message."
11
12import getpass
13import optparse
14import os
15import sys
16import urllib
17
18CHROMEOS_STATUS_SERVER = 'https://chromiumos-status.appspot.com'
19
20
21def get_status():
22    response = urllib.urlopen(CHROMEOS_STATUS_SERVER + '/current?format=raw')
23    return response.read()
24
25
26def get_pwd():
27    password_file = os.path.join('/home', getpass.getuser(),
28                                 '.status_password.txt')
29    if os.path.isfile(password_file):
30        return open(password_file, 'r').read().strip()
31    return getpass.getpass()
32
33
34def post_status(force, message):
35    if not force:
36        status = get_status()
37        if 'tree is closed' in status.lower():
38            print >> sys.stderr, 'Tree is already closed for some other reason.'
39            print >> sys.stderr, status
40            return -1
41    data = {
42        'message': message,
43        'username': getpass.getuser(),
44        'password': get_pwd(),
45    }
46    urllib.urlopen(CHROMEOS_STATUS_SERVER + '/status', urllib.urlencode(data))
47    return 0
48
49
50if __name__ == '__main__':
51    parser = optparse.OptionParser("%prog [options] quoted_message")
52    parser.add_option('--noforce',
53                      dest='force', action='store_false',
54                      default=True,
55                      help='Dont force to close tree if it is already closed.')
56    options, args = parser.parse_args()
57    if not args:
58        print >> sys.stderr, 'missing tree close message.'
59    sys.exit(post_status(options.force, args[0]))
60