Coverage for sparkle/CLI/remove_instances.py: 91%
46 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-01-07 15:22 +0000
« 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 an instance set from the Sparkle platform."""
4import sys
5import argparse
6import shutil
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.CLI.initialise import check_for_initialise
14from sparkle.CLI.help import argparse_custom as ac
15from sparkle.CLI.help.nicknames import resolve_object_name
18def parser_function() -> argparse.ArgumentParser:
19 """Define the command line arguments."""
20 parser = argparse.ArgumentParser(description="Remove instances from the platform.")
21 parser.add_argument(*ac.InstancesPathRemoveArgument.names,
22 **ac.InstancesPathRemoveArgument.kwargs)
23 return parser
26def main(argv: list[str]) -> None:
27 """Main function of the remove instances command."""
28 # Log command call
29 sl.log_command(sys.argv)
30 check_for_initialise()
32 # Define command line arguments
33 parser = parser_function()
35 # Process command line arguments
36 args = parser.parse_args(argv)
37 instances_path = resolve_object_name(
38 args.instances_path,
39 nickname_dict=gv.file_storage_data_mapping[gv.instances_nickname_path],
40 target_dir=gv.settings().DEFAULT_instance_dir)
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)
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))
57 feature_data.save_csv()
58 performance_data.save_csv()
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
69 # We unlink symbolics links, erase copies
70 if instances_path.is_symlink():
71 instances_path.unlink()
72 else:
73 # Remove the directory and all its files
74 shutil.rmtree(instances_path)
76 print(f"Removing instances set {instances_path.name} done!")
77 sys.exit(0)
80if __name__ == "__main__":
81 main(sys.argv[1:])