Skip to content

Commit

Permalink
Rewrite the _init_logdir function according to FIXME (#674)
Browse files Browse the repository at this point in the history
Co-authored-by: KAAANG <79990647+SAKURA-CAT@users.noreply.github.com>
  • Loading branch information
Puiching-Memory and SAKURA-CAT authored Dec 6, 2024
1 parent 89130eb commit 8ff44b9
Showing 1 changed file with 27 additions and 31 deletions.
58 changes: 27 additions & 31 deletions swanlab/data/callback_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import sys
import traceback
from datetime import datetime
from typing import Union

from swankit.callback.models import RuntimeInfo, MetricInfo
from swankit.core import SwanLabSharedSettings
Expand Down Expand Up @@ -52,47 +53,42 @@ def _error_print(tp):
swanlog.info("Error happened while training")

@staticmethod
def _init_logdir(logdir: str = None) -> str:
def _init_logdir(logdir: Union[str, None] = None) -> None:
"""
根据传入的logdir,初始化日志文件夹
FIXME shit code
根据传入的logdir,初始化日志文件夹
---
Args:
logdir: 日志文件夹路径
Return:
None
Step:
1: 参数检查
2: 环境变量设置
3: 默认路径
4: .gitignore
"""
env_key = SwanLabEnv.SWANLOG_FOLDER.value
# 如果传入了logdir,则将logdir设置为环境变量,代表日志文件存放的路径
if logdir is not None:
try:
if not isinstance(logdir, str):
raise ValueError("path must be a string")
if not os.path.isabs(logdir):
logdir = os.path.abspath(logdir)
# 如果创建失败,也是抛出IOError
try:
os.makedirs(logdir, exist_ok=True)
except Exception as e:
raise IOError(f"create path: {logdir} failed, error: {e}")
if not os.access(logdir, os.W_OK):
raise IOError(f"no write permission for path: {logdir}")
except ValueError:
raise ValueError("logdir must be a str.")
except IOError:
raise IOError("logdir must be a path and have Write permission.")
# 如果没有传入logdir,则使用默认的logdir, 即当前工作目录下的swanlog文件夹,但是需要保证目录存在
else:
logdir = os.environ.get(SwanLabEnv.SWANLOG_FOLDER.value) or os.path.join(os.getcwd(), "swanlog")
logdir = os.path.abspath(logdir)
try:
os.makedirs(logdir, exist_ok=True)
if not os.access(logdir, os.W_OK):
raise IOError
except IOError:
raise IOError("logdir must have Write permission.")
# 同步环境变量
if logdir is None:
logdir = os.environ.get(env_key) or os.path.join(os.getcwd(), "swanlog")

logdir = os.path.abspath(logdir)
try:
os.makedirs(logdir, exist_ok=True)
if not os.access(logdir, os.W_OK):
raise IOError(f"no write permission for path: {logdir}")
except Exception as error:
raise IOError(f"Failed to create or access logdir: {logdir}, error: {error}")

os.environ[env_key] = logdir

# 如果logdir是空的,创建.gitignore文件,写入*
if not os.listdir(logdir):
with open(os.path.join(logdir, ".gitignore"), "w", encoding="utf-8") as f:
f.write("*")
return logdir

def __str__(self):
return "SwanLabLocalRunCallback"
Expand Down

0 comments on commit 8ff44b9

Please sign in to comment.