3
_Afw                 @   sP   d dl mZ d dlZd dlZdZdZdZG dd deZej	dd	Z
d
d ZdS )    )print_functionNcommandzsync-networkz
sync-localc               @   sJ   e Zd ZdZdd ZdddZd	d
 Zdd Zdd Zdd Z	dd Z
dS )EventLoga   Event log that records events that occurred during a repo invocation.

  Events are written to the log as a consecutive JSON entries, one per line.
  Each entry contains the following keys:
  - id: A ('RepoOp', ID) tuple, suitable for storing in a datastore.
        The ID is only unique for the invocation of the repo command.
  - name: Name of the object being operated upon.
  - task_name: The task that was performed.
  - start: Timestamp of when the operation started.
  - finish: Timestamp of when the operation finished.
  - success: Boolean indicating if the operation was successful.
  - try_count: A counter indicating the try count of this task.

  Optionally:
  - parent: A ('RepoOp', ID) tuple indicating the parent event for nested
            events.

  Valid task_names include:
  - command: The invocation of a subcommand.
  - sync-network: The network component of a sync command.
  - sync-local: The local component of a sync command.

  Specific tasks may include additional informational properties.
  c             C   s   g | _ d| _dS )zInitializes the event log.N)_log_parent)self r   ;/home/qytech/356xLinux510-101server/.repo/repo/event_log.py__init__5   s    zEventLog.__init__N   RepoOpc       	      C   sX   |t  f||||d}| jr*| jd |d< |dk	s:|dk	rH| j||| | jj| |S )a  Add an event to the log.

    Args:
      name: Name of the object being operated upon.
      task_name: A sub-task that was performed for name.
      start: Timestamp of when the operation started.
      finish: Timestamp of when the operation finished.
      success: Boolean indicating if the operation was successful.
      try_count: A counter indicating the try count of this task.
      kind: The kind of the object for the unique identifier.

    Returns:
      A dictionary of the event added to the log.
    )idname	task_nameZ
start_timetryr   parentN)_NextEventIdr   FinishEventr   append)	r   r   r   startfinishsuccessZ	try_countkindeventr   r   r	   Add:   s    zEventLog.Addc             C   s   | j |j||||}|dk	r|j|d< |jr6|j|d< |jjrJ|jj|d< |jjr^|jj|d< y|j |d< W n tk
r   Y nX |S )a  Add a event to the log for a sync command.

    Args:
      project: Project being synced.
      task_name: A sub-task that was performed for name.
                 One of (TASK_SYNC_NETWORK, TASK_SYNC_LOCAL)
      start: Timestamp of when the operation started.
      finish: Timestamp of when the operation finished.
      success: Boolean indicating if the operation was successful.

    Returns:
      A dictionary of the event added to the log.
    NprojectrevisionZproject_urlZ
remote_urlZgit_hash)	r   relpathr   ZrevisionExprZremoteurlZfetchUrlZGetCommitRevisionId	Exception)r   r   r   r   r   r   r   r   r   r	   AddSync[   s    

zEventLog.AddSyncc             C   s   |rdS dS )zConverst a boolean success to a status string.

    Args:
      success: Boolean indicating if the operation was successful.

    Returns:
      status string.
    passZfailr   )r   r   r   r   r	   GetStatusStringx   s    	zEventLog.GetStatusStringc             C   s   | j ||d< ||d< |S )a  Finishes an incomplete event.

    Args:
      event: An event that has been added to the log.
      finish: Timestamp of when the operation finished.
      success: Boolean indicating if the operation was successful.

    Returns:
      A dictionary of the event added to the log.
    statusZfinish_time)r"   )r   r   r   r   r   r   r	   r      s    zEventLog.FinishEventc             C   s
   || _ dS )zcSet a parent event for all new entities.

    Args:
      event: The event to use as a parent.
    N)r   )r   r   r   r   r	   	SetParent   s    zEventLog.SetParentc             C   sD   t |d0}x(| jD ]}tj||dd |jd qW W dQ R X dS )z[Writes the log out to a file.

    Args:
      filename: The file to write the log to.
    zw+T)Z	sort_keys
N)openr   jsondumpwrite)r   filenamefer   r   r	   Write   s    zEventLog.Write)NNr   r   )__name__
__module____qualname____doc__r
   r   r    r"   r   r$   r-   r   r   r   r	   r      s    
 r   ir   c           
   C   s,   t j  t j} t  jd7  _W dQ R X | S )z|Helper function for grabbing the next unique id.

  Returns:
    A unique, to this invocation of the program, integer id.
  r   N)	_EVENT_IDZget_lockvalue)valr   r   r	   r      s    
r   )
__future__r   r'   ZmultiprocessingTASK_COMMANDZTASK_SYNC_NETWORKZTASK_SYNC_LOCALobjectr   ZValuer3   r   r   r   r   r	   <module>   s    