Coverage for sparkle/CLI/remove_solver.py: 77%

47 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-07 15:22 +0000

1#!/usr/bin/env python3 

2"""Sparkle command to remove a solver from the Sparkle platform.""" 

3 

4import sys 

5import argparse 

6import shutil 

7 

8from sparkle.platform import file_help as sfh 

9from sparkle.CLI.help import global_variables as gv 

10from sparkle.structures import PerformanceDataFrame 

11from sparkle.CLI.help import logging as sl 

12from sparkle.CLI.initialise import check_for_initialise 

13from sparkle.CLI.help import argparse_custom as ac 

14from sparkle.CLI.help.nicknames import resolve_object_name 

15 

16 

17def parser_function() -> argparse.ArgumentParser: 

18 """Define the command line arguments.""" 

19 parser = argparse.ArgumentParser(description="Remove a solver from the platform.") 

20 parser.add_argument(*ac.SolverRemoveArgument.names, 

21 **ac.SolverRemoveArgument.kwargs) 

22 return parser 

23 

24 

25def main(argv: list[str]) -> None: 

26 """Main function of the remove solver command.""" 

27 # Log command call 

28 sl.log_command(sys.argv) 

29 check_for_initialise() 

30 

31 # Define command line arguments 

32 parser = parser_function() 

33 

34 # Process command line arguments 

35 args = parser.parse_args(argv) 

36 solver_path = resolve_object_name( 

37 args.solver, 

38 gv.file_storage_data_mapping[gv.solver_nickname_list_path], 

39 gv.settings().DEFAULT_solver_dir) 

40 

41 if solver_path is None: 

42 print(f'Could not resolve Solver path/name "{solver_path}"!') 

43 sys.exit(-1) 

44 if not solver_path.parent == gv.settings().DEFAULT_solver_dir: 

45 # Allow user to only specify solvers in Sparkle solver dir 

46 print("Specified Solver is not is platform directory! Exiting.") 

47 sys.exit(-1) 

48 

49 print(f"Start removing solver {solver_path.name} ...") 

50 

51 solver_nickname_mapping = gv.file_storage_data_mapping[gv.solver_nickname_list_path] 

52 if len(solver_nickname_mapping): 

53 nickname = None 

54 for key in solver_nickname_mapping: 

55 if solver_nickname_mapping[key] == str(solver_path): 

56 nickname = key 

57 break 

58 

59 sfh.add_remove_platform_item( 

60 solver_path, 

61 gv.solver_nickname_list_path, 

62 gv.file_storage_data_mapping[gv.solver_nickname_list_path], 

63 key=nickname, 

64 remove=True) 

65 

66 if gv.settings().DEFAULT_performance_data_path.exists(): 

67 performance_data = PerformanceDataFrame( 

68 gv.settings().DEFAULT_performance_data_path) 

69 if str(solver_path) in performance_data.solvers: 

70 performance_data.remove_solver(str(solver_path)) 

71 performance_data.save_csv() 

72 

73 # We unlink symbolics links, erase copies 

74 if solver_path.is_symlink(): 

75 solver_path.unlink() 

76 else: 

77 # Remove the directory and all its files 

78 shutil.rmtree(solver_path) 

79 

80 print(f"Removing solver {solver_path.name} done!") 

81 sys.exit(0) 

82 

83 

84if __name__ == "__main__": 

85 main(sys.argv[1:])