3
_Af9/                 @   s(  d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ e rPd dlm	Z	 nd dl	m	Z	 d dl
mZ dd ZG dd deZG d	d
 d
eZG dd deZdd Zdd Zdd Zdd Zd/ddZdd Zdd Zdd Zd0dd Zd!d" Zd#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Z dS )1    N)
is_python3)Queue)Threadc               C   s   t j dkS )z Returns True when running with the native port of Python for Windows,
  False when running on any other platform (including the Cygwin port of
  Python).
  ZWindows)platformsystem r   r   @/home/qytech/356xLinux510-101server/.repo/repo/platform_utils.py	isWindows!   s    r	   c               @   sP   e Zd ZdZedd Zdd Zdd Zdd	 Ze	d
d Z
dd Zdd ZdS )FileDescriptorStreamsz Platform agnostic abstraction enabling non-blocking I/O over a
  collection of file descriptors. This abstraction is required because
  fctnl(os.O_NONBLOCK) is not supported on Windows.
  c             C   s   t  rt S t S dS )z\ Factory method: instantiates the concrete class according to the
    current platform.
    N)r	   _FileDescriptorStreamsThreads!_FileDescriptorStreamsNonBlocking)clsr   r   r   create/   s    zFileDescriptorStreams.createc             C   s
   g | _ d S )N)streams)selfr   r   r   __init__9   s    zFileDescriptorStreams.__init__c             C   s   | j j| j||| dS )z4 Wraps an existing file descriptor as a stream.
    N)r   append_create_stream)r   fddeststd_namer   r   r   add<   s    zFileDescriptorStreams.addc             C   s   | j j| dS )z* Removes a stream, when done with it.
    N)r   remove)r   streamr   r   r   r   A   s    zFileDescriptorStreams.removec             C   s   t | jdkS )z8 Returns True when all streams have been processed.
    r   )lenr   )r   r   r   r   is_doneF   s    zFileDescriptorStreams.is_donec             C   s   t dS )z Returns the set of streams that have data available to read.
    The returned streams each expose a read() and a close() method.
    When done with a stream, call the remove(stream) method.
    N)NotImplementedError)r   r   r   r   selectL   s    zFileDescriptorStreams.selectc             C   s   t dS )z@ Creates a new stream wrapping an existing file descriptor.
    N)r   )r   r   r   r   r   r   r   r   S   s    z$FileDescriptorStreams._create_streamN)__name__
__module____qualname____doc__classmethodr   r   r   r   propertyr   r   r   r   r   r   r   r
   *   s   
