Coverage for sparkle/CLI/add_instances.py: 93%
56 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 add an instance set to the Sparkle platform."""
4import sys
5import argparse
6from pathlib import Path
7import shutil
9from sparkle.CLI.help import global_variables as gv
10from sparkle.platform import file_help as sfh
11from sparkle.instance import Instance_Set
12from sparkle.structures import FeatureDataFrame, PerformanceDataFrame
13from sparkle.CLI.help import logging as sl
15from sparkle.CLI.initialise import check_for_initialise
16from sparkle.CLI.help import argparse_custom as ac
19def parser_function() -> argparse.ArgumentParser:
20 """Define the command line arguments."""
21 parser = argparse.ArgumentParser(description="Add instances to the platform.")
22 parser.add_argument(*ac.InstancesPathArgument.names,
23 **ac.InstancesPathArgument.kwargs)
24 parser.add_argument(*ac.NicknameInstanceSetArgument.names,
25 **ac.NicknameInstanceSetArgument.kwargs)
26 parser.add_argument(*ac.NoCopyArgument.names,
27 **ac.NoCopyArgument.kwargs)
28 return parser
31def main(argv: list[str]) -> None:
32 """Main function of the add instances command."""
33 # Log command call
34 sl.log_command(sys.argv)
36 # Define command line arguments
37 parser = parser_function()
38 check_for_initialise()
40 # Process command line arguments
41 args = parser.parse_args(argv)
42 instances_source = Path(args.instances_path)
43 instances_target = gv.settings().DEFAULT_instance_dir / instances_source.name
45 if not instances_source.exists():
46 print(f'Instance set path "{instances_source}" does not exist!')
47 sys.exit(-1)
48 if instances_target.exists():
49 print(f'Instance set "{instances_source.name}" already exists in Sparkle! '
50 "Exiting...")
51 sys.exit(-1)
52 if args.nickname is not None:
53 sfh.add_remove_platform_item(
54 instances_target,
55 gv.instances_nickname_path,
56 gv.file_storage_data_mapping[gv.instances_nickname_path],
57 key=args.nickname)
59 print(f"Start adding all instances in directory {instances_source} ...")
60 new_instance_set = Instance_Set(instances_source)
62 if args.no_copy:
63 print(f"Creating symbolic link from {instances_source} to {instances_target}...")
64 instances_target.symlink_to(instances_source.absolute())
65 else:
66 instances_target.mkdir(parents=True)
67 print("Copying files...")
68 for instance_path_source in new_instance_set.all_paths:
69 print(f"Copying {instance_path_source} to {instances_target}...", end="\r")
70 shutil.copy(instance_path_source, instances_target)
71 print("\nCopying done!")
72 # Refresh the instance set as the target instance set
73 new_instance_set = Instance_Set(instances_target)
75 # Add the instances to the Feature Data / Performance Data
76 feature_data = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path)
77 # When adding instances, an empty performance DF has no objectives yet
78 performance_data = PerformanceDataFrame(
79 gv.settings().DEFAULT_performance_data_path,
80 objectives=gv.settings().get_general_sparkle_objectives())
81 for instance_path in new_instance_set.instance_paths:
82 # Construct a name path due to multi-file instances
83 feature_data.add_instances(str(instance_path))
84 performance_data.add_instance(str(instance_path))
85 feature_data.save_csv()
86 performance_data.save_csv()
88 print(f"\nAdding instance set {new_instance_set.name} done!")
90 # Write used settings to file
91 gv.settings().write_used_settings()
92 sys.exit(0)
95if __name__ == "__main__":
96 main(sys.argv[1:])