1#!/usr/bin/env python2 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 12from __future__ import absolute_import 13from __future__ import division 14from __future__ import print_function 15import getpass 16import optparse 17import os 18import sys 19from six.moves import urllib 20 21CHROMEOS_STATUS_SERVER = 'https://chromiumos-status.appspot.com' 22 23 24def get_status(): 25 response = urllib.request.urlopen( 26 CHROMEOS_STATUS_SERVER + '/current?format=raw') 27 return response.read() 28 29 30def get_pwd(): 31 password_file = os.path.join('/home', getpass.getuser(), 32 '.status_password.txt') 33 if os.path.isfile(password_file): 34 return open(password_file, 'r').read().strip() 35 return getpass.getpass() 36 37 38def post_status(force, message): 39 if not force: 40 status = get_status() 41 if 'tree is closed' in status.lower(): 42 print('Tree is already closed for some other reason.', 43 file=sys.stderr) 44 print(status, file=sys.stderr) 45 return -1 46 data = { 47 'message': message, 48 'username': getpass.getuser(), 49 'password': get_pwd(), 50 } 51 urllib.request.urlopen(CHROMEOS_STATUS_SERVER + '/status', 52 urllib.parse.urlencode(data)) 53 return 0 54 55 56if __name__ == '__main__': 57 parser = optparse.OptionParser("%prog [options] quoted_message") 58 parser.add_option('--noforce', 59 dest='force', action='store_false', 60 default=True, 61 help='Dont force to close tree if it is already closed.') 62 options, args = parser.parse_args() 63 if not args: 64 print('missing tree close message.', file=sys.stderr) 65 sys.exit(post_status(options.force, args[0])) 66