r
   c               @   s0   e Zd ZdZG dd deZdd Zdd ZdS )	r   zZ Implementation of FileDescriptorStreams for platforms that support
  non blocking I/O.
  c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )z(_FileDescriptorStreamsNonBlocking.Streamz  Encapsulates a file descriptor c             C   s   || _ || _|| _| j  d S )N)r   r   r   set_non_blocking)r   r   r   r   r   r   r   r   `   s    z1_FileDescriptorStreamsNonBlocking.Stream.__init__c             C   s4   dd l }|j | j|j}|j | j|j|tjB  d S )Nr   )fcntlr   ZF_GETFLZF_SETFLos
O_NONBLOCK)r   r%   flagsr   r   r   r$   f   s    z9_FileDescriptorStreamsNonBlocking.Stream.set_non_blockingc             C   s
   | j j S )N)r   fileno)r   r   r   r   r)   k   s    z/_FileDescriptorStreamsNonBlocking.Stream.filenoc             C   s   | j jdS )Ni   )r   read)r   r   r   r   r*   n   s    z-_FileDescriptorStreamsNonBlocking.Stream.readc             C   s   | j j  d S )N)r   close)r   r   r   r   r+   q   s    z._FileDescriptorStreamsNonBlocking.Stream.closeN)	r   r   r    r!   r   r$   r)   r*   r+   r   r   r   r   Stream]   s   r,   c             C   s   | j |||S )N)r,   )r   r   r   r   r   r   r   r   t   s    z0_FileDescriptorStreamsNonBlocking._create_streamc             C   s   t j | jg g \}}}|S )N)r   r   )r   Zready_streams_r   r   r   r   w   s    z(_FileDescriptorStreamsNonBlocking.selectN)r   r   r    r!   objectr,   r   r   r   r   r   r   r   Y   s   r   c                   sP   e Zd ZdZ fddZdd Zdd ZG dd	 d	eZG d
d deZ	  Z
S )r   z Implementation of FileDescriptorStreams for platforms that don't support
  non blocking I/O. This implementation requires creating threads issuing
  blocking read operations on file descriptors.
  c                s   t t| j  td| _d S )N
   )superr   r   r   queue)r   )	__class__r   r   r      s    z&_FileDescriptorStreamsThreads.__init__c             C   s   | j |||| jS )N)r,   r1   )r   r   r   r   r   r   r   r      s    z,_FileDescriptorStreamsThreads._create_streamc             C   s   | j j }|j}|j|_|gS )N)r1   getr   data)r   itemr   r   r   r   r      s    
z$_FileDescriptorStreamsThreads.selectc               @   s   e Zd ZdZdd ZdS )z'_FileDescriptorStreamsThreads.QueueItemz Item put in the shared queue c             C   s   || _ || _d S )N)r   r4   )r   r   r4   r   r   r   r      s    z0_FileDescriptorStreamsThreads.QueueItem.__init__N)r   r   r    r!   r   r   r   r   r   	QueueItem   s   r6   c               @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )z$_FileDescriptorStreamsThreads.Streamz  Encapsulates a file descriptor c             C   sB   || _ || _|| _|| _d | _t| jd| _d| j_| jj	  d S )N)targetT)
r   r   r   r1   r4   r   read_to_queuethreaddaemonstart)r   r   r   r   r1   r   r   r   r      s    z-_FileDescriptorStreamsThreads.Stream.__init__c             C   s   | j j  d S )N)r   r+   )r   r   r   r   r+      s    z*_FileDescriptorStreamsThreads.Stream.closec             C   s   | j }d | _ |S )N)r4   )r   r4   r   r   r   r*      s    z)_FileDescriptorStreamsThreads.Stream.readc             C   sN   x*t | jjdD ]}| jjtj| | qW | jj  | jjtj| d dS )z The thread function: reads everything from the file descriptor into
      the shared queue and terminates when reaching EOF.
          N)iterr   readliner1   Zputr   r6   r+   )r   liner   r   r   r8      s    
z2_FileDescriptorStreamsThreads.Stream.read_to_queueN)r   r   r    r!   r   r+   r*   r8   r   r   r   r   r,      s
   
r,   )r   r   r    r!   r   r   r   r.   r6   r,   __classcell__r   r   )r2   r   r   |   s   r   c             C   sp   t  r`ddl}t| } t|}tjjtjj|| }t|rN|jt	| | ql|j
t	| | ntj| |S dS )zCreates a symbolic link pointing to source named link_name.
  Note: On Windows, source must exist on disk, as the implementation needs
  to know whether to create a "File" or a "Directory" symbolic link.
  r   N)r	   platform_utils_win32_validate_winpathr&   pathjoindirnameisdirZcreate_dirsymlink_makelongpathZcreate_filesymlinksymlink)sourceZ	link_namerA   r7   r   r   r   rH      s    rH   c             C   s*   t jj| } t| r| S tdj| d S )NzRPath "%s" must be a relative path or an absolute path starting with a drive letter)r&   rC   normpath_winpath_is_valid
ValueErrorformat)rC   r   r   r   rB      s
    rB   c             C   sV   t  s
ttjj| } tjj| \}}|rL|s<|d tjkS |d tjkS n| S dS )zWindows only: returns True if path is relative (e.g. ".\foo") or is
  absolute including a drive letter (e.g. "c:\foo"). Returns False if path
  is ambiguous (e.g. "x:foo" or "\foo").
  r   N)r	   AssertionErrorr&   rC   rJ   
splitdrivesep)rC   Zdrivetailr   r   r   rK      s    
rK   c             C   sL   t  rDt| dk r| S | jdr$| S tjj| s4| S dtjj|  S | S dS )zReturn the input path normalized to support the Windows long path syntax
  ("\\?\" prefix) if needed, i.e. if the input path is longer than the
  MAX_PATH limit.
     z\\?\N)r	   r   
