Skip to content

Commit

Permalink
rmdir null main_data_dir if _use_port_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
lidong committed Nov 7, 2023
1 parent 8894c81 commit dc5a10a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion ichrome/async_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3411,7 +3411,7 @@ async def add_tip(
text,
style=None,
max_lines: int = 10,
expires: float = None,
expires: Union[float, None] = None,
timeout=NotSet,
):
if style is None:
Expand Down
20 changes: 17 additions & 3 deletions ichrome/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def __init__(
self.popen_kwargs = (
self.DEFAULT_POPEN_ARGS if popen_kwargs is None else popen_kwargs
)
self._use_port_dir = False
self.init()

@classmethod
Expand Down Expand Up @@ -303,6 +304,7 @@ def _wrap_user_data_dir(self):
f"creating user data dir at [{os.path.realpath(self.user_data_dir)}]."
)
self.user_data_dir.mkdir(parents=True, exist_ok=True)
self._use_port_dir = True

@classmethod
def clear_user_dir(cls, user_data_dir=None, port=None):
Expand All @@ -317,12 +319,24 @@ def clear_user_dir(cls, user_data_dir=None, port=None):

def _clear_user_dir(self):
# Deprecated
return self._clear_user_data_dir()
return self.clear_user_data_dir()

def _clear_user_data_dir(self):
self.clear_dir_with_shutil(self.user_data_dir)
if self._use_port_dir:
main_user_dir = self.user_data_dir.parent
if main_user_dir.is_dir():
for sub_file_or_dir in main_user_dir.iterdir():
if sub_file_or_dir.exists():
break
else:
# remove null main_user_dir
main_user_dir.rmdir()

def clear_user_data_dir(self):
# clear self user dir
self.shutdown("_clear_user_dir")
return self.clear_dir_with_shutil(self.user_data_dir)
self._clear_user_data_dir()

@staticmethod
def clear_dir_with_shutil(dir_path):
Expand Down Expand Up @@ -983,7 +997,7 @@ async def _clear_user_dir(self):

async def clear_user_data_dir(self):
await self.shutdown("_clear_user_dir")
return await async_run(self.clear_dir_with_shutil, self.user_data_dir)
return await async_run(self._clear_user_data_dir)

def connect_tab(
self,
Expand Down

0 comments on commit dc5a10a

Please sign in to comment.