mirror of
https://github.com/ARM-software/workload-automation.git
synced 2025-04-15 23:30:47 +01:00
utils/file_lock: Create lock files in system temp directory
Use the original file path to create a lock file in the system temp directory. This prevents issues where we are attempting to lock a file where wa does not have permission to create new files.
This commit is contained in:
parent
b9d593e578
commit
2d32d81acb
@ -34,6 +34,7 @@ from contextlib import contextmanager
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from functools import reduce # pylint: disable=redefined-builtin
|
from functools import reduce # pylint: disable=redefined-builtin
|
||||||
from operator import mul
|
from operator import mul
|
||||||
|
from tempfile import gettempdir
|
||||||
from time import sleep
|
from time import sleep
|
||||||
if sys.version_info[0] == 3:
|
if sys.version_info[0] == 3:
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
@ -660,14 +661,15 @@ def lock_file(path, timeout=30):
|
|||||||
from wa.framework.exception import ResourceError
|
from wa.framework.exception import ResourceError
|
||||||
|
|
||||||
locked = False
|
locked = False
|
||||||
l_file = '{}.lock'.format(path) if not path.endswith('.lock') else path
|
l_file = 'wa-{}.lock'.format(path)
|
||||||
file_lock_logger.debug('Acquiring lock on "{}"'.format(l_file))
|
l_file = os.path.join(gettempdir(), l_file.replace(os.path.sep, '_'))
|
||||||
|
file_lock_logger.debug('Acquiring lock on "{}"'.format(path))
|
||||||
try:
|
try:
|
||||||
while timeout:
|
while timeout:
|
||||||
try:
|
try:
|
||||||
open(l_file, 'x').close()
|
open(l_file, 'x').close()
|
||||||
locked = True
|
locked = True
|
||||||
file_lock_logger.debug('Lock acquired on "{}"'.format(l_file))
|
file_lock_logger.debug('Lock acquired on "{}"'.format(path))
|
||||||
break
|
break
|
||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
msg = 'Failed to acquire lock on "{}" Retrying...'
|
msg = 'Failed to acquire lock on "{}" Retrying...'
|
||||||
@ -683,4 +685,4 @@ def lock_file(path, timeout=30):
|
|||||||
finally:
|
finally:
|
||||||
if locked and os.path.exists(l_file):
|
if locked and os.path.exists(l_file):
|
||||||
os.remove(l_file)
|
os.remove(l_file)
|
||||||
file_lock_logger.debug('Lock released "{}"'.format(l_file))
|
file_lock_logger.debug('Lock released "{}"'.format(path))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user