certbot.lock

Implements file locks compatible with Linux and Windows for locking files and directories.

certbot.lock.lock_dir(dir_path)[source]

Place a lock file on the directory at dir_path.

The lock file is placed in the root of dir_path with the name .certbot.lock.

Parameters:dir_path (str) – path to directory
Returns:the locked LockFile object
Return type:LockFile
Raises:errors.LockError – if unable to acquire the lock
class certbot.lock.LockFile(path)[source]

Bases: object

Platform independent file lock system. LockFile accepts a parameter, the path to a file acting as a lock. Once the LockFile, instance is created, the associated file is ‘locked from the point of view of the OS, meaning that if another instance of Certbot try at the same time to acquire the same lock, it will raise an Exception. Calling release method will release the lock, and make it available to every other instance. Upon exit, Certbot will also release all the locks. This allows us to protect a file or directory from being concurrently accessed or modified by two Certbot instances. LockFile is platform independent: it will proceed to the appropriate OS lock mechanism depending on Linux or Windows.

acquire()[source]

Acquire the lock on the file, forbidding any other Certbot instance to acquire it. :raises errors.LockError: if unable to acquire the lock

release()[source]

Release the lock on the file, allowing any other Certbot instance to acquire it.

is_locked()[source]

Check if the file is currently locked. :return: True if the file is locked, False otherwise

class certbot.lock._UnixLockMechanism(path)[source]

Bases: certbot.lock._BaseLockMechanism

A UNIX lock file mechanism. This lock file is released when the locked file is closed or the process exits. It cannot be used to provide synchronization between threads. It is based on the lock_file package by Martin Horcicka.

acquire()[source]

Acquire the lock.

_try_lock(fd)[source]

Try to acquire the lock file without blocking. :param int fd: file descriptor of the opened file to lock

_lock_success(fd)[source]

Did we successfully grab the lock? Because this class deletes the locked file when the lock is released, it is possible another process removed and recreated the file between us opening the file and acquiring the lock. :param int fd: file descriptor of the opened file to lock :returns: True if the lock was successfully acquired :rtype: bool

release()[source]

Remove, close, and release the lock file.

class certbot.lock._WindowsLockMechanism(path)[source]

Bases: certbot.lock._BaseLockMechanism

A Windows lock file mechanism. By default on Windows, acquiring a file handler gives exclusive access to the process and results in an effective lock. However, it is possible to explicitly acquire the file handler in shared access in terms of read and write, and this is done by os.open and io.open in Python. So an explicit lock needs to be done through the call of msvcrt.locking, that will lock the first byte of the file. In theory, it is also possible to access a file in shared delete access, allowing other processes to delete an opened file. But this needs also to be done explicitly by all processes using the Windows low level APIs, and Python does not do it. As of Python 3.7 and below, Python developers state that deleting a file opened by a process from another process is not possible with os.open and io.open. Consequently, mscvrt.locking is sufficient to obtain an effective lock, and the race condition encountered on Linux is not possible on Windows, leading to a simpler workflow.

acquire()[source]

Acquire the lock

release()[source]

Release the lock.