semmyk commited on
Commit
75d8b58
·
1 Parent(s): 171594a

file_utils: fix Permission Errno13 creating log file

Browse files
Files changed (1) hide show
  1. file_handler/file_utils.py +16 -3
file_handler/file_utils.py CHANGED
@@ -39,15 +39,28 @@ def check_create_logfile(filename: str, dir_path: Union[str, Path]="logs") -> Pa
39
  """
40
 
41
  import datetime
 
 
42
 
43
  # 1. Get the path of the current script's parent directory (the project folder).
44
  # `__file__` is a special variable that holds the path to the current script.
45
- project_root = Path(__file__).parent.parent.resolve()
 
 
 
 
 
 
 
 
 
 
 
46
  dir_path = dir_path if isinstance(dir_path, Path) else Path(dir_path)
47
 
48
  # 2. Define the path for the logs directory.
49
  # The `/` operator is overloaded to join paths easily.
50
- logs_dir = project_root / dir_path
51
 
52
  # 3. Create the logs directory if it doesn't already exist.
53
  # `mkdir()` with `exist_ok=True` prevents a FileExistsError if the folder exists.
@@ -63,7 +76,7 @@ def check_create_logfile(filename: str, dir_path: Union[str, Path]="logs") -> Pa
63
  # If the file doesn't exist, touch() will create an empty file.
64
  log_file.touch()
65
 
66
- print(f"Created log file at: {log_file}")
67
 
68
  return log_file
69
 
 
39
  """
40
 
41
  import datetime
42
+ import warnings
43
+ import tempfile
44
 
45
  # 1. Get the path of the current script's parent directory (the project folder).
46
  # `__file__` is a special variable that holds the path to the current script.
47
+ #project_root = Path(__file__).parent.parent.resolve()
48
+
49
+ # 1. Get the designated writable directory for Hugging Face Spaces: '/data'
50
+ writable_dir = Path("/data")
51
+ try:
52
+ if not writable_dir.is_dir():
53
+ logs_dir.mkdir(exist_ok=True)
54
+ except PermissionError: ##[Errno 13] Permission denied: '/home/user/app/logs/app_logging_2025-09-18.log'
55
+ warnings.warn("[Errno 13] Permission denied, possibly Persistent Storage not enable: attempting temp folder")
56
+ writable_dir = Path(tempfile.gettempdir()) #
57
+
58
+ # check log dir path
59
  dir_path = dir_path if isinstance(dir_path, Path) else Path(dir_path)
60
 
61
  # 2. Define the path for the logs directory.
62
  # The `/` operator is overloaded to join paths easily.
63
+ logs_dir = writable_dir / dir_path #project_root / dir_path
64
 
65
  # 3. Create the logs directory if it doesn't already exist.
66
  # `mkdir()` with `exist_ok=True` prevents a FileExistsError if the folder exists.
 
76
  # If the file doesn't exist, touch() will create an empty file.
77
  log_file.touch()
78
 
79
+ #print(f"Created log file at: {log_file}")
80
 
81
  return log_file
82