Coverage for MC6809/dev_shell.py: 91%

50 statements  

« prev     ^ index     » next       coverage.py v7.2.1, created at 2023-03-06 19:50 +0100

1import argparse 

2import cProfile 

3import pstats 

4from pathlib import Path 

5 

6import cmd2 

7from cmd2 import Cmd2ArgumentParser, with_argparser 

8from dev_shell.base_cmd2_app import DevShellBaseApp, run_cmd2_app 

9from dev_shell.command_sets import DevShellBaseCommandSet 

10from dev_shell.command_sets.dev_shell_commands import DevShellCommandSet as OriginDevShellCommandSet 

11from dev_shell.config import DevShellConfig 

12from dev_shell.utils.colorful import bright_yellow 

13from dev_shell.utils.subprocess_utils import verbose_check_call 

14from poetry_publish.publish import poetry_publish 

15 

16import MC6809 

17from MC6809.core.bechmark import run_benchmark 

18 

19 

20PACKAGE_ROOT = Path(MC6809.__file__).parent.parent.parent 

21 

22 

23@cmd2.with_default_category('MC6809 commands') 

24class MC6809CommandSet(DevShellBaseCommandSet): 

25 benchmark_argparser = Cmd2ArgumentParser() 

26 benchmark_argparser.add_argument( 

27 '--loops', 

28 type=int, 

29 default=6, 

30 help='How many benchmark loops should be run? (default: %(default)s)', 

31 ) 

32 benchmark_argparser.add_argument( 

33 '--multiply', 

34 type=int, 

35 default=15, 

36 help='Test data multiplier (default: %(default)s)', 

37 ) 

38 

39 @with_argparser(benchmark_argparser) 

40 def do_benchmark(self, args: argparse.Namespace): 

41 """ 

42 Run a MC6809 emulation benchmark 

43 """ 

44 run_benchmark(loops=args.loops, multiply=args.multiply) 

45 

46 @with_argparser(benchmark_argparser) 

47 def do_profile(self, args: argparse.Namespace): 

48 """ 

49 Profile the MC6809 emulation benchmark 

50 """ 

51 with cProfile.Profile() as pr: 

52 run_benchmark(loops=args.loops, multiply=args.multiply) 

53 pstats.Stats(pr).sort_stats('tottime', 'cumulative', 'calls').print_stats(20) 

54 

55 

56class DevShellCommandSet(OriginDevShellCommandSet): 

57 def do_publish(self, statement: cmd2.Statement): 

58 """ 

59 Publish "dev-shell" to PyPi 

60 """ 

61 # don't publish if code style wrong: 

62 verbose_check_call('darker', '--check') 

63 

64 # don't publish if test fails: 

65 verbose_check_call('pytest', '-x') 

66 

67 poetry_publish( 

68 package_root=PACKAGE_ROOT, 

69 version=MC6809.__version__, 

70 creole_readme=False, 

71 ) 

72 

73 

74class DevShellApp(DevShellBaseApp): 

75 # Remove some commands: 

76 delattr(cmd2.Cmd, 'do_edit') 

77 delattr(cmd2.Cmd, 'do_shell') 

78 delattr(cmd2.Cmd, 'do_run_script') 

79 delattr(cmd2.Cmd, 'do_run_pyscript') 

80 

81 def __init__(self, *args, **kwargs): 

82 super().__init__(*args, **kwargs) 

83 self.intro = f'Developer shell - {bright_yellow("MC6809")} - v{MC6809.__version__}\n' 

84 

85 

86def get_devshell_app_kwargs(): 

87 """ 

88 Generate the kwargs for the cmd2 App. 

89 (Separated because we needs the same kwargs in tests) 

90 """ 

91 config = DevShellConfig(package_module=MC6809) 

92 

93 # initialize all CommandSet() with context: 

94 kwargs = dict(config=config) 

95 

96 app_kwargs = dict( 

97 config=config, 

98 command_sets=[ 

99 MC6809CommandSet(**kwargs), 

100 DevShellCommandSet(**kwargs), 

101 ], 

102 ) 

103 return app_kwargs 

104 

105 

106def devshell_cmdloop(): 

107 """ 

108 Entry point to start the "dev-shell" cmd2 app. 

109 Used in: [tool.poetry.scripts] 

110 """ 

111 app = DevShellApp(**get_devshell_app_kwargs()) 

112 run_cmd2_app(app) # Run a cmd2 App as CLI or shell