|
1 | | -import errno |
2 | 1 | import glob |
3 | 2 | import logging |
4 | 3 | import os |
5 | 4 | from contextlib import contextmanager |
6 | 5 | from signal import SIGINT, SIGTERM, signal |
7 | 6 |
|
8 | | -from .files import create_file_racefree, wait_for_lock |
| 7 | +from .files import _create_lock, create_file_racefree, wait_for_lock |
| 8 | +from .paths import mkabs |
9 | 9 |
|
10 | 10 | PID = os.getpid() |
11 | 11 | READ = f"read-{PID}" |
@@ -290,32 +290,6 @@ def ensure_write_access(lock_path, strict_ro_locks=False): |
290 | 290 | return True |
291 | 291 |
|
292 | 292 |
|
293 | | -def _create_lock(lock_path, filepath, wait_max) -> None: |
294 | | - # _LOGGER.debug(f"Creating lock for {filepath} at {lock_path}") |
295 | | - _LOGGER.debug(f"Creating lock at {os.path.basename(lock_path)}") |
296 | | - try: |
297 | | - create_file_racefree(lock_path) |
298 | | - except FileNotFoundError: |
299 | | - parent_dir = os.path.dirname(filepath) |
300 | | - os.makedirs(parent_dir) |
301 | | - _create_lock(lock_path, filepath, wait_max) |
302 | | - except Exception as e: |
303 | | - if e.errno == errno.EEXIST: |
304 | | - # Rare case: file already exists; |
305 | | - # the lock has been created in the split second since the |
306 | | - # last lock existence check, |
307 | | - # wait for the lock file to be gone, but no longer than |
308 | | - # `wait_max`. |
309 | | - _LOGGER.info( |
310 | | - "The lock has been created in the split second since the " |
311 | | - "last lock existence check. Waiting" |
312 | | - ) |
313 | | - wait_for_lock(lock_path, wait_max) |
314 | | - _create_lock(lock_path, filepath, wait_max) |
315 | | - else: |
316 | | - raise e |
317 | | - |
318 | | - |
319 | 293 | def _remove_lock(lock_path) -> bool: |
320 | 294 | """Remove lock. |
321 | 295 |
|
@@ -345,32 +319,6 @@ def make_all_lock_paths(filepath): |
345 | 319 | return lock_paths |
346 | 320 |
|
347 | 321 |
|
348 | | -def mkabs(path, reldir=None): |
349 | | - """Make sure a path is absolute. |
350 | | -
|
351 | | - If not already absolute, it's made absolute relative to a given directory. |
352 | | - Also expands ~ and environment variables for kicks. |
353 | | -
|
354 | | - Args: |
355 | | - path: Path to make absolute |
356 | | - reldir: Relative directory to make path absolute from if it's not already absolute |
357 | | -
|
358 | | - Returns: |
359 | | - str: Absolute path |
360 | | - """ |
361 | | - |
362 | | - def xpand(path): |
363 | | - return os.path.expandvars(os.path.expanduser(path)) |
364 | | - |
365 | | - if os.path.isabs(xpand(path)): |
366 | | - return xpand(path) |
367 | | - |
368 | | - if not reldir: |
369 | | - return os.path.abspath(xpand(path)) |
370 | | - |
371 | | - return os.path.join(xpand(reldir), xpand(path)) |
372 | | - |
373 | | - |
374 | 322 | # class OneLocker(object): |
375 | 323 | # def lock(self, type=READ): |
376 | 324 | # if not self.filepath: |
|
0 commit comments