48
# Show warnings by default
49
if not sys.warnoptions:
51
warnings.simplefilter("default")
53
log = logging.getLogger(sys.argv[0])
54
logging.basicConfig(level="INFO", # Show info level messages
55
format="%(message)s") # Show basic log messages
57
logging.captureWarnings(True) # Show warnings via the logging system
59
46
if sys.version_info.major == 2:
250
237
def print_clients(clients, keywords):
251
238
def valuetostring(value, keyword):
252
if isinstance(value, dbus.Boolean):
239
if type(value) is dbus.Boolean:
253
240
return "Yes" if value else "No"
254
241
if keyword in ("Timeout", "Interval", "ApprovalDelay",
255
242
"ApprovalDuration", "ExtendedTimeout"):
358
345
if options.all and not has_actions(options):
359
346
parser.error("--all requires an action.")
350
fail_count, test_count = doctest.testmod()
351
sys.exit(os.EX_OK if fail_count == 0 else 1)
362
354
bus = dbus.SystemBus()
363
355
mandos_dbus_objc = bus.get_object(busname, server_path)
364
356
except dbus.exceptions.DBusException:
365
log.critical("Could not connect to Mandos server")
357
print("Could not connect to Mandos server", file=sys.stderr)
368
360
mandos_serv = dbus.Interface(mandos_dbus_objc,
387
379
os.dup2(stderrcopy, sys.stderr.fileno())
388
380
os.close(stderrcopy)
389
381
except dbus.exceptions.DBusException as e:
390
log.critical("Failed to access Mandos server through D-Bus:"
382
print("Access denied: "
383
"Accessing mandos server through D-Bus: {}".format(e),
394
387
# Compile dict of (clients: properties) to process
405
398
clients[client_objc] = client
408
log.critical("Client not found on server: %r", name)
401
print("Client not found on server: {!r}"
402
.format(name), file=sys.stderr)
411
405
if not has_actions(options) and clients:
499
493
client.Approve(dbus.Boolean(False),
500
494
dbus_interface=client_interface)
503
class Test_milliseconds_to_string(unittest.TestCase):
505
self.assertEqual(milliseconds_to_string(93785000),
507
def test_no_days(self):
508
self.assertEqual(milliseconds_to_string(7385000), "02:03:05")
509
def test_all_zero(self):
510
self.assertEqual(milliseconds_to_string(0), "00:00:00")
511
def test_no_fractional_seconds(self):
512
self.assertEqual(milliseconds_to_string(400), "00:00:00")
513
self.assertEqual(milliseconds_to_string(900), "00:00:00")
514
self.assertEqual(milliseconds_to_string(1900), "00:00:01")
517
def should_only_run_tests():
518
parser = argparse.ArgumentParser(add_help=False)
519
parser.add_argument("--check", action='store_true')
520
args, unknown_args = parser.parse_known_args()
521
run_tests = args.check
523
# Remove --check argument from sys.argv
524
sys.argv[1:] = unknown_args
527
# Add all tests from doctest strings
528
def load_tests(loader, tests, none):
530
tests.addTests(doctest.DocTestSuite())
533
497
if __name__ == "__main__":
534
if should_only_run_tests():
535
# Call using ./tdd-python-script --check [--verbose]