~charm-toolers/charm-tools/1.7

« back to all changes in this revision

Viewing changes to scripts/review

  • Committer: Marco Ceppi
  • Date: 2013-08-27 18:18:35 UTC
  • Revision ID: marco@ceppi.net-20130827181835-pm0g0gnd6axmmxyc
Re-organizing charm-tools to be juju-plugins

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
#
3
 
#    review - adds review comment to charm bug
4
 
#
5
 
#    Copyright (C) 2011  Canonical Ltd.
6
 
#    Author: Mark Mims <mark.mims@canonical.com>
7
 
#
8
 
#    This program is free software: you can redistribute it and/or modify
9
 
#    it under the terms of the GNU General Public License as published by
10
 
#    the Free Software Foundation, either version 3 of the License, or
11
 
#    (at your option) any later version.
12
 
#
13
 
#    This program is distributed in the hope that it will be useful,
14
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 
#    GNU General Public License for more details.
17
 
#
18
 
#    You should have received a copy of the GNU General Public License
19
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 
#
21
 
 
22
 
import argparse
23
 
import logging
24
 
import sys
25
 
 
26
 
from launchpadlib.launchpad import Launchpad
27
 
 
28
 
 
29
 
def parse_options():
30
 
    parser = argparse.ArgumentParser(
31
 
        description="Review a charm by adding a comment to the corresponding"
32
 
        " charm bug.  The review comment can be passed as a --message"
33
 
        " arg or via stdin")
34
 
 
35
 
    parser.add_argument('bug_id',
36
 
                        help='The launchpad bug for the charm being reviewed.')
37
 
 
38
 
    parser.add_argument(
39
 
        '--message', '-m',
40
 
        help='The review text to add as a comment to the bug.')
41
 
 
42
 
    parser.add_argument(
43
 
        '--yes', '-y', dest="skip_prompt", default=False, action='store_true',
44
 
        help='do not prompt me... just do it.')
45
 
 
46
 
    parser.add_argument(
47
 
        '--verbose', '-v', default=False, action='store_true',
48
 
        help='show debug logging?')
49
 
 
50
 
    parser.add_argument(
51
 
        '--lp-instance', '-t', dest='lp_instance', default='production',
52
 
        help="The Launchpad instance to use. Defaults to production, but "
53
 
        "staging' or 'qastaging' might be used for testing.")
54
 
 
55
 
    return parser.parse_args()
56
 
 
57
 
 
58
 
def log_level(verbose):
59
 
    if verbose:
60
 
        return logging.DEBUG
61
 
    else:
62
 
        return logging.WARNING
63
 
 
64
 
 
65
 
def get_message_from_stdin():
66
 
    stream = sys.stdin
67
 
    text = stream.read()
68
 
    sys.stdin = open('/dev/tty')  # 'reset' stdin to prompt if necessary
69
 
    return text
70
 
 
71
 
 
72
 
def get_message(message):
73
 
    if message:
74
 
        return message
75
 
    else:
76
 
        return get_message_from_stdin()
77
 
 
78
 
 
79
 
def prompt_to_continue(bug_id):
80
 
    logging.debug("prompting")
81
 
    ans = raw_input("Really add this comment to launchpad bug #%s? y/[n] "
82
 
                    % bug_id)
83
 
    return ans.strip().lower().startswith('y')
84
 
 
85
 
 
86
 
def main(argv):
87
 
    args = parse_options()
88
 
    logging.basicConfig(level=log_level(args.verbose),
89
 
                        format='%(levelname)s:%(message)s')
90
 
 
91
 
    review_message = get_message(args.message)  # before connecting to lp
92
 
 
93
 
    logging.debug('login with %s launchpad:', args.lp_instance)
94
 
    lp = Launchpad.login_with('charm-pilot', args.lp_instance)
95
 
 
96
 
    bug_id = args.bug_id
97
 
    logging.debug('find bug %s:', bug_id)
98
 
    bug = lp.bugs[bug_id]
99
 
 
100
 
    if bug:
101
 
        logging.debug('found bug')
102
 
 
103
 
        if args.skip_prompt or prompt_to_continue(bug_id):
104
 
            logging.debug('adding comment')
105
 
            # TODO check return or catch exception
106
 
            bug.newMessage(content=review_message)
107
 
        else:
108
 
            logging.debug('not adding comment')
109
 
 
110
 
    else:
111
 
        logging.error("no bug: %s", bug_id)
112
 
        return 1
113
 
 
114
 
    return 0
115
 
 
116
 
 
117
 
if __name__ == '__main__':
118
 
    sys.exit(main(sys.argv))