B
    һd!                 @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ d
d Zdd Zdd Zdd ZdddZdddZdddZdS )z;Communicating tracebacks and source code with debug server.    N)debug_service_pb2)	debug_pb2)common)debug_service_pb2_grpc)source_utils)gfile)tfprof_loggerc          	   C   st   t | }t |_| |_|j|_|j|_	y0t 
| d}|j|   W d Q R X W n tk
rn   Y nX d S )Nr)r   ZStatsocketgethostnamehost	file_pathZ
mtime_nsecZlast_modifiedlengthbytesZOpenlinesextendread
splitlinesIOError)r   Zsource_file_protoZ	file_statf r   [/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/debug/lib/source_remote.py_load_debugged_source_file   s    

"r   c             C   s   | |krt ||| < ||  S )N)len)stringstring_to_idr   r   r   _string_to_id+   s    r   c             C   s   i }d|d< x@| D ]8}|\}}}}|j jjt|||t||t||d qW |j}x(| D ]\}	}
|	dk	rr|	nd||
< q^W dS )zFormat a traceback stack for a `CallTraceback` proto.

  Args:
    origin_stack: The stack list as returned by `traceback.extract_stack()`.
    call_traceback_proto: A `CallTraceback` proto whose fields are to be
      populated.
  r   N)file_idlinenoZfunction_idZline_id )origin_stacktracesaddr   origin_id_to_stringitems)r    Zcall_traceback_protor   framer   r   	func_nameZ	line_textid_to_stringkeyvaluer   r   r   _format_origin_stack1   s    
r*   c                sT   t  }x(| D ] }x|jD ]}||j qW qW  fdd|D }dd |D }|S )a4  Extract source file paths outside TensorFlow Python library.

  Args:
    code_defs: An iterable of `CodeDef` protos, i.e., an iterable of stack
      traces.
    id_to_string: A proto map from integer ids to strings.

  Returns:
    An iterable of source file paths outside the TensorFlow Python library.
  c             3   s   | ]} | V  qd S )Nr   ).0r   )r'   r   r   	<genexpr>W   s    zC_source_file_paths_outside_tensorflow_py_library.<locals>.<genexpr>c             s   s&   | ]}t |st|r|V  qd S )N)r   Zguess_is_tensorflow_py_libraryr   ZExists)r+   r   r   r   r   r,   Y   s    )setr!   r"   r   )Z	code_defsr'   Zfile_idscode_deftraceZnon_tf_filesr   )r'   r   0_source_file_paths_outside_tensorflow_py_libraryH   s    
r0   FTc             C   s>  t | ts| g} dd | D } |r*tjjntjj}|rDtj|ddnd}tj||||r\|jndd}t	|| |rt
 }	|	tdd |jjD |jj |	t|jg|j g }
x.|	D ]&}t }t||j  |
| qW xT| D ]L}d	d
g}tj||d}t|}|| |rx|
D ]}|| q"W qW dS )ar  Send the tracebacks of a TensorFlow execution call.

  To gRPC debug server(s). This applies to graph execution (`tf.Session.run()`)
  calls and eager execution calls.

  If `send_source`, also sends the underlying source files outside the
  TensorFlow library.

  Args:
    destinations: gRPC destination addresses, a `str` or a `list` of `str`s,
      e.g., "localhost:4242". If a `list`, gRPC requests containing the same
      `CallTraceback` proto payload will be sent to all the destinations.
    origin_stack: The traceback stack for the origin of the execution call. For
      graph execution, this is the traceback of the `tf.Session.run()`
      invocation. For eager execution, this is the traceback of the Python
      line that executes the eager operation.
    is_eager_execution: (`bool`) whether an eager execution call (i.e., not a
      `tf.Session.run` or derived methods) is being sent.
    call_key: The key of the execution call, as a string. For graph execution,
      this is a string describing the feeds, fetches (and targets) names of the
      `tf.Session.run` call. For eager execution, this is ignored.
    graph: A Python `tf.Graph` object (i.e., *not* a `tf.compat.v1.GraphDef`),
      which contains op tracebacks, if applicable.
    send_source: Whether the source files involved in the op tracebacks but
      outside the TensorFlow library are to be sent.
  c             S   s.   g | ]&}| tjr&|ttjd  n|qS )N)
startswithr   ZGRPC_URL_PREFIXr   )r+   destr   r   r   
<listcomp>   s   z)_send_call_tracebacks.<locals>.<listcomp>F)Zadd_trainable_varN)	call_typecall_keygraph_tracebackZgraph_versionc             s   s   | ]}|j V  qd S )N)r.   )r+   Z	log_entryr   r   r   r,      s    z(_send_call_tracebacks.<locals>.<genexpr>)zgrpc.max_receive_message_length)zgrpc.max_send_message_lengthr7   )options)
isinstancelistr   ZCallTracebackZEAGER_EXECUTIONZGRAPH_EXECUTIONr   Zmerge_default_with_oplogversionr*   r-   updater0   r6   Zlog_entriesr'   r    r#   r   ZDebuggedSourceFilesr   source_filesr"   appendgrpcZinsecure_channelr   ZEventListenerStubZSendTracebacksZSendSourceFiles)destinationsr    is_eager_executionr5   graphsend_sourcer4   r6   Zcall_tracebackZsource_file_pathsZdebugged_source_filesr   r=   destinationZno_max_message_sizesZchannelZstubr   r   r   _send_call_tracebacks^   sD     







rE   c             C   s   t | |d|||d dS )a  Send the tracebacks of a graph execution call to debug server(s).

  Args:
    destinations: gRPC destination addresses, a `str` or a `list` of `str`s,
      e.g., "localhost:4242". If a `list`, gRPC requests containing the same
      `CallTraceback` proto payload will be sent to all the destinations.
    run_key: A string describing the feeds, fetches (and targets) names of the
      `tf.Session.run` call.
    origin_stack: The traceback of the `tf.Session.run()` invocation.
    graph: A Python `tf.Graph` object (i.e., *not* a `tf.compat.v1.GraphDef`),
      which contains op tracebacks.
    send_source: Whether the source files involved in the op tracebacks but
      outside the TensorFlow library are to be sent.
  F)rA   r5   rB   rC   N)rE   )r@   Zrun_keyr    rB   rC   r   r   r   send_graph_tracebacks   s    rF   c             C   s   t | |d|d dS )a  Send the tracebacks of an eager execution call to debug server(s).

  Args:
    destinations: gRPC destination addresses, a `str` or a `list` of `str`s,
      e.g., "localhost:4242". If a `list`, gRPC requests containing the same
    origin_stack: The traceback of the eager operation invocation.
    send_source: Whether the source files involved in the op tracebacks but
      outside the TensorFlow library are to be sent.
  T)rA   rC   N)rE   )r@   r    rC   r   r   r   send_eager_tracebacks   s    rG   )FNNT)T)T)__doc__r
   r?   Ztensorflow.core.debugr   Ztensorflow.core.protobufr   Ztensorflow.python.debug.libr   r   r   Ztensorflow.python.platformr   Ztensorflow.python.profilerr   r   r   r*   r0   rE   rF   rG   r   r   r   r   <module>   s&      
M
