~ack/landscape-client/sources.list-preserve-old-permissions

« back to all changes in this revision

Viewing changes to landscape/textmessage.py

  • Committer: Christopher Armstrong
  • Date: 2008-06-10 10:56:01 UTC
  • Revision ID: radix@twistedmatrix.com-20080610105601-l9qfvqjf88e7j8b6
Import landscape-client into public branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Support code for the C{landscape-message} utility, which sends a text
 
3
message to the Landscape web UI via the landscape-client's dbus
 
4
messaging service (see L{landscape.plugins.dbus_message}).
 
5
"""
 
6
 
 
7
import sys
 
8
from optparse import OptionParser
 
9
 
 
10
from twisted.python import log
 
11
from twisted.python.failure import Failure
 
12
from twisted.internet.defer import fail
 
13
 
 
14
from landscape.lib.dbus_util import get_bus
 
15
 
 
16
from landscape import VERSION
 
17
from landscape.broker.broker import BUS_NAME
 
18
from landscape.broker.remote import (
 
19
    RemoteBroker, SecurityError, ServiceUnknownError, NoReplyError)
 
20
 
 
21
 
 
22
class AcceptedTypeError(Exception):
 
23
    """
 
24
    Raised when a message is sent without 'text-message' being an
 
25
    accepted type.
 
26
    """
 
27
 
 
28
 
 
29
class EmptyMessageError(Exception):
 
30
    """Raised when an empty message is provied."""
 
31
 
 
32
 
 
33
def send_message(text, broker):
 
34
    """Add a message to the queue via a remote broker.
 
35
 
 
36
    The message is of type C{text-message}.
 
37
 
 
38
    @param broker: The L{landscape.broker.remote.RemoteBroker}
 
39
        object to use to send the message.
 
40
    @return: A L{Deferred} which will fire with the result of the send.
 
41
    @raise ServiceUnknownError: (Deferred) if an
 
42
        org.freedesktop.DBus.Error.ServiceUnknown is raised from DBUS.
 
43
    @raise SecurityError: (Deferred) if a security policy prevents
 
44
        sending the message.
 
45
    """
 
46
    message = {"type": "text-message", "message": text}
 
47
    response = broker.send_message(message, True)
 
48
    return response
 
49
 
 
50
 
 
51
def got_result(result):
 
52
    print u"Message sent."
 
53
 
 
54
 
 
55
def got_error(failure):
 
56
    """
 
57
    An error occurred. Attempt to write a meaningful message if it's
 
58
    an error we know about, otherwise just print the exception value.
 
59
    """
 
60
    print u"Failure sending message."
 
61
    if failure.check(ServiceUnknownError):
 
62
        print "Couldn't find the %s service." % BUS_NAME
 
63
        print "Is the Landscape Client running?"
 
64
    elif failure.check(SecurityError, NoReplyError):
 
65
        print "Couldn't access the %s service." % BUS_NAME
 
66
        print "You may need to run landscape-message as root."
 
67
    elif failure.check(AcceptedTypeError):
 
68
        print ("Server not accepting text messages.  "
 
69
               "Is Landscape Client registered with the server?")
 
70
    else:
 
71
        print "Unknown error:", failure.type, failure.getErrorMessage()
 
72
 
 
73
 
 
74
def get_message(args):
 
75
    if len(args) < 2:
 
76
        print ("Please enter your message, and send EOF (Control + D after "
 
77
               "newline) when done.")
 
78
        message = sys.stdin.read().decode(sys.stdin.encoding)
 
79
    else:
 
80
        message = u" ".join([x.decode(sys.stdin.encoding) for x in args[1:]])
 
81
    if not message:
 
82
        raise EmptyMessageError("Text messages may not be empty.")
 
83
    return message
 
84
 
 
85
 
 
86
def got_accepted_types(accepted_types, broker, args):
 
87
    from twisted.internet import reactor
 
88
 
 
89
    if not "text-message" in accepted_types:
 
90
        raise AcceptedTypeError("Text messages may not be created.  Is "
 
91
                                "Landscape Client registered with the server?")
 
92
    message = get_message(args)
 
93
    d = send_message(message, broker)
 
94
    d.addCallback(got_result)
 
95
    return d
 
96
 
 
97
 
 
98
def run(args=sys.argv):
 
99
    """Send a message to Landscape.
 
100
 
 
101
    This function runs a Twisted reactor, prints various status
 
102
    messages, and exits the process.
 
103
    """
 
104
    import dbus.glib
 
105
    from twisted.internet.glib2reactor import install
 
106
    install()
 
107
    from twisted.internet import reactor
 
108
 
 
109
    parser = OptionParser(version=VERSION)
 
110
    parser.add_option("-b", "--bus", default="system",
 
111
                      help="The DBUS bus to use to send the message.")
 
112
    options, args = parser.parse_args(args)
 
113
 
 
114
    try:
 
115
        broker = RemoteBroker(get_bus(options.bus))
 
116
    except:
 
117
        got_error(Failure())
 
118
        return
 
119
 
 
120
    result = broker.get_accepted_message_types()
 
121
    result.addCallback(got_accepted_types, broker, args)
 
122
    result.addErrback(got_error)
 
123
    result.addBoth(lambda x: reactor.callWhenRunning(reactor.stop))
 
124
    reactor.run()