/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to mandos-ctl

  • Committer: teddy at recompile
  • Date: 2019-12-04 23:46:59 UTC
  • Revision ID: teddy@recompile.se-20191204234659-m4u7bxnjrns6d3fc
Simplification of Python 3 compatibility code

Apply previously-made change in mandos and mandos-ctl also to
mandos-monitor.

* mandos-monitor: Set "__metaclass__ = type" globally for Python 2,
  and remove inheriting from "object" in all places possible.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/python
2
 
# -*- after-save-hook: (lambda () (let ((command (if (fboundp 'file-local-name) (file-local-name (buffer-file-name)) (or (file-remote-p (buffer-file-name) 'localname) (buffer-file-name))))) (if (= (progn (if (get-buffer "*Test*") (kill-buffer "*Test*")) (process-file-shell-command (format "%s --test" (shell-quote-argument command)) nil "*Test*")) 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w))) (progn (with-current-buffer "*Test*" (compilation-mode)) (display-buffer "*Test*" '(display-buffer-in-side-window)))))); coding: utf-8 -*-
 
1
#!/usr/bin/python3 -bbI
 
2
# -*- after-save-hook: (lambda () (let ((command (if (fboundp 'file-local-name) (file-local-name (buffer-file-name)) (or (file-remote-p (buffer-file-name) 'localname) (buffer-file-name))))) (if (= (progn (if (get-buffer "*Test*") (kill-buffer "*Test*")) (process-file-shell-command (format "%s --check" (shell-quote-argument command)) nil "*Test*")) 0) (let ((w (get-buffer-window "*Test*"))) (if w (delete-window w))) (progn (with-current-buffer "*Test*" (compilation-mode)) (display-buffer "*Test*" '(display-buffer-in-side-window)))))); coding: utf-8 -*-
3
3
#
4
4
# Mandos Monitor - Control and monitor the Mandos server
5
5
#
46
46
import tempfile
47
47
import contextlib
48
48
 
 
49
if sys.version_info.major == 2:
 
50
    __metaclass__ = type
 
51
    str = unicode
 
52
 
49
53
try:
50
54
    import pydbus
51
55
    import gi
53
57
except ImportError:
54
58
    import dbus as dbus_python
55
59
    pydbus = None
56
 
    class gi(object):
 
60
    class gi:
57
61
        """Dummy gi module, for the tests"""
58
 
        class repository(object):
59
 
            class GLib(object):
 
62
        class repository:
 
63
            class GLib:
60
64
                class Error(Exception):
61
65
                    pass
62
66
 
72
76
logging.captureWarnings(True)   # Show warnings via the logging system
73
77
 
74
78
if sys.version_info.major == 2:
75
 
    str = unicode
76
79
    import StringIO
77
80
    io.StringIO = StringIO.StringIO
78
81
 
79
82
locale.setlocale(locale.LC_ALL, "")
80
83
 
81
 
version = "1.8.4"
 
84
version = "1.8.9"
82
85
 
83
86
 
84
87
def main():
250
253
def rfc3339_duration_to_delta(duration):
251
254
    """Parse an RFC 3339 "duration" and return a datetime.timedelta
252
255
 
253
 
    >>> rfc3339_duration_to_delta("P7D")
254
 
    datetime.timedelta(7)
255
 
    >>> rfc3339_duration_to_delta("PT60S")
256
 
    datetime.timedelta(0, 60)
257
 
    >>> rfc3339_duration_to_delta("PT60M")
258
 
    datetime.timedelta(0, 3600)
259
 
    >>> rfc3339_duration_to_delta("P60M")
260
 
    datetime.timedelta(1680)
261
 
    >>> rfc3339_duration_to_delta("PT24H")
262
 
    datetime.timedelta(1)
263
 
    >>> rfc3339_duration_to_delta("P1W")
264
 
    datetime.timedelta(7)
265
 
    >>> rfc3339_duration_to_delta("PT5M30S")
266
 
    datetime.timedelta(0, 330)
267
 
    >>> rfc3339_duration_to_delta("P1DT3M20S")
268
 
    datetime.timedelta(1, 200)
 
256
    >>> rfc3339_duration_to_delta("P7D") == datetime.timedelta(7)
 
257
    True
 
258
    >>> rfc3339_duration_to_delta("PT60S") == datetime.timedelta(0, 60)
 
259
    True
 
260
    >>> rfc3339_duration_to_delta("PT60M") == datetime.timedelta(hours=1)
 
261
    True
 
262
    >>> # 60 months
 
263
    >>> rfc3339_duration_to_delta("P60M") == datetime.timedelta(1680)
 
264
    True
 
265
    >>> rfc3339_duration_to_delta("PT24H") == datetime.timedelta(1)
 
266
    True
 
267
    >>> rfc3339_duration_to_delta("P1W") == datetime.timedelta(7)
 
268
    True
 
269
    >>> rfc3339_duration_to_delta("PT5M30S") == datetime.timedelta(0, 330)
 
270
    True
 
271
    >>> rfc3339_duration_to_delta("P1DT3M20S") == datetime.timedelta(1, 200)
 
272
    True
269
273
    >>> # Can not be empty:
270
274
    >>> rfc3339_duration_to_delta("")
271
275
    Traceback (most recent call last):
381
385
    """Parse an interval string as documented by Mandos before 1.6.1,
