1717from pathlib import Path
1818from typing import Dict , List , Optional , Tuple , Type , Union
1919
20- from ..subpackages import load_subpackages
21- from ..utils import logging
22- from .base import BaseOptimumCLICommand , CommandInfo , RootOptimumCLICommand
23- from .env import EnvironmentCommand
24- from .export import ExportCommand
20+ from optimum .commands .base import BaseOptimumCLICommand , CommandInfo , RootOptimumCLICommand
21+ from optimum .commands .env import EnvironmentCommand
22+ from optimum .commands .export .base import ExportCommand
23+ from optimum .utils import logging
2524
2625
2726logger = logging .get_logger ()
@@ -110,29 +109,37 @@ def dynamic_load_commands_in_register() -> (
110109):
111110 """
112111 Loads a list of command classes to register to the CLI from the `optimum/commands/register/` directory.
113- It will look in any python file if there is a `REGISTER_COMMANDS` list, and load the commands to register
114- accordingly.
115- At this point, nothing is actually registered in the root CLI.
112+
116113
117114 Returns:
118115 `List[Tuple[Union[Type[BaseOptimumCLICommand], CommandInfo], Optional[Type[BaseOptimumCLICommand]]]]`: A list
119116 of tuples with two elements. The first element corresponds to the command to register, it can either be a
120117 subclass of `BaseOptimumCLICommand` or a `CommandInfo`. The second element corresponds to the parent command,
121118 where `None` means that the parent command is the root CLI command.
122119 """
123- commands_to_register = []
124- register_dir_path = Path (__file__ ).parent / "register"
125- for filename in register_dir_path .iterdir ():
126- if filename .is_dir () or filename .suffix != ".py" :
127- if filename .name not in ["__pycache__" , "README.md" ]:
128- logger .warning (
129- f"Skipping { filename } because only python files are allowed when registering commands dynamically."
130- )
120+ registration_files = []
121+ for dist in importlib .metadata .distributions ():
122+ if dist .metadata ["Name" ] is None :
123+ continue
124+ if dist .metadata ["Name" ] == "optimum-benchmark" :
125+ continue
126+ if not dist .metadata ["Name" ].startswith ("optimum-" ):
131127 continue
132- module_name = f".register.{ filename .stem } "
133- module = importlib .import_module (module_name , package = "optimum.commands" )
134- commands_to_register_in_file = getattr (module , "REGISTER_COMMANDS" , [])
135- for command_idx , command in enumerate (commands_to_register_in_file ):
128+
129+ dist_name = dist .metadata ["Name" ]
130+ dist_module_name = dist_name .replace ("-" , "." )
131+ dist_module = importlib .import_module (dist_module_name )
132+ dist_module_path = Path (dist_module .__file__ ).parent
133+ commands_register_path = dist_module_path .parent / "commands" / "register"
134+ for file in commands_register_path .iterdir ():
135+ if file .suffix == ".py" and file .stem != "__init__" :
136+ registration_files .append (file .stem )
137+
138+ commands_to_register = []
139+ for register_file in registration_files :
140+ submodule = importlib .import_module (f"optimum.commands.register.{ register_file } " )
141+ commands_to_register_in_module = getattr (submodule , "REGISTER_COMMANDS" , [])
142+ for command_idx , command in enumerate (commands_to_register_in_module ):
136143 if isinstance (command , tuple ):
137144 command_or_command_info , parent_command_cls = command
138145 else :
@@ -143,9 +150,10 @@ def dynamic_load_commands_in_register() -> (
143150 command_or_command_info , BaseOptimumCLICommand
144151 ):
145152 raise ValueError (
146- f"The command at index { command_idx } in { filename } is not of the right type: { type (command_or_command_info )} ."
153+ f"The command at index { command_idx } in the `commands.register` module of { dist_name } is not of the right type: { type (command_or_command_info )} ."
147154 )
148155 commands_to_register .append ((command_or_command_info , parent_command_cls ))
156+
149157 return commands_to_register
150158
151159
@@ -181,9 +189,6 @@ def main():
181189 for subcommand_cls in OPTIMUM_CLI_ROOT_SUBCOMMANDS :
182190 register_optimum_cli_subcommand (subcommand_cls , parent_command = root )
183191
184- # Load subpackages to give them a chance to declare their own subcommands
185- load_subpackages ()
186-
187192 # Register subcommands declared by the subpackages or found in the register files under commands/register
188193 commands_to_register = _OPTIMUM_CLI_SUBCOMMANDS + dynamic_load_commands_in_register ()
189194 command2command_instance = resolve_command_to_command_instance (
0 commit comments