Coverage for sparkle/CLI/remove_instances.py: 91%

45 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 an instance set from the Sparkle platform.""" 

3 

4import sys 

5import argparse 

6import shutil 

7 

8from sparkle.CLI.help import global_variables as gv 

9from sparkle.platform import file_help as sfh 

10from sparkle.structures import FeatureDataFrame, PerformanceDataFrame 

11from sparkle.instance import Instance_Set 

12from sparkle.CLI.help import logging as sl 

13from sparkle.platform import CommandName, COMMAND_DEPENDENCIES 

14from sparkle.CLI.initialise import check_for_initialise 

15from sparkle.CLI.help import argparse_custom as ac 

16from sparkle.CLI.help.nicknames import resolve_object_name 

17 

18 

19def parser_function() -> argparse.ArgumentParser: 

20 """Define the command line arguments.""" 

21 parser = argparse.ArgumentParser(description="Remove instances from the platform.") 

22 parser.add_argument(*ac.InstancesPathRemoveArgument.names, 

23 **ac.InstancesPathRemoveArgument.kwargs) 

24 return parser 

25 

26 

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

28 """Main function of the remove instances command.""" 

29 # Log command call 

30 sl.log_command(sys.argv) 

31 

32 # Define command line arguments 

33 parser = parser_function() 

34 

35 # Process command line arguments 

36 args = parser.parse_args(argv) 

37 instances_path = resolve_object_name(args.instances_path, 

38 target_dir=gv.settings().DEFAULT_instance_dir) 

39 

40 check_for_initialise(COMMAND_DEPENDENCIES[CommandName.REMOVE_INSTANCES]) 

41 

42 if instances_path is None or not instances_path.exists() or not\ 

43 instances_path.is_dir(): 

44 print(f'Could not resolve instances path arg "{args.instances_path}"!') 

45 print("Check that the path or nickname is spelled correctly.") 

46 sys.exit(-1) 

47 

48 print(f"Start removing all instances in directory {instances_path} ...") 

49 old_instance_set = Instance_Set(instances_path) 

50 # Remove from feature data and performance data 

51 feature_data = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path) 

52 performance_data = PerformanceDataFrame(gv.settings().DEFAULT_performance_data_path) 

53 for instance in old_instance_set.instance_paths: 

54 feature_data.remove_instances(str(instance)) 

55 performance_data.remove_instance(str(instance)) 

56 

57 feature_data.save_csv() 

58 performance_data.save_csv() 

59 

60 # Remove nickname, if it exists 

61 instances_nicknames = gv.file_storage_data_mapping[gv.instances_nickname_path] 

62 for key in instances_nicknames: 

63 if instances_nicknames[key] == instances_path: 

64 sfh.add_remove_platform_item(instances_path, 

65 gv.instances_nickname_path, 

66 key=key, remove=True) 

67 break 

68 

69 # Remove the directory and all its files 

70 shutil.rmtree(instances_path) 

71 

72 print(f"Removing instances in directory {instances_path} done!") 

73 sys.exit(0) 

74 

75 

76if __name__ == "__main__": 

77 main(sys.argv[1:])