fixed permission error for some OS
This commit is contained in:
@@ -3,32 +3,30 @@
|
|||||||
# Common file management functions for the Shake&Tune package
|
# Common file management functions for the Shake&Tune package
|
||||||
# Written by Frix_x#0161 #
|
# Written by Frix_x#0161 #
|
||||||
|
|
||||||
|
import os
|
||||||
import time
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
def wait_file_ready(filepath: Path) -> None:
|
def wait_file_ready(filepath: Path, timeout: int = 60) -> None:
|
||||||
file_busy = True
|
file_busy = True
|
||||||
loop_count = 0
|
loop_count = 0
|
||||||
proc_path = Path('/proc')
|
|
||||||
while file_busy:
|
|
||||||
if loop_count > 60:
|
|
||||||
# If Klipper is taking too long to release the file (60 * 1s = 1min), raise an error
|
|
||||||
raise TimeoutError(f'Klipper is taking too long to release {filepath}!')
|
|
||||||
|
|
||||||
for proc in proc_path.iterdir():
|
while file_busy:
|
||||||
if proc.name.isdigit():
|
if loop_count >= timeout:
|
||||||
fd_path = proc / 'fd'
|
raise TimeoutError(f'Klipper is taking too long to release the CSV file ({filepath})!')
|
||||||
if fd_path.exists():
|
|
||||||
for fd in fd_path.iterdir():
|
# Try to open the file in write-only mode to check if it is in use
|
||||||
|
# If we successfully open and close the file, it is not in use
|
||||||
try:
|
try:
|
||||||
# Using resolve to ensure symbolic links are followed
|
fd = os.open(filepath, os.O_WRONLY)
|
||||||
if fd.resolve(strict=False) == filepath:
|
os.close(fd)
|
||||||
pass # File is still being used by Klipper
|
|
||||||
except FileNotFoundError: # Klipper has already released the CSV file
|
|
||||||
file_busy = False
|
file_busy = False
|
||||||
break
|
except OSError:
|
||||||
except PermissionError: # Unable to check for this particular process due to permissions
|
# If OSError is caught, it indicates the file is still being used
|
||||||
|
pass
|
||||||
|
except Exception:
|
||||||
|
# If another exception is raised, it's not a problem, we just loop again
|
||||||
pass
|
pass
|
||||||
|
|
||||||
loop_count += 1
|
loop_count += 1
|
||||||
|
|||||||
Reference in New Issue
Block a user