File: //lib/python3/dist-packages/uaclient/__pycache__/lock.cpython-310.pyc
o
    Uٵg�  �                   @   s�   d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZmZ d dl
mZ d dlmZmZ d dlmZ d dlmZ e �e�e��ZG d	d
� d
e
�Zeeeddd
�ej�Zdeeef fdd�Zdd� Z G dd� d�Z!dS )�    N)�Tuple)�
exceptions�system�util)�
DataObject�Field�StringDataValue)�notices)�DataObjectFile�DataObjectFileFormat)�UAFile)�Noticec                   @   s2   e Zd Zede�ede�gZdedefdd�ZdS )�LockData�lock_pid�lock_holderc                 C   s   || _ || _d S �N�r   r   )�selfr   r   � r   �//usr/lib/python3/dist-packages/uaclient/lock.py�__init__   s   
zLockData.__init__N)�__name__�
__module__�__qualname__r   r   �fields�strr   r   r   r   r   r      s
    �r   �lockF)�private�returnc                  C   s�   zt �� } W n tjy   tjt jd��w d}| s|S | j}| j}zt�	d|g� t
|�|fW S  tjy\   t�
� sKt�d||� t
|�|f Y S t�d||� t�t j� | Y S w )a  Return lock info if lock file is present the lock is active.
    If process claiming the lock is no longer present, remove the lock file
    and log a warning.
    :return: A tuple (pid, string describing lock holder)
        If no active lock, pid will be -1.
    )�lock_file_path)���� �psz.Found stale lock file previously held by %s:%sz1Removing stale lock file previously held by %s:%s)�lock_data_file�readr   �InvalidFileFormatError�InvalidLockFile�pathr   r   r   �subp�int�ProcessExecutionErrorr   �we_are_currently_root�LOG�debug�warning�ensure_file_absent)�
lock_data_obj�no_lockr   r   r   r   r   �check_lock_info"