@@ -163,14 +163,15 @@ def get_active_network(namespace):
163163
164164@click .command (context_settings = {"ignore_unknown_options" : True })
165165@click .argument ("scenario_file" , type = click .Path (exists = True , file_okay = True , dir_okay = False ))
166+ @click .option ("--source_dir" , type = click .Path (exists = True , file_okay = False , dir_okay = True ), required = False )
166167@click .argument ("additional_args" , nargs = - 1 , type = click .UNPROCESSED )
167- def run (scenario_file : str , additional_args : tuple [str ]):
168+ def run (scenario_file : str , source_dir , additional_args : tuple [str ]):
168169 """
169170 Run a scenario from a file.
170171 Pass `-- --help` to get individual scenario help
171172 """
172173 scenario_path = Path (scenario_file ).resolve ()
173- scenario_dir = scenario_path .parent
174+ scenario_dir = scenario_path .parent if not source_dir else Path ( source_dir ). resolve ()
174175 scenario_name = scenario_path .stem
175176
176177 if additional_args and ("--help" in additional_args or "-h" in additional_args ):
@@ -203,15 +204,24 @@ def run(scenario_file: str, additional_args: tuple[str]):
203204 def filter (path ):
204205 if any (needle in str (path ) for needle in [".pyc" , ".csv" , ".DS_Store" ]):
205206 return False
206- return any (
207- needle in str (path ) for needle in ["commander.py" , "test_framework" , scenario_name ]
208- )
209-
207+ if any (needle in str (path ) for needle in ["__init__.py" , "commander.py" , "test_framework" , scenario_path .name ]):
208+ print (f"Including: { path } " )
209+ return True
210+ return False
211+
212+ # In case the scenario file is not in the root of the archive directory,
213+ # we need to specify its relative path as a submodule
214+ # First get the path of the file relative to the source directory
215+ relative_path = scenario_path .relative_to (scenario_dir )
216+ # Remove the '.py' extension
217+ relative_name = relative_path .with_suffix ("" )
218+ # Replace path separators with dots and pray the user included __init__.py
219+ module_name = "." .join (relative_name .parts )
210220 # Compile python archive
211221 zipapp .create_archive (
212222 source = scenario_dir ,
213223 target = archive_buffer ,
214- main = f"{ scenario_name } :main" ,
224+ main = f"{ module_name } :main" ,
215225 compressed = True ,
216226 filter = filter ,
217227 )
0 commit comments