Coverage for aiocoap / cli / defaults.py: 89%

38 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-29 12:32 +0000

1# SPDX-FileCopyrightText: Christian Amsüss and the aiocoap contributors 

2# 

3# SPDX-License-Identifier: MIT 

4 

5"""This helper script can be used to easily inspect aiocoap's environment 

6autodetection (ie. whether all modules required for particular subsystems are 

7available, loosely corresponding to its extras (as described in pyproject.toml 

8or the installation documentation). 

9 

10Run it as `python3 -m aiocoap.cli.defaults`.""" 

11 

12import socket 

13import sys 

14from aiocoap.meta import version 

15from aiocoap.defaults import ( 

16 has_reuse_port, 

17 get_default_clienttransports, 

18 get_default_servertransports, 

19 missing_module_functions, 

20) 

21import argparse 

22import os 

23 

24 

25def describe_ai_addrconfig_v6(): 

26 try: 

27 addrs = socket.getaddrinfo( 

28 "::1", 

29 1234, 

30 family=socket.AF_UNSPEC, 

31 type=socket.SOCK_DGRAM, 

32 proto=socket.IPPROTO_UDP, 

33 flags=socket.AI_ADDRCONFIG, 

34 ) 

35 return f"returned {len(addrs)} result(s)" 

36 except socket.gaierror: 

37 return "raises gaierror" 

38 

39 

40def main(argv=None): 

41 p = argparse.ArgumentParser(description=__doc__) 

42 # Allow passing this in as AIOCOAP_DEFAULTS_EXPECT_ALL=1 via the 

43 # environment, as that's easier to set in tox 

44 p.add_argument( 

45 "--expect-all", 

46 help="Exit with an error unless all subsystems are available", 

47 action="store_true", 

48 default=os.environ.get("AIOCOAP_DEFAULTS_EXPECT_ALL") == "1", 

49 ) 

50 p.add_argument("--version", action="version", version=version) 

51 args = p.parse_args(sys.argv[1:] if argv is None else argv) 

52 

53 error = 0 

54 

55 print("Python version: %s" % sys.version) 

56 print("aiocoap version: %s" % version) 

57 print("Modules missing for subsystems:") 

58 for name, f in missing_module_functions.items(): 

59 missing = f() 

60 if missing and args.expect_all: 

61 error = 1 

62 print( 

63 " %s: %s" 

64 % ( 

65 name, 

66 "everything there" if not missing else "missing " + ", ".join(missing), 

67 ) 

68 ) 

69 print("Python platform: %s" % sys.platform) 

70 print( 

71 "Default server transports: %s" 

72 % ":".join(get_default_servertransports(use_env=False)) 

73 ) 

74 print("Selected server transports: %s" % ":".join(get_default_servertransports())) 

75 print( 

76 "Default client transports: %s" 

77 % ":".join(get_default_clienttransports(use_env=False)) 

78 ) 

79 print("Selected client transports: %s" % ":".join(get_default_clienttransports())) 

80 print( 

81 "SO_REUSEPORT available (default, selected): %s, %s" 

82 % (has_reuse_port(use_env=False), has_reuse_port()) 

83 ) 

84 print( 

85 "Behavior of real getaddrinfo('::1', …, AI_ADDRCONFIG) is:", 

86 describe_ai_addrconfig_v6(), 

87 ) 

88 

89 if error: 

90 print( 

91 "Exiting unsuccessfully because --expect-all was set and not all extras are available." 

92 ) 

93 return error 

94 

95 

96if __name__ == "__main__": 

97 sys.exit(main())