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

46 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-05 14:48 +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.platform import CommandName, COMMAND_DEPENDENCIES 

13from sparkle.CLI.initialise import check_for_initialise 

14from sparkle.CLI.help import argparse_custom as ac 

15from sparkle.CLI.help.nicknames import resolve_object_name 

16 

17 

18def parser_function() -> argparse.ArgumentParser: 

19 """Define the command line arguments.""" 

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

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

22 **ac.SolverRemoveArgument.kwargs) 

23 return parser 

24 

25 

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

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

28 # Log command call 

29 sl.log_command(sys.argv) 

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(args.solver, 

37 gv.solver_nickname_mapping, 

38 gv.settings().DEFAULT_solver_dir) 

39 

40 check_for_initialise(COMMAND_DEPENDENCIES[CommandName.REMOVE_SOLVER]) 

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.solver_nickname_mapping 

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 sfh.add_remove_platform_item( 

59 nickname, 

60 gv.solver_nickname_list_path, 

61 gv.file_storage_data_mapping[gv.solver_nickname_list_path], 

62 remove=True) 

63 

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

65 performance_data = PerformanceDataFrame( 

66 gv.settings().DEFAULT_performance_data_path) 

67 if solver_path.name in performance_data.dataframe.columns: 

68 performance_data.remove_solver(solver_path.name) 

69 performance_data.save_csv() 

70 

71 shutil.rmtree(solver_path) 

72 

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

74 sys.exit(0) 

75 

76 

77if __name__ == "__main__": 

78 main(sys.argv[1:])