From a4c2ead732ba7ba5daca371b0856f23e6cb0a834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Boisselier?= Date: Sun, 19 Nov 2023 18:33:47 +0100 Subject: [PATCH] using fcntl to check if a file is still open by klipper --- K-ShakeTune/scripts/is_workflow.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/K-ShakeTune/scripts/is_workflow.py b/K-ShakeTune/scripts/is_workflow.py index be38add..30ca525 100755 --- a/K-ShakeTune/scripts/is_workflow.py +++ b/K-ShakeTune/scripts/is_workflow.py @@ -32,6 +32,7 @@ import glob import sys import shutil import tarfile +import fcntl from datetime import datetime ################################################################################################################# @@ -48,15 +49,15 @@ RESULTS_SUBFOLDERS = ['belts', 'inputshaper', 'vibrations'] def is_file_open(filepath): - for proc in os.listdir('/proc'): - if proc.isdigit(): - for fd in glob.glob(f'/proc/{proc}/fd/*'): - try: - if os.path.samefile(fd, filepath): - return True - except FileNotFoundError: - pass - return False + try: + with open(filepath, 'a') as file: + fcntl.flock(file.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB) + # File is not in use (and can be used safely) + fcntl.flock(file.fileno(), fcntl.LOCK_UN) + return False + except IOError: + # File is still in use (and should not be touched for now) + return True def get_belts_graph():