Coverage for src/sparkle/CLI/remove_instances.py: 92%
48 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-08 12:00 +0000
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-08 12:00 +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
16from sparkle.CLI.help import jobs as jobs_help
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(
23 *ac.InstancesPathRemoveArgument.names, **ac.InstancesPathRemoveArgument.kwargs
24 )
25 return parser
28def main(argv: list[str]) -> None:
29 """Main function of the remove instances command."""
30 # Log command call
31 sl.log_command(sys.argv, gv.settings().random_state)
32 check_for_initialise()
34 # Define command line arguments
35 parser = parser_function()
37 # Process command line arguments
38 args = parser.parse_args(argv)
39 instances_path = resolve_object_name(
40 args.instances_path,
41 nickname_dict=gv.file_storage_data_mapping[gv.instances_nickname_path],
42 target_dir=gv.settings().DEFAULT_instance_dir,
43 )
45 if (
46 instances_path is None
47 or not instances_path.exists()
48 or not instances_path.is_dir()
49 ):
50 print(f'Could not resolve instances path arg "{args.instances_path}"!')
51 print("Check that the path or nickname is spelled correctly.")
52 sys.exit(-1)
54 jobs_help.check_running_waiting_jobs(
55 gv.settings().DEFAULT_log_output,
56 )
58 print(f"Start removing all instances in directory {instances_path} ...")
59 old_instance_set = Instance_Set(instances_path)
60 # Remove from feature data and performance data
61 feature_data = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path)
62 performance_data = PerformanceDataFrame(gv.settings().DEFAULT_performance_data_path)
63 instance_pairs = old_instance_set.instance_pairs
64 feature_data.remove_instance(instance_pairs)
65 performance_data.remove_instance(instance_pairs)
67 feature_data.save_csv()
68 performance_data.save_csv()
70 # Remove nickname, if it exists
71 instances_nicknames = gv.file_storage_data_mapping[gv.instances_nickname_path]
72 for key in instances_nicknames:
73 if instances_nicknames[key] == instances_path:
74 sfh.add_remove_platform_item(
75 instances_path, gv.instances_nickname_path, key=key, remove=True
76 )
77 break
79 # We unlink symbolics links, erase copies
80 if instances_path.is_symlink():
81 instances_path.unlink()
82 else:
83 # Remove the directory and all its files
84 shutil.rmtree(instances_path)
86 print(f"Removing instances set {instances_path.name} done!")
87 sys.exit(0)
90if __name__ == "__main__":
91 main(sys.argv[1:])