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