Skip to content

Commit

Permalink
remove UrlFile.download method
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-black-tea committed Sep 10, 2024
1 parent 59bff92 commit 0abb3b3
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 36 deletions.
12 changes: 7 additions & 5 deletions src/linktools/_environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,15 @@ def get_temp_path(self, *paths: str, create_parent: bool = False) -> Path:
"""
return utils.get_path(self.temp_path, *paths, create_parent=create_parent)

def clean_temp_files(self, expire_days: int = 7) -> None:
def clean_temp_files(self, *paths: str, expire_days: int = 7) -> None:
"""
清理临时文件
"""
current_time = time.time()
target_time = current_time - expire_days * 24 * 60 * 60
for root, dirs, files in os.walk(self.temp_path, topdown=False):

temp_path = self.get_temp_path(*paths)
for root, dirs, files in os.walk(temp_path, topdown=False):
for name in files:
path = os.path.join(root, name)
last_time = max(
Expand All @@ -186,7 +188,7 @@ def clean_temp_files(self, expire_days: int = 7) -> None:
shutil.rmtree(path, ignore_errors=True)

@cached_classproperty
def _log_manager(self) -> logging.Manager:
def _log_manager(self) -> "logging.Manager":

empty_args = tuple()

Expand Down Expand Up @@ -223,13 +225,13 @@ def getLogger(self, name):
return LogManager(logging.root.manager)

@cached_property
def logger(self) -> logging.Logger:
def logger(self) -> "logging.Logger":
"""
模块根logger
"""
return self._log_manager.getLogger(self.name)

def get_logger(self, name: str = None) -> logging.Logger:
def get_logger(self, name: str = None) -> "logging.Logger":
"""
获取模块名作为前缀的logger
"""
Expand Down
33 changes: 8 additions & 25 deletions src/linktools/_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,10 @@ def is_local(self):
return False

@timeoutable
def download(self, retry: int = 2, timeout: TimeoutType = None, **kwargs) -> str:
"""
从指定url下载文件到临时目录
:param timeout: 超时时间
:param retry: 重试次数
:return: 文件路径
"""
try:
self._acquire(timeout=timeout.remain)
local_path, local_name = self._download(retry, timeout, **kwargs)
return local_path
except DownloadError:
raise
except Exception as e:
raise DownloadError(e)
finally:
ignore_error(self._release)

@timeoutable
def save(self, dir: PathType, name: str = None, timeout: TimeoutType = None, retry: int = 2, **kwargs) -> str:
def save(self, dir: PathType = None, name: str = None, timeout: TimeoutType = None, retry: int = 2, **kwargs) -> str:
"""
从指定url下载文件
:param dir: 文件路径
:param dir: 文件路径,如果为空,则会返回临时文件路径
:param name: 文件名,如果为空,则默认为下载的文件名
:param timeout: 超时时间
:param retry: 重试次数
Expand All @@ -95,17 +76,19 @@ def save(self, dir: PathType, name: str = None, timeout: TimeoutType = None, ret
try:
self._acquire(timeout=timeout.remain)

local_path, local_name = self._download(retry, timeout, **kwargs)
temp_path, temp_name = self._download(retry, timeout, **kwargs)
if not dir:
return temp_path

# 先创建文件夹
if not os.path.exists(dir):
self._environ.logger.debug(f"{dir} does not exist, create")
os.makedirs(dir, exist_ok=True)

# 然后把文件保存到指定路径下
dest_path = os.path.join(dir, name or local_name)
self._environ.logger.debug(f"Copy {local_path} to {dest_path}")
shutil.copy(local_path, dest_path)
dest_path = os.path.join(dir, name or temp_name)
self._environ.logger.debug(f"Copy {temp_path} to {dest_path}")
shutil.copy(temp_path, dest_path)

# 把文件移动到指定目录之后,就可以清理缓存文件了
self.clear(timeout=timeout.remain)
Expand Down
2 changes: 1 addition & 1 deletion src/linktools/android/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def install(self, path_or_url: str, opts: [str] = (), **kwargs):
:param opts: 安装参数
"""
_logger.info(f"Install apk url: {path_or_url}")
apk_path = environ.get_url_file(path_or_url).download()
apk_path = environ.get_url_file(path_or_url).save()
_logger.debug(f"Local apk path: {apk_path}")

remote_name = f"installed_{int(time.time())}.apk"
Expand Down
2 changes: 1 addition & 1 deletion src/linktools/cli/commands/common/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def on_java_home(self, version: str = None, shell: str = DEFAULT_SHELL):
@subcommand("clean", help="clean temporary files")
@subcommand_argument("days", metavar="DAYS", nargs="?", help="expire days")
def on_clean(self, days: int = 7):
self.environ.clean_temp_files(days)
self.environ.clean_temp_files(expire_days=days)


command = Command()
Expand Down
4 changes: 2 additions & 2 deletions src/linktools/frida/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ def _load(self):
# 如果是本地文件,直接就返回了
if file.is_local:
_logger.info(f"Load local {self}")
source = utils.read_file(file.download(), text=True)
source = utils.read_file(file.save(), text=True)
return source

# 判断是否需要情况缓存
if not self._cached:
file.clear()

_logger.info(f"Download {self}")
dest_path = file.download()
dest_path = file.save()

source = utils.read_file(dest_path, text=True)
if self._trusted:
Expand Down
2 changes: 1 addition & 1 deletion src/linktools/frida/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def download(self):
with environ.get_url_file(self.url) as file:
if os.path.exists(self.path):
return
temp_path = file.download()
temp_path = file.save()
temp_name = utils.guess_file_name(self.url)
if temp_name.endswith(".xz"):
with lzma.open(temp_path, "rb") as read, open(self.path, "wb") as write:
Expand Down
2 changes: 1 addition & 1 deletion src/linktools/ios/sib.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def exec(self, *args: [Any], **kwargs) -> str:
@timeoutable
def install(self, path_or_url: str, **kwargs) -> str:
_logger.info(f"Install ipa url: {path_or_url}")
ipa_path = environ.get_url_file(path_or_url).download()
ipa_path = environ.get_url_file(path_or_url).save()
_logger.debug(f"Local ipa path: {ipa_path}")
return self.exec("app", "install", "--path", ipa_path, **kwargs)

Expand Down

0 comments on commit 0abb3b3

Please sign in to comment.