2007-04-20 22:25:37 +02:00
|
|
|
"""An "actor" on the network, whether remote or local"""
|
2007-02-22 20:11:31 +01:00
|
|
|
# Copyright (C) 2007, Red Hat, Inc.
|
|
|
|
# Copyright (C) 2007, Collabora Ltd.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
import os
|
2007-02-22 20:11:31 +01:00
|
|
|
import gobject
|
2007-05-11 23:27:36 +02:00
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
from dbus.gobject_service import ExportedGObject
|
2007-04-12 04:49:14 +02:00
|
|
|
from ConfigParser import ConfigParser, NoOptionError
|
2007-05-14 03:34:15 +02:00
|
|
|
import psutils
|
2007-03-09 22:28:35 +01:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
from sugar import env, profile, util
|
|
|
|
import logging
|
|
|
|
import random
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
_BUDDY_PATH = "/org/laptop/Sugar/Presence/Buddies/"
|
|
|
|
_BUDDY_INTERFACE = "org.laptop.Sugar.Presence.Buddy"
|
|
|
|
_OWNER_INTERFACE = "org.laptop.Sugar.Presence.Buddy.Owner"
|
|
|
|
|
|
|
|
class NotFoundError(dbus.DBusException):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Raised when a given actor is not found on the network"""
|
2007-02-22 20:11:31 +01:00
|
|
|
def __init__(self):
|
|
|
|
dbus.DBusException.__init__(self)
|
|
|
|
self._dbus_error_name = _PRESENCE_INTERFACE + '.NotFound'
|
|
|
|
|
2007-04-20 18:34:16 +02:00
|
|
|
_PROP_NICK = "nick"
|
|
|
|
_PROP_KEY = "key"
|
|
|
|
_PROP_ICON = "icon"
|
|
|
|
_PROP_CURACT = "current-activity"
|
|
|
|
_PROP_COLOR = "color"
|
|
|
|
_PROP_OWNER = "owner"
|
|
|
|
_PROP_VALID = "valid"
|
|
|
|
|
2007-05-01 05:44:39 +02:00
|
|
|
# Will go away soon
|
|
|
|
_PROP_IP4_ADDRESS = "ip4-address"
|
|
|
|
|
2007-05-11 23:27:36 +02:00
|
|
|
class Buddy(ExportedGObject):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Person on the network (tracks properties and shared activites)
|
|
|
|
|
|
|
|
The Buddy is a collection of metadata describing a particular
|
|
|
|
actor/person on the network. The Buddy object tracks a set of
|
|
|
|
activities which the actor has shared with the presence service.
|
|
|
|
|
|
|
|
Buddies have a "valid" property which is used to flag Buddies
|
|
|
|
which are no longer reachable. That is, a Buddy may represent
|
|
|
|
a no-longer reachable target on the network.
|
|
|
|
|
|
|
|
The Buddy emits GObject events that the PresenceService uses
|
|
|
|
to track changes in its status.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
|
|
|
|
_activities -- dictionary mapping activity ID to
|
|
|
|
activity.Activity objects
|
|
|
|
handles -- dictionary mapping telepresence client to
|
|
|
|
"handle" (XXX what's that)
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
__gsignals__ = {
|
|
|
|
'validity-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
2007-03-09 22:28:35 +01:00
|
|
|
([gobject.TYPE_BOOLEAN])),
|
|
|
|
'property-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT])),
|
|
|
|
'icon-changed': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE,
|
|
|
|
([gobject.TYPE_PYOBJECT]))
|
2007-03-02 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
__gproperties__ = {
|
2007-04-20 18:34:16 +02:00
|
|
|
_PROP_KEY : (str, None, None, None,
|
2007-03-02 17:10:44 +01:00
|
|
|
gobject.PARAM_READWRITE | gobject.PARAM_CONSTRUCT_ONLY),
|
2007-04-20 18:34:16 +02:00
|
|
|
_PROP_ICON : (object, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_NICK : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_COLOR : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_CURACT : (str, None, None, None, gobject.PARAM_READWRITE),
|
|
|
|
_PROP_VALID : (bool, None, None, False, gobject.PARAM_READABLE),
|
2007-05-01 05:44:39 +02:00
|
|
|
_PROP_OWNER : (bool, None, None, False, gobject.PARAM_READABLE),
|
|
|
|
_PROP_IP4_ADDRESS : (str, None, None, None, gobject.PARAM_READWRITE)
|
2007-03-02 17:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, bus_name, object_id, **kwargs):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Initialize the Buddy object
|
|
|
|
|
|
|
|
bus_name -- DBUS object bus name (identifier)
|
|
|
|
object_id -- the activity's unique identifier
|
|
|
|
kwargs -- used to initialize the object's properties
|
|
|
|
|
|
|
|
constructs a DBUS "object path" from the _BUDDY_PATH
|
|
|
|
and object_id
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
if not bus_name:
|
|
|
|
raise ValueError("DBus bus name must be valid")
|
|
|
|
if not object_id or not isinstance(object_id, int):
|
|
|
|
raise ValueError("object id must be a valid number")
|
|
|
|
|
|
|
|
self._bus_name = bus_name
|
|
|
|
self._object_id = object_id
|
|
|
|
self._object_path = _BUDDY_PATH + str(self._object_id)
|
|
|
|
|
|
|
|
self._activities = {} # Activity ID -> Activity
|
2007-04-22 01:09:59 +02:00
|
|
|
self._activity_sigids = {}
|
2007-02-26 12:28:50 +01:00
|
|
|
self.handles = {} # tp client -> handle
|
2007-02-26 01:24:48 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
self._valid = False
|
|
|
|
self._owner = False
|
|
|
|
self._key = None
|
2007-03-06 17:26:21 +01:00
|
|
|
self._icon = ''
|
2007-03-12 16:31:34 +01:00
|
|
|
self._current_activity = None
|
|
|
|
self._nick = None
|
|
|
|
self._color = None
|
2007-05-01 05:44:39 +02:00
|
|
|
self._ip4_address = None
|
2007-03-02 17:10:44 +01:00
|
|
|
|
2007-04-20 18:40:53 +02:00
|
|
|
if not kwargs.get(_PROP_KEY):
|
2007-03-08 18:51:10 +01:00
|
|
|
raise ValueError("key required")
|
|
|
|
|
2007-05-01 05:44:39 +02:00
|
|
|
_ALLOWED_INIT_PROPS = [_PROP_NICK, _PROP_KEY, _PROP_ICON, _PROP_CURACT, _PROP_COLOR, _PROP_IP4_ADDRESS]
|
2007-04-20 18:34:16 +02:00
|
|
|
for (key, value) in kwargs.items():
|
|
|
|
if key not in _ALLOWED_INIT_PROPS:
|
|
|
|
logging.debug("Invalid init property '%s'; ignoring..." % key)
|
|
|
|
del kwargs[key]
|
2007-05-11 23:27:36 +02:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
# Set icon after superclass init, because it sends DBus and GObject
|
|
|
|
# signals when set
|
|
|
|
icon_data = None
|
|
|
|
if kwargs.has_key(_PROP_ICON):
|
|
|
|
icon_data = kwargs[_PROP_ICON]
|
|
|
|
del kwargs[_PROP_ICON]
|
|
|
|
|
2007-05-11 23:27:36 +02:00
|
|
|
ExportedGObject.__init__(self, bus_name, self._object_path,
|
|
|
|
gobject_properties=kwargs)
|
2007-03-02 17:10:44 +01:00
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
if icon_data:
|
|
|
|
self.props.icon = icon_data
|
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
def do_get_property(self, pspec):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve current value for the given property specifier
|
|
|
|
|
|
|
|
pspec -- property specifier with a "name" attribute
|
|
|
|
"""
|
2007-04-20 18:34:16 +02:00
|
|
|
if pspec.name == _PROP_KEY:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._key
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_ICON:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._icon
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_NICK:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._nick
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_COLOR:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._color
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_CURACT:
|
2007-03-02 17:10:44 +01:00
|
|
|
if not self._current_activity:
|
|
|
|
return None
|
|
|
|
if not self._activities.has_key(self._current_activity):
|
|
|
|
return None
|
2007-03-16 17:18:54 +01:00
|
|
|
return self._current_activity
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_VALID:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._valid
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_OWNER:
|
2007-03-02 17:10:44 +01:00
|
|
|
return self._owner
|
2007-05-01 05:44:39 +02:00
|
|
|
elif pspec.name == _PROP_IP4_ADDRESS:
|
|
|
|
return self._ip4_address
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
def do_set_property(self, pspec, value):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Set given property
|
|
|
|
|
|
|
|
pspec -- property specifier with a "name" attribute
|
|
|
|
value -- value to set
|
|
|
|
|
|
|
|
emits 'icon-changed' signal on icon setting
|
|
|
|
calls _update_validity on all calls
|
|
|
|
"""
|
2007-04-20 18:34:16 +02:00
|
|
|
if pspec.name == _PROP_ICON:
|
2007-03-09 22:28:35 +01:00
|
|
|
if str(value) != self._icon:
|
|
|
|
self._icon = str(value)
|
|
|
|
self.IconChanged(self._icon)
|
|
|
|
self.emit('icon-changed', self._icon)
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_NICK:
|
2007-03-02 17:10:44 +01:00
|
|
|
self._nick = value
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_COLOR:
|
2007-03-02 17:10:44 +01:00
|
|
|
self._color = value
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_CURACT:
|
2007-03-02 17:10:44 +01:00
|
|
|
self._current_activity = value
|
2007-04-20 18:34:16 +02:00
|
|
|
elif pspec.name == _PROP_KEY:
|
2007-05-01 05:44:39 +02:00
|
|
|
if self._key:
|
|
|
|
raise RuntimeError("Key already set.")
|
2007-03-02 17:10:44 +01:00
|
|
|
self._key = value
|
2007-05-01 05:44:39 +02:00
|
|
|
elif pspec.name == _PROP_IP4_ADDRESS:
|
|
|
|
self._ip4_address = value
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
self._update_validity()
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
# dbus signals
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="ay")
|
|
|
|
def IconChanged(self, icon_data):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal with icon_data"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="o")
|
|
|
|
def JoinedActivity(self, activity_path):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal when buddy joins activity
|
|
|
|
|
|
|
|
activity_path -- DBUS path to the activity object
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="o")
|
|
|
|
def LeftActivity(self, activity_path):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal when buddy leaves activity
|
|
|
|
|
|
|
|
activity_path -- DBUS path to the activity object
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.signal(_BUDDY_INTERFACE,
|
|
|
|
signature="a{sv}")
|
|
|
|
def PropertyChanged(self, updated):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Generates DBUS signal when buddy's property changes
|
|
|
|
|
|
|
|
updated -- updated property-set (dictionary) with the
|
|
|
|
Buddy's property (changed) values. Note: not the
|
|
|
|
full set of properties, just the changes.
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
# dbus methods
|
|
|
|
@dbus.service.method(_BUDDY_INTERFACE,
|
|
|
|
in_signature="", out_signature="ay")
|
|
|
|
def GetIcon(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve Buddy's icon data
|
|
|
|
|
|
|
|
returns empty string or dbus.ByteArray
|
|
|
|
"""
|
2007-03-02 17:10:44 +01:00
|
|
|
if not self.props.icon:
|
2007-02-22 20:11:31 +01:00
|
|
|
return ""
|
2007-03-03 09:12:27 +01:00
|
|
|
return dbus.ByteArray(self.props.icon)
|
2007-02-22 20:11:31 +01:00
|
|
|
|
|
|
|
@dbus.service.method(_BUDDY_INTERFACE,
|
|
|
|
in_signature="", out_signature="ao")
|
|
|
|
def GetJoinedActivities(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve set of Buddy's joined activities (paths)
|
|
|
|
|
|
|
|
returns list of dbus service paths for the Buddy's joined
|
|
|
|
activities
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
acts = []
|
|
|
|
for act in self.get_joined_activities():
|
2007-04-22 01:20:06 +02:00
|
|
|
if act.props.valid:
|
|
|
|
acts.append(act.object_path())
|
2007-02-22 20:11:31 +01:00
|
|
|
return acts
|
|
|
|
|
|
|
|
@dbus.service.method(_BUDDY_INTERFACE,
|
|
|
|
in_signature="", out_signature="a{sv}")
|
|
|
|
def GetProperties(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve set of Buddy's properties
|
|
|
|
|
|
|
|
returns dictionary of
|
|
|
|
nick : str(nickname)
|
|
|
|
owner : bool( whether this Buddy is an owner??? )
|
|
|
|
XXX what is the owner flag for?
|
|
|
|
key : str(public-key)
|
|
|
|
color: Buddy's icon colour
|
|
|
|
XXX what type?
|
|
|
|
current-activity: Buddy's current activity_id, or
|
|
|
|
"" if no current activity
|
|
|
|
"""
|
2007-02-22 20:11:31 +01:00
|
|
|
props = {}
|
2007-04-20 18:34:16 +02:00
|
|
|
props[_PROP_NICK] = self.props.nick
|
|
|
|
props[_PROP_OWNER] = self.props.owner
|
|
|
|
props[_PROP_KEY] = self.props.key
|
|
|
|
props[_PROP_COLOR] = self.props.color
|
2007-05-01 05:44:39 +02:00
|
|
|
|
|
|
|
if self.props.ip4_address:
|
|
|
|
props[_PROP_IP4_ADDRESS] = self.props.ip4_address
|
|
|
|
else:
|
|
|
|
props[_PROP_IP4_ADDRESS] = ""
|
|
|
|
|
2007-04-10 16:59:35 +02:00
|
|
|
if self.props.current_activity:
|
2007-04-20 18:34:16 +02:00
|
|
|
props[_PROP_CURACT] = self.props.current_activity
|
2007-04-10 16:59:35 +02:00
|
|
|
else:
|
2007-04-20 18:34:16 +02:00
|
|
|
props[_PROP_CURACT] = ""
|
2007-02-22 20:11:31 +01:00
|
|
|
return props
|
|
|
|
|
|
|
|
# methods
|
|
|
|
def object_path(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve our dbus.ObjectPath object"""
|
2007-02-22 20:11:31 +01:00
|
|
|
return dbus.ObjectPath(self._object_path)
|
|
|
|
|
2007-04-22 01:09:59 +02:00
|
|
|
def _activity_validity_changed_cb(self, activity, valid):
|
|
|
|
"""Join or leave the activity when its validity changes"""
|
|
|
|
if valid:
|
|
|
|
self.JoinedActivity(activity.object_path())
|
|
|
|
else:
|
|
|
|
self.LeftActivity(activity.object_path())
|
|
|
|
|
2007-02-22 20:11:31 +01:00
|
|
|
def add_activity(self, activity):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Add an activity to the Buddy's set of activities
|
|
|
|
|
|
|
|
activity -- activity.Activity instance
|
|
|
|
|
|
|
|
calls JoinedActivity
|
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
actid = activity.props.id
|
2007-02-22 20:11:31 +01:00
|
|
|
if self._activities.has_key(actid):
|
|
|
|
return
|
|
|
|
self._activities[actid] = activity
|
2007-04-22 01:09:59 +02:00
|
|
|
# join/leave activity when it's validity changes
|
|
|
|
sigid = activity.connect("validity-changed", self._activity_validity_changed_cb)
|
|
|
|
self._activity_sigids[actid] = sigid
|
2007-03-08 18:51:10 +01:00
|
|
|
if activity.props.valid:
|
2007-02-22 20:11:31 +01:00
|
|
|
self.JoinedActivity(activity.object_path())
|
|
|
|
|
|
|
|
def remove_activity(self, activity):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Remove the activity from the Buddy's set of activities
|
|
|
|
|
|
|
|
activity -- activity.Activity instance
|
|
|
|
|
|
|
|
calls LeftActivity
|
|
|
|
"""
|
2007-03-08 18:51:10 +01:00
|
|
|
actid = activity.props.id
|
2007-02-22 20:11:31 +01:00
|
|
|
if not self._activities.has_key(actid):
|
|
|
|
return
|
2007-04-22 01:09:59 +02:00
|
|
|
activity.disconnect(self._activity_sigids[actid])
|
|
|
|
del self._activity_sigids[actid]
|
2007-02-22 20:11:31 +01:00
|
|
|
del self._activities[actid]
|
2007-03-08 18:51:10 +01:00
|
|
|
if activity.props.valid:
|
2007-02-22 20:11:31 +01:00
|
|
|
self.LeftActivity(activity.object_path())
|
|
|
|
|
|
|
|
def get_joined_activities(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieves list of still-valid activity objects"""
|
2007-02-22 20:11:31 +01:00
|
|
|
acts = []
|
|
|
|
for act in self._activities.values():
|
2007-04-22 01:20:06 +02:00
|
|
|
acts.append(act)
|
2007-02-22 20:11:31 +01:00
|
|
|
return acts
|
|
|
|
|
2007-02-28 17:02:43 +01:00
|
|
|
def set_properties(self, properties):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Set the given set of properties on the object
|
|
|
|
|
|
|
|
properties -- set of property values to set
|
|
|
|
|
|
|
|
if no change, no events generated
|
|
|
|
if change, generates property-changed and
|
|
|
|
calls _update_validity
|
|
|
|
"""
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = False
|
2007-04-17 19:27:34 +02:00
|
|
|
changed_props = {}
|
2007-04-20 18:34:16 +02:00
|
|
|
if _PROP_NICK in properties.keys():
|
|
|
|
nick = properties[_PROP_NICK]
|
2007-03-09 22:28:35 +01:00
|
|
|
if nick != self._nick:
|
|
|
|
self._nick = nick
|
2007-04-20 18:34:16 +02:00
|
|
|
changed_props[_PROP_NICK] = nick
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = True
|
2007-04-20 18:34:16 +02:00
|
|
|
if _PROP_COLOR in properties.keys():
|
|
|
|
color = properties[_PROP_COLOR]
|
2007-03-09 22:28:35 +01:00
|
|
|
if color != self._color:
|
|
|
|
self._color = color
|
2007-04-20 18:34:16 +02:00
|
|
|
changed_props[_PROP_COLOR] = color
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = True
|
2007-04-20 18:34:16 +02:00
|
|
|
if _PROP_CURACT in properties.keys():
|
|
|
|
curact = properties[_PROP_CURACT]
|
2007-03-09 22:28:35 +01:00
|
|
|
if curact != self._current_activity:
|
|
|
|
self._current_activity = curact
|
2007-04-20 18:34:16 +02:00
|
|
|
changed_props[_PROP_CURACT] = curact
|
2007-03-09 22:28:35 +01:00
|
|
|
changed = True
|
2007-05-01 05:44:39 +02:00
|
|
|
if _PROP_IP4_ADDRESS in properties.keys():
|
|
|
|
ip4addr = properties[_PROP_IP4_ADDRESS]
|
|
|
|
if ip4addr != self._ip4_address:
|
|
|
|
self._ip4_address = ip4addr
|
|
|
|
changed_props[_PROP_IP4_ADDRESS] = ip4addr
|
|
|
|
changed = True
|
2007-03-09 22:28:35 +01:00
|
|
|
|
2007-04-17 19:27:34 +02:00
|
|
|
if not changed or not len(changed_props.keys()):
|
2007-03-09 22:28:35 +01:00
|
|
|
return
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
# Try emitting PropertyChanged before updating validity
|
|
|
|
# to avoid leaking a PropertyChanged signal before the buddy is
|
|
|
|
# actually valid the first time after creation
|
|
|
|
if self._valid:
|
2007-04-17 19:27:34 +02:00
|
|
|
self.PropertyChanged(changed_props)
|
|
|
|
self.emit('property-changed', changed_props)
|
2007-03-02 17:10:44 +01:00
|
|
|
|
|
|
|
self._update_validity()
|
2007-02-23 14:15:51 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
def _update_validity(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Check whether we are now valid
|
|
|
|
|
|
|
|
validity is True if color, nick and key are non-null
|
|
|
|
|
|
|
|
emits validity-changed if we have changed validity
|
|
|
|
"""
|
2007-03-02 17:10:44 +01:00
|
|
|
try:
|
|
|
|
old_valid = self._valid
|
|
|
|
if self._color and self._nick and self._key:
|
|
|
|
self._valid = True
|
|
|
|
else:
|
|
|
|
self._valid = False
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-02 17:10:44 +01:00
|
|
|
if old_valid != self._valid:
|
|
|
|
self.emit("validity-changed", self._valid)
|
|
|
|
except AttributeError:
|
|
|
|
self._valid = False
|
2007-02-23 17:30:25 +01:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
class GenericOwner(Buddy):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Common functionality for Local User-like objects
|
|
|
|
|
|
|
|
The TestOwner wants to produce something *like* a
|
|
|
|
ShellOwner, but with randomised changes and the like.
|
|
|
|
This class provides the common features for a real
|
|
|
|
local owner and a testing one.
|
|
|
|
"""
|
2007-04-12 04:49:14 +02:00
|
|
|
__gtype_name__ = "GenericOwner"
|
|
|
|
|
|
|
|
__gproperties__ = {
|
|
|
|
'registered' : (bool, None, None, False, gobject.PARAM_READWRITE | gobject.PARAM_CONSTRUCT),
|
|
|
|
'server' : (str, None, None, None, gobject.PARAM_READABLE | gobject.PARAM_CONSTRUCT),
|
|
|
|
'key-hash' : (str, None, None, None, gobject.PARAM_READABLE | gobject.PARAM_CONSTRUCT)
|
|
|
|
}
|
|
|
|
|
2007-04-17 04:44:54 +02:00
|
|
|
def __init__(self, ps, bus_name, object_id, **kwargs):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Initialize the GenericOwner instance
|
|
|
|
|
|
|
|
ps -- presenceservice.PresenceService object
|
|
|
|
bus_name -- DBUS object bus name (identifier)
|
|
|
|
object_id -- the activity's unique identifier
|
|
|
|
kwargs -- used to initialize the object's properties
|
|
|
|
|
|
|
|
calls Buddy.__init__
|
|
|
|
"""
|
2007-04-17 04:44:54 +02:00
|
|
|
self._ps = ps
|
2007-04-12 04:49:14 +02:00
|
|
|
self._server = 'olpc.collabora.co.uk'
|
|
|
|
self._key_hash = None
|
|
|
|
self._registered = False
|
|
|
|
if kwargs.has_key("server"):
|
|
|
|
self._server = kwargs["server"]
|
|
|
|
del kwargs["server"]
|
|
|
|
if kwargs.has_key("key_hash"):
|
|
|
|
self._key_hash = kwargs["key_hash"]
|
|
|
|
del kwargs["key_hash"]
|
|
|
|
if kwargs.has_key("registered"):
|
|
|
|
self._registered = kwargs["registered"]
|
|
|
|
del kwargs["registered"]
|
|
|
|
|
|
|
|
Buddy.__init__(self, bus_name, object_id, **kwargs)
|
|
|
|
self._owner = True
|
|
|
|
|
2007-05-01 05:44:39 +02:00
|
|
|
self._bus = dbus.SessionBus()
|
|
|
|
self._bus.add_signal_receiver(self._name_owner_changed_cb,
|
|
|
|
signal_name="NameOwnerChanged",
|
|
|
|
dbus_interface="org.freedesktop.DBus")
|
|
|
|
|
2007-05-14 03:34:15 +02:00
|
|
|
self._ip4_addr_monitor = psutils.IP4AddressMonitor.get_instance()
|
2007-05-01 05:44:39 +02:00
|
|
|
self._ip4_addr_monitor.connect("address-changed", self._ip4_address_changed_cb)
|
|
|
|
|
|
|
|
def _ip4_address_changed_cb(self, monitor, address):
|
|
|
|
"""Handle IPv4 address change, set property to generate event"""
|
|
|
|
props = {_PROP_IP4_ADDRESS: address}
|
|
|
|
self.set_properties(props)
|
|
|
|
|
|
|
|
def _name_owner_changed_cb(self, name, old, new):
|
|
|
|
"""Handle D-Bus services we care about appearing and disappearing."""
|
|
|
|
self._ip4_addr_monitor.handle_name_owner_changed(name, old, new)
|
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
def get_registered(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve whether owner has registered with presence server"""
|
2007-04-12 04:49:14 +02:00
|
|
|
return self._registered
|
|
|
|
|
|
|
|
def get_server(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve presence server (XXX url??)"""
|
2007-04-12 04:49:14 +02:00
|
|
|
return self._server
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
def get_key_hash(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve the user's private-key hash"""
|
2007-04-12 04:49:14 +02:00
|
|
|
return self._key_hash
|
|
|
|
|
|
|
|
def set_registered(self, registered):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Customisation point: handle the registration of the owner"""
|
2007-04-12 04:49:14 +02:00
|
|
|
raise RuntimeError("Subclasses must implement")
|
|
|
|
|
|
|
|
class ShellOwner(GenericOwner):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Representation of the local-machine owner using Sugar's Shell
|
|
|
|
|
|
|
|
The ShellOwner uses the Sugar Shell's dbus services to
|
|
|
|
register for updates about the user's profile description.
|
|
|
|
"""
|
2007-04-12 04:49:14 +02:00
|
|
|
__gtype_name__ = "ShellOwner"
|
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
_SHELL_SERVICE = "org.laptop.Shell"
|
|
|
|
_SHELL_OWNER_INTERFACE = "org.laptop.Shell.Owner"
|
|
|
|
_SHELL_PATH = "/org/laptop/Shell"
|
|
|
|
|
2007-04-17 04:44:54 +02:00
|
|
|
def __init__(self, ps, bus_name, object_id, test=False):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Initialize the ShellOwner instance
|
|
|
|
|
|
|
|
ps -- presenceservice.PresenceService object
|
|
|
|
bus_name -- DBUS object bus name (identifier)
|
|
|
|
object_id -- the activity's unique identifier
|
|
|
|
test -- ignored
|
|
|
|
|
|
|
|
Retrieves initial property values from the profile
|
|
|
|
module. Loads the buddy icon from file as well.
|
|
|
|
XXX note: no error handling on that
|
|
|
|
|
|
|
|
calls GenericOwner.__init__
|
|
|
|
"""
|
2007-04-12 04:49:14 +02:00
|
|
|
server = profile.get_server()
|
|
|
|
key_hash = profile.get_private_key_hash()
|
|
|
|
registered = profile.get_server_registered()
|
2007-03-01 04:13:27 +01:00
|
|
|
key = profile.get_pubkey()
|
2007-03-02 17:10:44 +01:00
|
|
|
nick = profile.get_nick_name()
|
|
|
|
color = profile.get_color().to_string()
|
2007-02-22 20:11:31 +01:00
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
icon_file = os.path.join(env.get_profile_path(), "buddy-icon.jpg")
|
|
|
|
f = open(icon_file, "r")
|
|
|
|
icon = f.read()
|
|
|
|
f.close()
|
|
|
|
|
2007-04-17 04:44:54 +02:00
|
|
|
GenericOwner.__init__(self, ps, bus_name, object_id, key=key, nick=nick,
|
2007-04-12 04:49:14 +02:00
|
|
|
color=color, icon=icon, server=server, key_hash=key_hash,
|
|
|
|
registered=registered)
|
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
# Connect to the shell to get notifications on Owner object
|
|
|
|
# property changes
|
|
|
|
try:
|
|
|
|
self._connect_to_shell()
|
|
|
|
except dbus.DBusException:
|
|
|
|
pass
|
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
def set_registered(self, value):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle notification that we have been registered"""
|
2007-04-12 04:49:14 +02:00
|
|
|
if value:
|
|
|
|
profile.set_server_registered()
|
2007-03-09 22:28:35 +01:00
|
|
|
|
2007-05-01 05:44:39 +02:00
|
|
|
def _name_owner_changed_cb(self, name, old, new):
|
|
|
|
# chain up to superclass
|
|
|
|
GenericOwner._name_owner_changed_cb(self, name, old, new)
|
|
|
|
|
2007-03-09 22:28:35 +01:00
|
|
|
if name != self._SHELL_SERVICE:
|
|
|
|
return
|
|
|
|
if (old and len(old)) and (not new and not len(new)):
|
|
|
|
# shell went away
|
|
|
|
self._shell_owner = None
|
|
|
|
elif (not old and not len(old)) and (new and len(new)):
|
|
|
|
# shell started
|
|
|
|
self._connect_to_shell()
|
|
|
|
|
|
|
|
def _connect_to_shell(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Connect to the Sugar Shell service to watch for events
|
|
|
|
|
|
|
|
Connects the various XChanged events on the Sugar Shell
|
|
|
|
service to our _x_changed_cb methods.
|
|
|
|
"""
|
2007-03-09 22:28:35 +01:00
|
|
|
obj = self._bus.get_object(self._SHELL_SERVICE, self._SHELL_PATH)
|
|
|
|
self._shell_owner = dbus.Interface(obj, self._SHELL_OWNER_INTERFACE)
|
|
|
|
self._shell_owner.connect_to_signal('IconChanged', self._icon_changed_cb)
|
|
|
|
self._shell_owner.connect_to_signal('ColorChanged', self._color_changed_cb)
|
|
|
|
self._shell_owner.connect_to_signal('NickChanged', self._nick_changed_cb)
|
|
|
|
self._shell_owner.connect_to_signal('CurrentActivityChanged',
|
|
|
|
self._cur_activity_changed_cb)
|
|
|
|
|
|
|
|
def _icon_changed_cb(self, icon):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle icon change, set property to generate event"""
|
2007-03-09 22:28:35 +01:00
|
|
|
self.props.icon = icon
|
|
|
|
|
|
|
|
def _color_changed_cb(self, color):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle color change, set property to generate event"""
|
2007-04-20 18:34:16 +02:00
|
|
|
props = {_PROP_COLOR: color}
|
2007-03-09 22:28:35 +01:00
|
|
|
self.set_properties(props)
|
|
|
|
|
|
|
|
def _nick_changed_cb(self, nick):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle nickname change, set property to generate event"""
|
2007-04-20 18:34:16 +02:00
|
|
|
props = {_PROP_NICK: nick}
|
2007-03-09 22:28:35 +01:00
|
|
|
self.set_properties(props)
|
|
|
|
|
|
|
|
def _cur_activity_changed_cb(self, activity_id):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Handle current-activity change, set property to generate event
|
|
|
|
|
|
|
|
Filters out local activities (those not in self.activites)
|
|
|
|
because the network users can't join those activities, so
|
|
|
|
the activity_id shared will be None in those cases...
|
|
|
|
"""
|
2007-03-09 22:28:35 +01:00
|
|
|
if not self._activities.has_key(activity_id):
|
2007-03-16 17:18:54 +01:00
|
|
|
# This activity is local-only
|
|
|
|
activity_id = None
|
2007-04-20 18:40:53 +02:00
|
|
|
props = {_PROP_CURACT: activity_id}
|
2007-03-09 22:28:35 +01:00
|
|
|
self.set_properties(props)
|
|
|
|
|
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
class TestOwner(GenericOwner):
|
|
|
|
"""Class representing the owner of the machine. This test owner
|
|
|
|
changes random attributes periodically."""
|
|
|
|
|
|
|
|
__gtype_name__ = "TestOwner"
|
|
|
|
|
2007-04-30 00:01:23 +02:00
|
|
|
def __init__(self, ps, bus_name, object_id, test_num, randomize):
|
2007-04-12 04:49:14 +02:00
|
|
|
self._cp = ConfigParser()
|
|
|
|
self._section = "Info"
|
2007-04-17 04:44:54 +02:00
|
|
|
self._test_activities = []
|
|
|
|
self._test_cur_act = ""
|
|
|
|
self._change_timeout = 0
|
2007-04-12 04:49:14 +02:00
|
|
|
|
|
|
|
self._cfg_file = os.path.join(env.get_profile_path(), 'test-buddy-%d' % test_num)
|
|
|
|
|
|
|
|
(pubkey, privkey, registered) = self._load_config()
|
|
|
|
if not pubkey or not len(pubkey) or not privkey or not len(privkey):
|
|
|
|
(pubkey, privkey) = _get_new_keypair(test_num)
|
|
|
|
|
|
|
|
if not pubkey or not privkey:
|
|
|
|
raise RuntimeError("Couldn't get or create test buddy keypair")
|
|
|
|
|
|
|
|
self._save_config(pubkey, privkey, registered)
|
|
|
|
privkey_hash = util.printable_hash(util._sha_data(privkey))
|
|
|
|
|
|
|
|
nick = _get_random_name()
|
|
|
|
from sugar.graphics import xocolor
|
|
|
|
color = xocolor.XoColor().to_string()
|
|
|
|
icon = _get_random_image()
|
|
|
|
|
2007-04-17 04:44:54 +02:00
|
|
|
logging.debug("pubkey is %s" % pubkey)
|
|
|
|
GenericOwner.__init__(self, ps, bus_name, object_id, key=pubkey, nick=nick,
|
2007-04-12 04:49:14 +02:00
|
|
|
color=color, icon=icon, registered=registered, key_hash=privkey_hash)
|
|
|
|
|
2007-04-30 00:01:23 +02:00
|
|
|
# Only do the random stuff if randomize is true
|
|
|
|
if randomize:
|
|
|
|
self._ps.connect('connection-status', self._ps_connection_status_cb)
|
2007-04-17 04:44:54 +02:00
|
|
|
|
|
|
|
def _share_reply_cb(self, actid, object_path):
|
|
|
|
activity = self._ps.internal_get_activity(actid)
|
|
|
|
if not activity or not object_path:
|
|
|
|
logging.debug("Couldn't find activity %s even though it was shared." % actid)
|
|
|
|
return
|
|
|
|
logging.debug("Shared activity %s (%s)." % (actid, activity.props.name))
|
|
|
|
self._test_activities.append(activity)
|
|
|
|
|
|
|
|
def _share_error_cb(self, actid, err):
|
|
|
|
logging.debug("Error sharing activity %s: %s" % (actid, str(err)))
|
|
|
|
|
|
|
|
def _ps_connection_status_cb(self, ps, connected):
|
|
|
|
if not connected:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not len(self._test_activities):
|
|
|
|
# Share some activities
|
|
|
|
actid = util.unique_id("Activity 1")
|
|
|
|
callbacks = (lambda *args: self._share_reply_cb(actid, *args),
|
|
|
|
lambda *args: self._share_error_cb(actid, *args))
|
|
|
|
atype = "org.laptop.WebActivity"
|
|
|
|
properties = {"foo": "bar"}
|
|
|
|
self._ps._share_activity(actid, atype, "Wembley Stadium", properties, callbacks)
|
|
|
|
|
|
|
|
actid2 = util.unique_id("Activity 2")
|
|
|
|
callbacks = (lambda *args: self._share_reply_cb(actid2, *args),
|
|
|
|
lambda *args: self._share_error_cb(actid2, *args))
|
|
|
|
atype = "org.laptop.WebActivity"
|
|
|
|
properties = {"baz": "bar"}
|
|
|
|
self._ps._share_activity(actid2, atype, "Maine Road", properties, callbacks)
|
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
# Change a random property ever 10 seconds
|
2007-04-17 04:44:54 +02:00
|
|
|
if self._change_timeout == 0:
|
|
|
|
self._change_timeout = gobject.timeout_add(10000, self._update_something)
|
2007-04-12 04:49:14 +02:00
|
|
|
|
|
|
|
def set_registered(self, value):
|
|
|
|
if value:
|
|
|
|
self._registered = True
|
|
|
|
|
|
|
|
def _load_config(self):
|
|
|
|
if not os.path.exists(self._cfg_file):
|
|
|
|
return (None, None, False)
|
|
|
|
if not self._cp.read([self._cfg_file]):
|
|
|
|
return (None, None, False)
|
|
|
|
if not self._cp.has_section(self._section):
|
|
|
|
return (None, None, False)
|
|
|
|
|
|
|
|
try:
|
|
|
|
pubkey = self._cp.get(self._section, "pubkey")
|
|
|
|
privkey = self._cp.get(self._section, "privkey")
|
|
|
|
registered = self._cp.get(self._section, "registered")
|
|
|
|
return (pubkey, privkey, registered)
|
|
|
|
except NoOptionError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return (None, None, False)
|
|
|
|
|
|
|
|
def _save_config(self, pubkey, privkey, registered):
|
|
|
|
# Save config again
|
|
|
|
if not self._cp.has_section(self._section):
|
|
|
|
self._cp.add_section(self._section)
|
|
|
|
self._cp.set(self._section, "pubkey", pubkey)
|
|
|
|
self._cp.set(self._section, "privkey", privkey)
|
|
|
|
self._cp.set(self._section, "registered", registered)
|
|
|
|
f = open(self._cfg_file, 'w')
|
|
|
|
self._cp.write(f)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def _update_something(self):
|
2007-04-12 06:14:29 +02:00
|
|
|
it = random.randint(0, 10000) % 4
|
2007-04-12 04:49:14 +02:00
|
|
|
if it == 0:
|
|
|
|
self.props.icon = _get_random_image()
|
|
|
|
elif it == 1:
|
|
|
|
from sugar.graphics import xocolor
|
2007-04-20 18:34:16 +02:00
|
|
|
props = {_PROP_COLOR: xocolor.XoColor().to_string()}
|
2007-04-12 04:49:14 +02:00
|
|
|
self.set_properties(props)
|
|
|
|
elif it == 2:
|
2007-04-20 18:34:16 +02:00
|
|
|
props = {_PROP_NICK: _get_random_name()}
|
2007-04-12 04:49:14 +02:00
|
|
|
self.set_properties(props)
|
|
|
|
elif it == 3:
|
2007-04-17 04:44:54 +02:00
|
|
|
actid = ""
|
|
|
|
idx = random.randint(0, len(self._test_activities))
|
|
|
|
# if idx == len(self._test_activites), it means no current
|
|
|
|
# activity
|
|
|
|
if idx < len(self._test_activities):
|
|
|
|
activity = self._test_activities[idx]
|
|
|
|
actid = activity.props.id
|
2007-04-20 18:34:16 +02:00
|
|
|
props = {_PROP_CURACT: actid}
|
2007-04-12 04:49:14 +02:00
|
|
|
self.set_properties(props)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def _hash_private_key(self):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Unused method to has a private key, see profile"""
|
2007-04-12 04:49:14 +02:00
|
|
|
self.privkey_hash = None
|
|
|
|
|
|
|
|
key_path = os.path.join(env.get_profile_path(), 'owner.key')
|
|
|
|
try:
|
|
|
|
f = open(key_path, "r")
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
except IOError, e:
|
|
|
|
logging.error("Error reading private key: %s" % e)
|
|
|
|
return
|
|
|
|
|
|
|
|
key = ""
|
|
|
|
for l in lines:
|
|
|
|
l = l.strip()
|
|
|
|
if l.startswith("-----BEGIN DSA PRIVATE KEY-----"):
|
|
|
|
continue
|
|
|
|
if l.startswith("-----END DSA PRIVATE KEY-----"):
|
|
|
|
continue
|
|
|
|
key += l
|
|
|
|
if not len(key):
|
|
|
|
logging.error("Error parsing public key.")
|
|
|
|
|
|
|
|
# hash it
|
|
|
|
key_hash = util._sha_data(key)
|
|
|
|
self.privkey_hash = util.printable_hash(key_hash)
|
|
|
|
|
|
|
|
def _extract_public_key(keyfile):
|
|
|
|
try:
|
|
|
|
f = open(keyfile, "r")
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
except IOError, e:
|
|
|
|
logging.error("Error reading public key: %s" % e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Extract the public key
|
|
|
|
magic = "ssh-dss "
|
|
|
|
key = ""
|
|
|
|
for l in lines:
|
|
|
|
l = l.strip()
|
|
|
|
if not l.startswith(magic):
|
|
|
|
continue
|
|
|
|
key = l[len(magic):]
|
|
|
|
break
|
|
|
|
if not len(key):
|
|
|
|
logging.error("Error parsing public key.")
|
|
|
|
return None
|
|
|
|
return key
|
|
|
|
|
|
|
|
def _extract_private_key(keyfile):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Get a private key from a private key file"""
|
2007-04-12 04:49:14 +02:00
|
|
|
# Extract the private key
|
|
|
|
try:
|
|
|
|
f = open(keyfile, "r")
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
except IOError, e:
|
|
|
|
logging.error("Error reading private key: %s" % e)
|
|
|
|
return None
|
|
|
|
|
|
|
|
key = ""
|
|
|
|
for l in lines:
|
|
|
|
l = l.strip()
|
|
|
|
if l.startswith("-----BEGIN DSA PRIVATE KEY-----"):
|
|
|
|
continue
|
|
|
|
if l.startswith("-----END DSA PRIVATE KEY-----"):
|
|
|
|
continue
|
|
|
|
key += l
|
|
|
|
if not len(key):
|
|
|
|
logging.error("Error parsing private key.")
|
|
|
|
return None
|
|
|
|
return key
|
|
|
|
|
|
|
|
def _get_new_keypair(num):
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Retrieve a public/private key pair for testing"""
|
2007-04-12 04:49:14 +02:00
|
|
|
# Generate keypair
|
|
|
|
privkeyfile = os.path.join("/tmp", "test%d.key" % num)
|
|
|
|
pubkeyfile = os.path.join("/tmp", 'test%d.key.pub' % num)
|
|
|
|
|
|
|
|
# force-remove key files if they exist to ssh-keygen doesn't
|
|
|
|
# start asking questions
|
|
|
|
try:
|
|
|
|
os.remove(pubkeyfile)
|
|
|
|
os.remove(privkeyfile)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
cmd = "ssh-keygen -q -t dsa -f %s -C '' -N ''" % privkeyfile
|
|
|
|
import commands
|
|
|
|
print "Generating new keypair..."
|
|
|
|
(s, o) = commands.getstatusoutput(cmd)
|
|
|
|
print "Done."
|
|
|
|
pubkey = privkey = None
|
|
|
|
if s != 0:
|
|
|
|
logging.error("Could not generate key pair: %d (%s)" % (s, o))
|
|
|
|
else:
|
|
|
|
pubkey = _extract_public_key(pubkeyfile)
|
|
|
|
privkey = _extract_private_key(privkeyfile)
|
|
|
|
|
|
|
|
try:
|
|
|
|
os.remove(pubkeyfile)
|
|
|
|
os.remove(privkeyfile)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
return (pubkey, privkey)
|
|
|
|
|
|
|
|
def _get_random_name():
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Produce random names for testing"""
|
2007-04-12 04:49:14 +02:00
|
|
|
names = ["Liam", "Noel", "Guigsy", "Whitey", "Bonehead"]
|
|
|
|
return names[random.randint(0, len(names) - 1)]
|
2007-03-09 22:28:35 +01:00
|
|
|
|
2007-04-12 04:49:14 +02:00
|
|
|
def _get_random_image():
|
2007-04-20 22:25:37 +02:00
|
|
|
"""Produce a random image for display"""
|
2007-03-09 22:28:35 +01:00
|
|
|
import cairo, math, random, gtk
|
|
|
|
|
|
|
|
def rand():
|
|
|
|
return random.random()
|
|
|
|
|
|
|
|
SIZE = 200
|
|
|
|
|
|
|
|
s = cairo.ImageSurface(cairo.FORMAT_ARGB32, SIZE, SIZE)
|
|
|
|
cr = cairo.Context(s)
|
|
|
|
|
|
|
|
# background gradient
|
|
|
|
cr.save()
|
|
|
|
g = cairo.LinearGradient(0, 0, 1, 1)
|
|
|
|
g.add_color_stop_rgba(1, rand(), rand(), rand(), rand())
|
|
|
|
g.add_color_stop_rgba(0, rand(), rand(), rand(), rand())
|
|
|
|
cr.set_source(g)
|
|
|
|
cr.rectangle(0, 0, SIZE, SIZE);
|
|
|
|
cr.fill()
|
|
|
|
cr.restore()
|
|
|
|
|
|
|
|
# random path
|
|
|
|
cr.set_line_width(10 * rand() + 5)
|
|
|
|
cr.move_to(SIZE * rand(), SIZE * rand())
|
|
|
|
cr.line_to(SIZE * rand(), SIZE * rand())
|
|
|
|
cr.rel_line_to(SIZE * rand() * -1, 0)
|
|
|
|
cr.close_path()
|
|
|
|
cr.stroke()
|
|
|
|
|
|
|
|
# a circle
|
|
|
|
cr.set_source_rgba(rand(), rand(), rand(), rand())
|
|
|
|
cr.arc(SIZE * rand(), SIZE * rand(), 100 * rand() + 30, 0, 2 * math.pi)
|
|
|
|
cr.fill()
|
|
|
|
|
|
|
|
# another circle
|
|
|
|
cr.set_source_rgba(rand(), rand(), rand(), rand())
|
|
|
|
cr.arc(SIZE * rand(), SIZE * rand(), 100 * rand() + 30, 0, 2 * math.pi)
|
|
|
|
cr.fill()
|
|
|
|
|
|
|
|
def img_convert_func(buf, data):
|
|
|
|
data[0] += buf
|
|
|
|
return True
|
|
|
|
|
|
|
|
data = [""]
|
|
|
|
pixbuf = gtk.gdk.pixbuf_new_from_data(s.get_data(), gtk.gdk.COLORSPACE_RGB,
|
|
|
|
True, 8, s.get_width(), s.get_height(), s.get_stride())
|
|
|
|
pixbuf.save_to_callback(img_convert_func, "jpeg", {"quality": "90"}, data)
|
|
|
|
del pixbuf
|
|
|
|
|
|
|
|
return str(data[0])
|
2007-02-22 20:11:31 +01:00
|
|
|
|