382
386
    and return a datetime.timedelta
383
387
 
384
 
    >>> parse_pre_1_6_1_interval('7d')
385
 
    datetime.timedelta(7)
386
 
    >>> parse_pre_1_6_1_interval('60s')
387
 
    datetime.timedelta(0, 60)
388
 
    >>> parse_pre_1_6_1_interval('60m')
389
 
    datetime.timedelta(0, 3600)
390
 
    >>> parse_pre_1_6_1_interval('24h')
391
 
    datetime.timedelta(1)
392
 
    >>> parse_pre_1_6_1_interval('1w')
393
 
    datetime.timedelta(7)
394
 
    >>> parse_pre_1_6_1_interval('5m 30s')
395
 
    datetime.timedelta(0, 330)
396
 
    >>> parse_pre_1_6_1_interval('')
397
 
    datetime.timedelta(0)
 
388
    >>> parse_pre_1_6_1_interval('7d') == datetime.timedelta(days=7)
 
389
    True
 
390
    >>> parse_pre_1_6_1_interval('60s') == datetime.timedelta(0, 60)
 
391
    True
 
392
    >>> parse_pre_1_6_1_interval('60m') == datetime.timedelta(hours=1)
 
393
    True
 
394
    >>> parse_pre_1_6_1_interval('24h') == datetime.timedelta(days=1)
 
395
    True
 
396
    >>> parse_pre_1_6_1_interval('1w') == datetime.timedelta(days=7)
 
397
    True
 
398
    >>> parse_pre_1_6_1_interval('5m 30s') == datetime.timedelta(0, 330)
 
399
    True
 
400
    >>> parse_pre_1_6_1_interval('') == datetime.timedelta(0)
 
401
    True
398
402
    >>> # Ignore unknown characters, allow any order and repetitions
399
 
    >>> parse_pre_1_6_1_interval('2dxy7zz11y3m5m')
400
 
    datetime.timedelta(2, 480, 18000)
 
403
    >>> parse_pre_1_6_1_interval('2dxy7zz11y3m5m') == datetime.timedelta(2, 480, 18000)
 
404
    True
401
405
 
402
406
    """
403
407
 
466
470
        parser.error("--remove can only be combined with --deny")
467
471
 
468
472
 
469
 
class dbus(object):
 
473
class dbus:
470
474
 
471
 
    class SystemBus(object):
 
475
    class SystemBus:
472
476
 
473
477
        object_manager_iface = "org.freedesktop.DBus.ObjectManager"
474
478
        def get_managed_objects(self, busname, objectpath):
520
524
        pass
521
525
 
522
526
 
523
 
class dbus_python_adapter(object):
 
527
class dbus_python_adapter:
524
528
 
525
529
    class SystemBus(dbus.MandosBus):
526
530
        """Use dbus-python"""
580
584
                                     self.client_interface, key,
581
585
                                     value)
582
586
 
583
 
    class SilenceLogger(object):
 
587
    class SilenceLogger:
584
588
        "Simple context manager to silence a particular logger"
585
589
        def __init__(self, loggername):
586
590
            self.logger = logging.getLogger(loggername)
615
619
                return new_object
616
620
 
617
621
 
618
 
class pydbus_adapter(object):
 
622
class pydbus_adapter:
619
623
    class SystemBus(dbus.MandosBus):
620
624
        def __init__(self, module=pydbus):
621
625
            self.pydbus = module
711
715
    return commands
712
716
 
713
717
 
714
 
class command(object):
 
718
class command:
715
719
    """A namespace for command classes"""
716
720
 
717
 
    class Base(object):
 
721
    class Base:
718
722
        """Abstract base class for commands"""
719
723
        def run(self, clients, bus=None):
720
724
            """Normal commands should implement run_on_one_client(),
783
787
                keywords = self.all_keywords
784
788
            print(self.TableOfClients(clients.values(), keywords))
785
789
 
786
 
        class TableOfClients(object):
 
790
        class TableOfClients:
787
791
            tableheaders = {
788
792
                "Name": "Name",
789
793
                "Enabled": "Enabled",
1020
1024
                                                     "output"))
1021
1025
 
1022
1026
 
1023
 
class Unique(object):
 
1027
class Unique:
1024
1028
    """Class for objects which exist only to be unique objects, since
1025
1029
unittest.mock.sentinel only exists in Python 3.3"""
1026
1030
 
