@@ -172,14 +172,15 @@ def get_active_network(namespace):
172172 default = False ,
173173 help = "Stream scenario output and delete container when stopped" ,
174174)
175+ @click .option ("--source_dir" , type = click .Path (exists = True , file_okay = False , dir_okay = True ), required = False )
175176@click .argument ("additional_args" , nargs = - 1 , type = click .UNPROCESSED )
176- def run (scenario_file : str , debug : bool , additional_args : tuple [str ]):
177+ def run (scenario_file : str , source_dir , additional_args : tuple [str ]):
177178 """
178179 Run a scenario from a file.
179180 Pass `-- --help` to get individual scenario help
180181 """
181182 scenario_path = Path (scenario_file ).resolve ()
182- scenario_dir = scenario_path .parent
183+ scenario_dir = scenario_path .parent if not source_dir else Path ( source_dir ). resolve ()
183184 scenario_name = scenario_path .stem
184185
185186 if additional_args and ("--help" in additional_args or "-h" in additional_args ):
@@ -212,15 +213,24 @@ def run(scenario_file: str, debug: bool, additional_args: tuple[str]):
212213 def filter (path ):
213214 if any (needle in str (path ) for needle in [".pyc" , ".csv" , ".DS_Store" ]):
214215 return False
215- return any (
216- needle in str (path ) for needle in ["commander.py" , "test_framework" , scenario_name ]
217- )
218-
216+ if any (needle in str (path ) for needle in ["__init__.py" , "commander.py" , "test_framework" , scenario_path .name ]):
217+ print (f"Including: { path } " )
218+ return True
219+ return False
220+
221+ # In case the scenario file is not in the root of the archive directory,
222+ # we need to specify its relative path as a submodule
223+ # First get the path of the file relative to the source directory
224+ relative_path = scenario_path .relative_to (scenario_dir )
225+ # Remove the '.py' extension
226+ relative_name = relative_path .with_suffix ("" )
227+ # Replace path separators with dots and pray the user included __init__.py
228+ module_name = "." .join (relative_name .parts )
219229 # Compile python archive
220230 zipapp .create_archive (
221231 source = scenario_dir ,
222232 target = archive_buffer ,
223- main = f"{ scenario_name } :main" ,
233+ main = f"{ module_name } :main" ,
224234 compressed = True ,
225235 filter = filter ,
226236 )
0 commit comments