startswithr&   rC   isabsrJ   )rC   r   r   r   rG      s    
rG   Fc             C   s*   d}t  rt| } t}tj| ||d dS )zcshutil.rmtree(path) wrapper with support for long paths on Windows.

  Availability: Unix, Windows.N)ignore_errorsonerror)r	   rG   handle_rmtree_errorshutilrmtree)rC   rU   rV   r   r   r   rY      s
    rY   c             C   s   t j|tj | | d S )N)r&   chmodstatS_IWRITE)functionrC   Zexcinfor   r   r   rW     s    rW   c             C   s   t  rvytjt| t| W q tk
rr } z8|jtjkr`tjt| tjt| t| n W Y dd}~X qX ntj| | dS )zcos.rename(src, dst) wrapper with support for long paths on Windows.

  Availability: Unix, Windows.N)r	   r&   renamerG   OSErrorerrnoEEXISTr   )srcdster   r   r   r^   	  s    r^   c             C   s   t  rt| }ytj| W q tk
r } zJ|jtjkrptj|tj	 t
|rdt|rdtj| qrtj| n W Y dd}~X qX n
tj|  dS )zRemove (delete) the file path. This is a replacement for os.remove that
  allows deleting read-only files on Windows, with support for long paths and
  for deleting directory symbolic links.

  Availability: Unix, Windows.N)r	   rG   r&   r   r_   r`   ZEACCESrZ   r[   r\   islinkrF   rmdir)rC   Zlongpathrd   r   r   r   r     s    r   Tc             C   s(   t  rt| |||S tj| |||S dS )z`os.walk(path) wrapper with support for long paths on Windows.

  Availability: Windows, Unix.
  N)r	   _walk_windows_implr&   walk)toptopdownrV   followlinksr   r   r   rh   4  s    rh   c             c   s   yt | }W n0 tk
r< } z|d k	r.|| d S d }~X nX g g  }}x4|D ],}ttjj| |rp|j| qN|j| qNW |r| ||fV  xF|D ]>}tjj| |}	|st|	 rxt|	|||D ]
}
|
V  qW qW |s| ||fV  d S )N)	listdir	ExceptionrF   r&   rC   rD   r   re   rg   )ri   rj   rV   rk   nameserrdirsnondirsnamenew_pathxr   r   r   rg   ?  s(    


rg   c             C   s   t jt| S )zcos.listdir(path) wrapper with support for long paths on Windows.

  Availability: Windows, Unix.
  )r&   rl   rG   )rC   r   r   r   rl   Y  s    rl   c             C   s   t jt|  dS )zaos.rmdir(path) wrapper with support for long paths on Windows.

  Availability: Windows, Unix.
  N)r&   rf   rG   )rC   r   r   r   rf   a  s    rf   c             C   s   t jjt| S )zfos.path.isdir(path) wrapper with support for long paths on Windows.

  Availability: Windows, Unix.
  )r&   rC   rF   rG   )rC   r   r   r   rF   i  s    rF   c             C   s,   t  rddl}|jt| S tjj| S dS )zgos.path.islink(path) wrapper with support for long paths on Windows.

  Availability: Windows, Unix.
  r   N)r	   rA   re   rG   r&   rC   )rC   rA   r   r   r   re   q  s    re   c             C   s*   t  rddl}|jt| S tj| S dS )a  Return a string representing the path to which the symbolic link
  points. The result may be either an absolute or relative pathname;
  if it is relative, it may be converted to an absolute pathname using
  os.path.join(os.path.dirname(path), result).

  Availability: Windows, Unix.
  r   N)r	   rA   readlinkrG   r&   )rC   rA   r   r   r   ru   }  s    ru   c             C   s   t  rtjj| }g }xptddD ]b}t|rNt|}tjjtjj||}q"tjj	|}|dkrn|j
| P |j
| tjj|}q"W |j  tjjtjj| }|S tjj| S dS )zReturn the canonical path of the specified filename, eliminating
  any symbolic links encountered in the path.

  Availability: Windows, Unix.
  r   d    N)r	   r&   rC   abspathrangere   ru   rD   rE   basenamer   reverserJ   realpath)rC   Zcurrent_pathZ	path_tailcr7   rz   resultr   r   r   r|     s"    

r|   )F)TNF)!r`   r&   r   r   rX   r[   	pyversionr   r1   r   	threadingr   r	   r.   r
   r   r   rH   rB   rK   rG   rY   rW   r^   r   rh   rg   rl   rf   rF   re   ru   r|   r   r   r   r   <module>   s<   	/#=