1310
1314
class Test_dbus_python_adapter_SystemBus(TestCaseWithAssertLogs):
1311
1315
 
1312
1316
    def MockDBusPython_func(self, func):
1313
 
        class mock_dbus_python(object):
 
1317
        class mock_dbus_python:
1314
1318
            """mock dbus-python module"""
1315
 
            class exceptions(object):
 
1319
            class exceptions:
1316
1320
                """Pseudo-namespace"""
1317
1321
                class DBusException(Exception):
1318
1322
                    pass
1319
 
            class SystemBus(object):
 
1323
            class SystemBus:
1320
1324
                @staticmethod
1321
1325
                def get_object(busname, objectpath):
1322
1326
                    DBusObject = collections.namedtuple(
1336
1340
                                    dbus_interface=dbus_interface)
1337
1341
                    return DBusObject(methodname=method,
1338
1342
                                      Set=set_property)
1339
 
            class Boolean(object):
 
1343
            class Boolean:
1340
1344
                def __init__(self, value):
1341
1345
                    self.value = bool(value)
1342
1346
                def __bool__(self):
1554
1558
            self.call_method(bus, "methodname", "busname",
1555
1559
                             "objectpath", "interface")
1556
1560
 
1557
 
    class fake_dbus_python_raises_exception_on_connect(object):
 
1561
    class fake_dbus_python_raises_exception_on_connect:
1558
1562
        """fake dbus-python module"""
1559
 
        class exceptions(object):
 
1563
        class exceptions:
1560
1564
            """Pseudo-namespace"""
1561
1565
            class DBusException(Exception):
1562
1566
                pass
1570
1574
 
1571
1575
 
1572
1576
class Test_dbus_python_adapter_CachingBus(unittest.TestCase):
1573
 
    class mock_dbus_python(object):
 
1577
    class mock_dbus_python:
1574
1578
        """mock dbus-python modules"""
1575
 
        class SystemBus(object):
 
1579
        class SystemBus:
1576
1580
            @staticmethod
1577
1581
            def get_object(busname, objectpath):
1578
1582
                return Unique()
1624
1628
class Test_pydbus_adapter_SystemBus(TestCaseWithAssertLogs):
1625
1629
 
1626
1630
    def Stub_pydbus_func(self, func):
1627
 
        class stub_pydbus(object):
 
1631
        class stub_pydbus:
1628
1632
            """stub pydbus module"""
1629
 
            class SystemBus(object):
 
1633
            class SystemBus:
1630
1634
                @staticmethod
1631
1635
                def get(busname, objectpath):
1632
1636
                    DBusObject = collections.namedtuple(
1678
1682
            self.call_method(bus, "methodname", "busname",
1679
1683
                             "objectpath", "interface")
1680
1684
 
1681
 
    class fake_pydbus_raises_exception_on_connect(object):
 
1685
    class fake_pydbus_raises_exception_on_connect:
1682
1686
        """fake dbus-python module"""
1683
1687
        @classmethod
1684
1688
        def SystemBus(cls):
1688
1692
            return Bus(get=get)
1689
1693
 
1690
1694
    def test_set_property_uses_setattr(self):
1691
 
        class Object(object):
 
1695
        class Object:
1692
1696
            pass
1693
1697
        obj = Object()
1694
 
        class pydbus_spy(object):
1695
 
            class SystemBus(object):
 
1698
        class pydbus_spy:
 
1699
            class SystemBus:
1696
1700
                @staticmethod
1697
1701
                def get(busname, objectpath):
1698
1702
                    return {"interface": obj}
1705
1709
    def test_get_suppresses_xml_deprecation_warning(self):
1706
1710
        if sys.version_info.major >= 3:
1707
1711
            return
1708
 
        class stub_pydbus_get(object):
1709
 
            class SystemBus(object):
 
1712
        class stub_pydbus_get:
 
1713
            class SystemBus:
1710
1714
                @staticmethod
1711
1715
                def get(busname, objectpath):
1712
1716
                    warnings.warn_explicit(
1720
1724
 
1721
1725
 
1722
1726
class Test_pydbus_adapter_CachingBus(unittest.TestCase):
1723
 
    class stub_pydbus(object):
 
1727
    class stub_pydbus:
1724
1728
        """stub pydbus module"""
1725
 
        class SystemBus(object):
 
1729
        class SystemBus:
1726
1730
            @staticmethod
1727
1731
            def get(busname, objectpath):
1728
1732
                return Unique()
2001
2005
 
2002
2006
 
2003
2007
    def test_manual_page_example_1(self):
2004
 
        self.assert_command_from_args("--verbose".split(),
 
2008
        self.assert_command_from_args("",
2005
2009
                                      command.PrintTable,
2006
2010
                                      clients=[],
2007
 
                                      verbose=True)
 
2011
                                      verbose=False)
2008
2012
 
2009
2013
    def test_manual_page_example_2(self):
2010
2014
        self.assert_command_from_args(