Coverage for lbutils.py: 89%

47 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-07-10 13:43 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2021 RSK Labs Ltd 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy of 

6# this software and associated documentation files (the "Software"), to deal in 

7# the Software without restriction, including without limitation the rights to 

8# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 

9# of the Software, and to permit persons to whom the Software is furnished to do 

10# so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in all 

13# copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 

21# SOFTWARE. 

22 

23import ast 

24import contextlib 

25import io 

26import sys 

27 

28 

29def post_process_list_apps(raw_output): 

30 app_list = [] 

31 processed_output = "" 

32 for line in raw_output.splitlines(): 

33 line = line.strip() 

34 if line.startswith("[{") and line.endswith("}]"): 

35 data = ast.literal_eval(line) 

36 if isinstance(data, list) and all( 

37 isinstance(app_dict, dict) for app_dict in data): 

38 for app_dict in data: 

39 app_list.append(app_dict["name"]) 

40 break 

41 if app_list: 

42 processed_output = "\n".join(app_list) 

43 return processed_output 

44 

45 

46def main(): 

47 import runpy 

48 

49 utilities = { 

50 "load": {"module": "loadApp", "post_process": None}, 

51 "delete": {"module": "deleteApp", "post_process": None}, 

52 "setupCA": {"module": "setupCustomCA", "post_process": None}, 

53 "resetCA": {"module": "resetCustomCA", "post_process": None}, 

54 "genCA": {"module": "genCAPair", "post_process": None}, 

55 "listApps": {"module": "listApps", "post_process": post_process_list_apps}, 

56 } 

57 

58 if len(sys.argv) < 2 or sys.argv[1] not in utilities: 

59 commands = ", ".join(utilities.keys()) 

60 print("Ledgerblue utilities") 

61 print(f"usage: {sys.argv[0]} { {commands}} [options]") 

62 sys.exit(99) 

63 

64 try: 

65 command = sys.argv[1] 

66 sys.argv = [f"{sys.argv[0]} {command}"] + sys.argv[2:] 

67 module = f"ledgerblue.{utilities[command]['module']}" 

68 post_process = utilities[command]["post_process"] 

69 

70 buffer = io.StringIO() 

71 with contextlib.redirect_stdout(buffer): 

72 with contextlib.suppress(UnicodeDecodeError): 

73 runpy.run_module(module, run_name="__main__") 

74 output = buffer.getvalue() 

75 buffer.close() 

76 if post_process: 

77 output = post_process(output) 

78 if output: 

79 print(output) 

80 sys.exit(0) 

81 except Exception as e: 

82 print(f"Error: {str(e)}") 

83 sys.exit(1) 

84 

85 

86if __name__ == "__main__": 

87 main()