@@ -646,14 +646,29 @@ def pip_install_target(
646
646
pip_args : list ,
647
647
rm_patterns : str = "*.dist-info,__pycache__" ,
648
648
force = False ,
649
+ clear_dir = False ,
649
650
sys_path : typing .Optional [int ] = None ,
650
651
):
652
+ """
653
+ Installs target dependencies using pip and cleans up the installed files afterwards.
654
+
655
+ Args:
656
+ target (Path): The target directory for dependency installation.
657
+ pip_args (list): List of arguments for the pip install command.
658
+ rm_patterns (str, optional): Patterns of files to remove after installation, defaults to "*.dist-info,__pycache__".
659
+ force (bool, optional): Flag to force reinstallation, defaults to False.
660
+ clear_dir (bool, optional): will rmtree the target directory while clear_dir=True.
661
+ sys_path (typing.Optional[int], optional): If provided, inserts the target directory at the specified position in sys.path.
662
+
663
+ Returns:
664
+ bool: Returns True if the installation is successful.
665
+ """
651
666
target = Path (target )
652
667
md5_path = target / ZipApp .get_md5 (pip_args )
653
668
if not force and md5_path .exists ():
654
669
return False
655
- if force :
656
- shutil .rmtree (target .as_posix ())
670
+ if clear_dir and target . is_dir () :
671
+ shutil .rmtree (target .as_posix (), ignore_errors = True )
657
672
ZipApp ._pip_install (target_dir = target , pip_args = pip_args )
658
673
if rm_patterns :
659
674
ZipApp ._rm_with_patterns (target , patterns = rm_patterns .split ("," ))
@@ -663,4 +678,5 @@ def pip_install_target(
663
678
sys .path .insert (sys_path , target .absolute ().as_posix ())
664
679
return True
665
680
681
+
666
682
create_app = ZipApp .create_app
0 commit comments