/usr/lib/python3/dist-packages/autopilot/introspection/_search.py is in python3-autopilot 1.5.1+16.04.20160412-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2014 Canonical
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
"""Private module for searching dbus for useful connections."""
import dbus
import logging
import os
import psutil
import subprocess
from functools import partial
from operator import methodcaller
from autopilot import dbus_handler
from autopilot._timeout import Timeout
from autopilot.exceptions import ProcessSearchError
from autopilot.introspection import backends
from autopilot.introspection import constants
from autopilot.introspection import dbus as ap_dbus
from autopilot.introspection import _object_registry
from autopilot.introspection._xpathselect import get_classname_from_path
from autopilot.introspection.backends import WireProtocolVersionMismatch
from autopilot.introspection.utilities import (
_get_bus_connections_pid,
_pid_is_running,
)
from autopilot.utilities import deprecated
logger = logging.getLogger(__name__)
@deprecated('get_proxy_object_for_existing_process')
def get_autopilot_proxy_object_for_process(
process,
emulator_base,
dbus_bus='session'
):
"""Return the autopilot proxy object for the given *process*.
:raises RuntimeError: if no autopilot interface was found.
"""
pid = process.pid
proxy_obj = get_proxy_object_for_existing_process(
pid,
process=process,
emulator_base=emulator_base,
dbus_bus=dbus_bus,
)
proxy_obj.set_process(process)
return proxy_obj
def get_proxy_object_for_existing_process(**kwargs):
"""Return a single proxy object for an application that is already running
(i.e. launched outside of Autopilot).
Searches the given bus (supplied by the kwarg **dbus_bus**) for an
application matching the search criteria (also supplied in kwargs, see
further down for explaination on what these can be.)
Returns a proxy object created using the supplied custom emulator
**emulator_base** (which defaults to None).
This function take kwargs arguments containing search parameter values to
use when searching for the target application.
**Possible search criteria**:
*(unless specified otherwise these parameters default to None)*
:param pid: The PID of the application to search for.
:param process: The process of the application to search for.
If provided only the pid of the process is used in the search, but if
the process exits before the search is complete it is used to supply
details provided by the process object.
:param connection_name: A string containing the DBus connection name to
use with the search criteria.
:param application_name: A string containing the applications name to
search for.
:param object_path: A string containing the object path to use as the
search criteria.
Defaults to:
:py:data:`autopilot.introspection.constants.AUTOPILOT_PATH`.
**Non-search parameters:**
:param dbus_bus: The DBus bus to search for the application.
Must be a string containing either 'session', 'system' or the
custom buses name (i.e. 'unix:abstract=/tmp/dbus-IgothuMHNk').
Defaults to 'session'
:param emulator_base: The custom emulator to use when creating the
resulting proxy object.
Defaults to None
**Exceptions possibly thrown by this function:**
:raises ProcessSearchError: If no search criteria match.
:raises RuntimeError: If the search criteria results in many matches.
:raises RuntimeError: If both ``process`` and ``pid`` are supplied, but
``process.pid != pid``.
**Examples:**
Retrieving an application on the system bus where the applications PID is
known::
app_proxy = get_proxy_object_for_existing_process(pid=app_pid)
Multiple criteria are allowed, for instance you could search on **pid**
and **connection_name**::
app_proxy = get_proxy_object_for_existing_process(
pid=app_pid,
connection_name='org.gnome.Gedit'
)
If the application from the previous example was on the system bus::
app_proxy = get_proxy_object_for_existing_process(
dbus_bus='system',
pid=app_pid,
connection_name='org.gnome.Gedit'
)
It is possible to search for the application given just the applications
name.
An example for an application running on a custom bus searching using the
applications name::
app_proxy = get_proxy_object_for_existing_process(
application_name='qmlscene',
dbus_bus='unix:abstract=/tmp/dbus-IgothuMHNk'
)
"""
# Pop off non-search stuff.
dbus_bus = _get_dbus_bus_from_string(kwargs.pop('dbus_bus', 'session'))
process = kwargs.pop('process', None)
emulator_base = kwargs.pop('emulator_base', None)
# Force default object_path
kwargs['object_path'] = kwargs.get('object_path', constants.AUTOPILOT_PATH)
# Special handling of pid.
pid = _check_process_and_pid_details(process, kwargs.get('pid', None))
if pid is not None:
kwargs['pid'] = pid
matcher_function = _filter_function_from_search_params(kwargs)
connections = _find_matching_connections(
dbus_bus,
matcher_function,
process
)
if pid is not None:
# Due to the filtering including children parents, if there exists a
# top-level pid, take that instead of any children that may have
# matched.
connections = _filter_parent_pids_from_children(
pid,
connections,
dbus_bus
)
_raise_if_not_single_result(
connections,
_get_search_criteria_string_representation(**kwargs)
)
object_path = kwargs['object_path']
connection_name = connections[0]
return _make_proxy_object(
_get_dbus_address_object(connection_name, object_path, dbus_bus),
emulator_base
)
def _map_connection_to_pid(connection, dbus_bus):
try:
return _get_bus_connections_pid(dbus_bus, connection)
except dbus.DBusException as e:
logger.info(
"dbus.DBusException while attempting to get PID for %s: %r" %
(connection, e))
def _filter_parent_pids_from_children(
pid, connections, dbus_bus, _connection_pid_fn=_map_connection_to_pid):
"""Return any connections that have an actual pid matching the requested
and aren't just a child of that requested pid.
:param pid: Pid passed in for matching
:param connections: List of connections to filter
:param dbus_bus: Dbus object that the connections are contained.
:param _connection_pid_fn: Function that takes 2 args 'connection' 'dbus
object' that returns the pid (or None if not found) of the connection on
that dbus bus.
(Note: Useful for testing.)
:returns: List of suitable connections (e.g. returns what was passed if no
connections match the pid (i.e. all matches are children)).
"""
for conn in connections:
if pid == _connection_pid_fn(conn, dbus_bus):
logger.info('Found the parent pid, ignoring any others.')
return [conn]
return connections
def _get_dbus_bus_from_string(dbus_string):
if dbus_string == 'session':
return dbus_handler.get_session_bus()
elif dbus_string == 'system':
return dbus_handler.get_system_bus()
else:
return dbus_handler.get_custom_bus(dbus_string)
def _check_process_and_pid_details(process=None, pid=None):
"""Do error checking on process and pid specification.
:raises RuntimeError: if both process and pid are specified, but the
process's 'pid' attribute is different to the pid attribute specified.
:raises ProcessSearchError: if the process specified is not running.
:returns: the pid to use in all search queries.
"""
if process is not None:
if pid is None:
pid = process.pid
elif pid != process.pid:
raise RuntimeError("Supplied PID and process.pid do not match.")
if pid is not None and not _pid_is_running(pid):
raise ProcessSearchError("PID %d could not be found" % pid)
return pid
def _filter_function_from_search_params(search_params, filter_lookup=None):
filters = _mandatory_filters() + _filters_from_search_parameters(
search_params,
filter_lookup
)
return _filter_function_with_sorted_filters(filters, search_params)
def _mandatory_filters():
"""Returns a list of Filters that are considered mandatory regardless of
the search parameters supplied by the user.
"""
return [
ConnectionIsNotOurConnection,
ConnectionIsNotOrgFreedesktopDBus
]
def _filter_lookup_map():
return dict(
connection_name=ConnectionHasName,
application_name=ConnectionHasAppName,
object_path=ConnectionHasPathWithAPInterface,
pid=ConnectionHasPid,
)
def _filters_from_search_parameters(parameters, filter_lookup=None):
parameter_filter_lookup = filter_lookup or _filter_lookup_map()
try:
filter_list = list({
parameter_filter_lookup[key]
for key in parameters.keys()
})
return filter_list
except KeyError as e:
raise KeyError(
"Search parameter %s doesn't have a corresponding filter in %r"
% (e, parameter_filter_lookup),
)
def _filter_function_with_sorted_filters(filters, search_params):
"""Returns a callable filter function that will take the argument
(dbus_tuple).
The returned filter function will be bound to use a prioritised filter list
and the supplied search parameters dictionary.
"""
sorted_filter_list = _priority_sort_filters(filters)
return partial(_filter_runner, sorted_filter_list, search_params)
def _priority_sort_filters(filter_list):
return sorted(filter_list, key=methodcaller('priority'), reverse=True)
def _filter_runner(filter_list, search_parameters, dbus_tuple):
"""Helper function to run filters over dbus connections.
:param filter_list: List of filters to call matches on passing the provided
dbus details and search parameters.
:param dbus_tuple: 2 length tuple containing (bus, connection_name) where
bus is a SessionBus, SystemBus or BusConnection object and
connection_name is a string.
:param search_parameters: Dictionary of search parameters that the filters
will consume to make their decisions.
"""
if not filter_list:
raise ValueError("Filter list must not be empty")
return all(
f.matches(dbus_tuple, search_parameters)
for f in filter_list
)
def _find_matching_connections(bus, connection_matcher, process=None):
"""Returns a list of connection names that have passed the
connection_matcher.
:param dbus_bus: A DBus bus object to search
(i.e. SessionBus, SystemBus or BusConnection)
:param connection_matcher: Callable that takes a connection name and
returns True if it is what we're looking for, False otherwise.
:param process: (optional) A process object that we're looking for it's
dbus connection.
Used to ensure that the process is in fact still running
while we're searching for it.
"""
for _ in Timeout.default():
_get_child_pids.reset_cache()
_raise_if_process_has_exited(process)
connections = bus.list_names()
valid_connections = [
c for c
in connections
if connection_matcher((bus, c))
]
if len(valid_connections) >= 1:
return _dedupe_connections_on_pid(valid_connections, bus)
return []
def _raise_if_process_has_exited(process):
"""Raises ProcessSearchError if process is no longer running."""
if process is not None and not _process_is_running(process):
return_code = process.poll()
raise ProcessSearchError(
"Process exited with exit code: %d"
% return_code
)
def _process_is_running(process):
return process.poll() is None
def _dedupe_connections_on_pid(valid_connections, bus):
seen_pids = []
deduped_connections = []
for connection in valid_connections:
pid = _get_bus_connections_pid(bus, connection)
if pid not in seen_pids:
seen_pids.append(pid)
deduped_connections.append(connection)
return deduped_connections
def _raise_if_not_single_result(connections, criteria_string):
if connections is None or len(connections) == 0:
raise ProcessSearchError(
"Search criteria (%s) returned no results" %
(criteria_string)
)
if len(connections) > 1:
raise RuntimeError(
"Search criteria (%s) returned multiple results"
% (criteria_string)
)
def _get_search_criteria_string_representation(**kwargs):
# Some slight re-naming for process objects
if kwargs.get('process') is not None:
kwargs['process_object'] = "%r" % kwargs.pop('process')
return ", ".join([
"%s = %r" % (k.replace("_", " "), v)
for k, v
in kwargs.items()
])
def _make_proxy_object(dbus_address, emulator_base):
"""Returns a root proxy object given a DBus service name.
:param dbus_address: The DBusAddress object we're querying.
:param emulator_base: The emulator base object (or None), as provided by
the user.
"""
# make sure we always have an emulator base. Either use the one the user
# gave us, or make one:
emulator_base = emulator_base or _make_default_emulator_base()
_raise_if_base_class_not_actually_base(emulator_base)
# Get the dbus introspection Xml for the backend.
intro_xml = _get_introspection_xml_from_backend(dbus_address)
try:
# Figure out if the backend has any extension methods, and return
# classes that understand how to use each of those extensions:
extension_classes = _get_proxy_bases_from_introspection_xml(intro_xml)
# Register those base classes for everything that will derive from this
# emulator base class.
_object_registry.register_extension_classes_for_proxy_base(
emulator_base,
extension_classes,
)
except RuntimeError as e:
e.args = (
"Could not find Autopilot interface on dbus address '%s'."
% dbus_address,
)
raise e
cls_name, path, cls_state = _get_proxy_object_class_name_and_state(
dbus_address
)
proxy_class = _object_registry._get_proxy_object_class(
emulator_base._id,
path,
cls_state
)
# For this object only, add the ApplicationProxy class, since it's the
# root of the tree. Ideally this would be nicer...
if ApplicationProxyObject not in proxy_class.__bases__:
proxy_class.__bases__ += (ApplicationProxyObject, )
return proxy_class(cls_state, path, backends.Backend(dbus_address))
def _make_default_emulator_base():
"""Make a default base class for all proxy classes to derive from."""
return type("DefaultEmulatorBase", (ap_dbus.DBusIntrospectionObject,), {})
WRONG_CPO_CLASS_MSG = '''\
base_class: {passed} does not appear to be the actual base CPO class.
Perhaps you meant to use: {actual}.'''
def _raise_if_base_class_not_actually_base(base_class):
"""Raises ValueError if the provided base_class is not actually the
base_class
To ensure that the expected base classes are used when creating proxy
objects.
:param base_class: The base class to check.
:raises ValueError: The actual base class is not the one provided
"""
actual_base_class = base_class
for cls in base_class.mro():
if hasattr(cls, '_id'):
actual_base_class = cls
if actual_base_class != base_class:
raise(
ValueError(
WRONG_CPO_CLASS_MSG.format(
passed=base_class,
actual=actual_base_class
)
)
)
def _make_proxy_object_async(
data_source, emulator_base, reply_handler, error_handler):
"""Make a proxy object for a dbus backend.
Similar to :meth:`_make_proxy_object` except this method runs
asynchronously and must have a reply_handler callable set. The
reply_handler will be called with a single argument: The proxy object.
"""
# Note: read this function backwards!
#
# Due to the callbacks, I need to define the end of the callback chain
# first, so start reading from the bottom of the function upwards, and
# it'll make a whole lot more sense.
# Final phase: We have all the information we need, now we construct
# everything. This phase has no dbus calls, and so is very fast:
def build_proxy(introspection_xml, cls_name, path, cls_state):
# Figure out if the backend has any extension methods, and return
# classes that understand how to use each of those extensions:
extension_classes = _get_proxy_bases_from_introspection_xml(
introspection_xml
)
# Register those base classes for everything that will derive from this
# emulator base class.
_object_registry.register_extension_classes_for_proxy_base(
emulator_base,
extension_classes,
)
proxy_class = _object_registry._get_proxy_object_class(
emulator_base._id,
path,
cls_state
)
reply_handler(
proxy_class(cls_state, path, backends.Backend(data_source))
)
# Phase 2: We recieve the introspection string, and make an asynchronous
# dbus call to get the state information for the root of this applicaiton.
def get_root_state(introspection_xml):
_get_proxy_object_class_name_and_state(
data_source,
reply_handler=partial(build_proxy, introspection_xml),
error_handler=error_handler,
)
# Phase 1: Make an asynchronous dbus call to get the introspection xml
# from the data_source provided for us.
emulator_base = emulator_base or _make_default_emulator_base()
_get_introspection_xml_from_backend(
data_source,
reply_handler=get_root_state,
error_handler=error_handler
)
def _get_introspection_xml_from_backend(
backend, reply_handler=None, error_handler=None):
"""Get DBus Introspection xml from a backend.
:param backend: The backend object to query.
:param reply_handler: If set, makes a dbus async call, and the result will
be sent to reply_handler. This must be a callable object.
:param error_handler: If set, this callable will recieve any errors, and
the call will be made asyncronously.
:returns: A string containing introspection xml, if called synchronously.
:raises ValueError: if one, but not both of 'reply_handler' and
'error_handler' are set.
"""
if callable(reply_handler) and callable(error_handler):
backend.dbus_introspection_iface.Introspect(
reply_handler=reply_handler,
error_handler=error_handler,
)
elif reply_handler or error_handler:
raise ValueError(
"Both 'reply_handler' and 'error_handler' must be set."
)
else:
return backend.dbus_introspection_iface.Introspect()
def _get_proxy_object_class_name_and_state(
backend, reply_handler=None, error_handler=None):
"""Get details about this autopilot backend via a dbus GetState call.
:param reply_handler: A callable that must accept three positional
arguments, which correspond to the return value of this function when
called synchronously.
:param error_handler: A callable which will recieve any dbus errors, should
they occur.
:raises ValueError: if one, but not both of reply_handler and error_handler
are set.
:returns: A tuple containing the class name of the root of the
introspection tree, the full path to the root of the introspection
tree, and the state dictionary of the root node in the introspection
tree.
"""
if callable(reply_handler) and callable(error_handler):
# Async call:
# Since we get an array of state, and we only care about the first one
# we use a lambda to unpack it and get the details we want.
backend.introspection_iface.GetState(
"/",
reply_handler=lambda r: reply_handler(
*_get_details_from_state_data(r[0])
),
error_handler=error_handler,
)
elif reply_handler or error_handler:
raise ValueError(
"Both 'reply_handler' and 'error_handler' must be set."
)
else:
# Sync call
state = backend.introspection_iface.GetState("/")[0]
return _get_details_from_state_data(state)
def _get_details_from_state_data(state_data):
"""Get details from a state data array.
Returns class name, path, and state dictionary.
"""
object_path, object_state = state_data
return (
get_classname_from_path(object_path),
object_path.encode('utf-8'),
object_state,
)
def _get_proxy_bases_from_introspection_xml(introspection_xml):
"""Return tuple of the base classes to use when creating a proxy object.
Currently this works by looking for certain interface names in the XML. In
the future we may want to parse the XML and perform more rigerous checks.
:param introspection_xml: An xml string that describes the exported object
on the dbus backend. This determines which capabilities are present in
the backend, and therefore which base classes should be used to create
the proxy object.
:raises RuntimeError: if the autopilot interface cannot be found.
"""
bases = []
if constants.AP_INTROSPECTION_IFACE not in introspection_xml:
raise RuntimeError("Could not find Autopilot interface.")
if constants.QT_AUTOPILOT_IFACE in introspection_xml:
from autopilot.introspection.qt import QtObjectProxyMixin
bases.append(QtObjectProxyMixin)
return tuple(bases)
class ApplicationProxyObject(object):
"""A class that better supports query data from an application."""
def __init__(self):
self._process = None
def set_process(self, process):
"""Set the subprocess.Popen object of the process that this is a proxy
for.
You should never normally need to call this method.
"""
self._process = process
@property
def pid(self):
return self._process.pid
@property
def process(self):
return self._process
@deprecated(
"the AutopilotTestCase launch_test_application method to handle"
" cleanup of launched applications."
)
def kill_application(self):
"""Kill the running process that this is a proxy for using
'kill `pid`'."""
subprocess.call(["kill", "%d" % self._process.pid])
def _extend_proxy_bases_with_emulator_base(proxy_bases, emulator_base):
if emulator_base is None:
emulator_base = type(
'DefaultEmulatorBase',
(ap_dbus.CustomEmulatorBase,),
{}
)
return proxy_bases + (emulator_base, )
def _get_dbus_address_object(connection_name, object_path, bus):
return backends.DBusAddress(bus, connection_name, object_path)
class _cached_get_child_pids(object):
"""Get a list of all child process Ids, for the given parent.
Since we call this often, and it's a very expensive call, we optimise this
such that the return value will be cached for each scan through the dbus
bus.
Calling reset_cache() at the end of each dbus scan will ensure that you get
fresh values on the next call.
"""
def __init__(self):
self._cached_result = None
def __call__(self, pid):
if self._cached_result is None:
self._cached_result = [
p.pid for p in psutil.Process(pid).children(recursive=True)
]
return self._cached_result
def reset_cache(self):
self._cached_result = None
_get_child_pids = _cached_get_child_pids()
# Filters
class ConnectionIsNotOrgFreedesktopDBus(object):
"""Not interested in any connections with names 'org.freedesktop.DBus'."""
@classmethod
def priority(cls):
"""A connection with this name will never be valid."""
return 13
@classmethod
def matches(cls, dbus_tuple, params):
bus, connection_name = dbus_tuple
return connection_name != 'org.freedesktop.DBus'
class ConnectionIsNotOurConnection(object):
"""Ensure we're not inspecting our own bus connection."""
@classmethod
def priority(cls):
"""The connection from this process will never be valid."""
return 12
@classmethod
def matches(cls, dbus_tuple, params):
try:
bus, connection_name = dbus_tuple
bus_pid = _get_bus_connections_pid(bus, connection_name)
return bus_pid != os.getpid()
except dbus.DBusException:
return False
class ConnectionHasName(object):
"""Ensure connection_name within dbus_tuple is the name we want."""
@classmethod
def priority(cls):
"""Connection name is easy to check for and if not valid, nothing else
will be.
"""
return 11
@classmethod
def matches(cls, dbus_tuple, params):
"""Returns true if the connection name in dbus_tuple is the name
in the search criteria params.
"""
requested_connection_name = params['connection_name']
bus, connection_name = dbus_tuple
return connection_name == requested_connection_name
class ConnectionHasPid(object):
"""Match a connection based on the connections pid."""
@classmethod
def priority(cls):
return 9
@classmethod
def matches(cls, dbus_tuple, params):
"""Match a connection based on the connections pid.
:raises KeyError: if the pid parameter isn't passed in params.
"""
pid = params['pid']
bus, connection_name = dbus_tuple
try:
bus_pid = _get_bus_connections_pid(bus, connection_name)
except dbus.DBusException as e:
logger.info(
"dbus.DBusException while attempting to get PID for %s: %r" %
(connection_name, e))
return False
eligible_pids = [pid] + _get_child_pids(pid)
return bus_pid in eligible_pids
class ConnectionHasPathWithAPInterface(object):
"""Ensure that the connection exposes the Autopilot interface."""
@classmethod
def priority(cls):
return 8
@classmethod
def matches(cls, dbus_tuple, params):
"""Ensure the connection has the path that we expect to be there.
:raises KeyError: if the object_path parameter isn't included in
params.
"""
try:
bus, connection_name = dbus_tuple
path = params['object_path']
obj = bus.get_object(connection_name, path)
dbus.Interface(
obj,
'com.canonical.Autopilot.Introspection'
).GetVersion()
return True
except dbus.DBusException:
return False
class ConnectionHasAppName(object):
"""Ensure the connection has the requested Application name."""
@classmethod
def priority(cls):
return 0
@classmethod
def matches(cls, dbus_tuple, params):
"""Returns True if dbus_tuple has the required application name.
Can be provided an optional object_path parameter. This defaults to
:py:data:`autopilot.introspection.constants.AUTOPILOT_PATH` if not
provided.
This filter should only activated if the application_name is provided
in the search criteria.
:raises KeyError: if the 'application_name' parameter isn't passed in
params
"""
requested_app_name = params['application_name']
object_path = params.get('object_path', constants.AUTOPILOT_PATH)
bus, connection_name = dbus_tuple
try:
app_name = cls._get_application_name(
bus,
connection_name,
object_path
)
return app_name == requested_app_name
except WireProtocolVersionMismatch:
return False
@classmethod
def _get_application_name(cls, bus, connection_name, object_path):
dbus_object = _get_dbus_address_object(
connection_name,
object_path,
bus
)
return cls._get_application_name_from_dbus_address(dbus_object)
@classmethod
def _get_application_name_from_dbus_address(cls, dbus_address):
"""Return the application name from a dbus_address object."""
return get_classname_from_path(
dbus_address.introspection_iface.GetState('/')[0][0]
)
|