@@ -193,19 +193,50 @@ def ls_recipe():
193193 print (get_recipes ())
194194
195195
196- def uninstall_recipe (module_name : str ):
196+ def uninstall_recipe (wf_name : str ,
197+ savedir : pathlib .Path = this_dir .joinpath ("recipes" )):
197198 """
198199 Uninstalls a recipe installed in the system.
199200 """
200201
201- for entry_point in pkg_resources .iter_entry_points ('workflow_recipes' ):
202- if entry_point .module_name == module_name :
203- print (f"Uninstalling package: wfchef.recipe.{ module_name } " )
204- proc = subprocess .Popen (["pip" , "uninstall" , f"wfchef.recipe.{ module_name } " ])
205- proc .wait ()
206- return
207-
208- print (f"Could not find recipe with module name { module_name } installed" )
202+ dst = pathlib .Path (savedir , f"{ savedir .stem } _recipes" , wf_name ).resolve ()
203+ try :
204+ # Removing package from setup.py
205+ with this_dir .joinpath (dst .parent .parent .joinpath ("setup.py" )).open ("r" ) as fp :
206+ setup_str = fp .read ()
207+
208+ # Find and remove the specific line that added the package
209+ for line in setup_str .split ("\n " ):
210+ if wf_name in setup_str :
211+ setup_str = setup_str .replace (line , "" )
212+
213+ with this_dir .joinpath (dst .parent .parent .joinpath ("setup.py" )).open ("w" ) as fp :
214+ fp .write (setup_str )
215+
216+ # Removing the import line from __init__.py
217+ init_file = dst .parent .joinpath ("__init__.py" )
218+ if init_file .exists ():
219+ with init_file .open ("r" ) as fp :
220+ init_str = fp .read ()
221+
222+ # Remove the line that imports the package
223+
224+ for line in init_str .split ("\n " ):
225+ if wf_name in line :
226+ init_str = init_str .replace (line , "" )
227+ break
228+
229+ with init_file .open ("w" ) as fp :
230+ fp .write (init_str )
231+ # for entry_point in pkg_resources.iter_entry_points('workflow_recipes'):
232+ # if entry_point.module_name == module_name:
233+ # print(f"Uninstalling package: {module_name}")
234+ # proc = subprocess.Popen(["pip", "uninstall", module_name])
235+ # proc.wait()
236+ # return
237+ except Exception as e :
238+ traceback .print_exc ()
239+ # print(f"Could not find recipe with module name {module_name} installed")
209240
210241
211242def create_recipe (path_to_instances : Union [str , pathlib .Path ],
@@ -300,7 +331,9 @@ def get_parser() -> argparse.ArgumentParser:
300331
301332 uninstall_parser = subparsers .add_parser ("uninstall" )
302333 uninstall_parser .set_defaults (action = uninstall_recipe )
303- uninstall_parser .add_argument ("module_name" , help = "name of recipe module to uninstall" )
334+ # uninstall_parser.add_argument("module_name", help="name of recipe module to uninstall")
335+ uninstall_parser .add_argument ("-n" , "--name" , help = "name of the workflow to uninstall" )
336+ uninstall_parser .add_argument ("-o" , "--out" , help = "directory where the recipe is located" )
304337
305338 create_parser = subparsers .add_parser ("create" )
306339 create_parser .set_defaults (action = create_recipe )
@@ -352,7 +385,7 @@ def main():
352385 if args .action == ls_recipe :
353386 ls_recipe ()
354387 elif args .action == uninstall_recipe :
355- uninstall_recipe (args .module_name )
388+ uninstall_recipe (args .name , pathlib . Path ( args . out ) )
356389 elif args .action == create_recipe :
357390 create_recipe (args .path , args .out , args .name , cutoff = args .cutoff , verbose = True )
358391
0 commit comments