
[[\W                 @   s  d  Z  d d l Z d d l Z d d l Z d d l Z e j e j g a d a d a	 d a
 d d   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 d d   Z d d d  Z d d d  Z d d   Z d d   Z d d    Z d! d"   Z d# d$   Z d% Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z  e j! j" s{e    n  d. d/   Z# e$ d0 k re#   n  d S)1a  Append module search paths for third-party packages to sys.path.

****************************************************************
* This module is automatically imported during initialization. *
****************************************************************

This will append site-specific paths to the module search path.  On
Unix (including Mac OSX), it starts with sys.prefix and
sys.exec_prefix (if different) and appends
lib/python3/dist-packages as well as lib/site-python.
On other platforms (such as Windows), it tries each of the
prefixes directly, as well as with lib/site-packages appended.  The
resulting directories, if they exist, are appended to sys.path, and
also inspected for path configuration files.

For Debian and derivatives, this sys.path is augmented with directories
for packages distributed within the distribution. Local addons go
into /usr/local/lib/python<version>/dist-packages, Debian addons
install into /usr/lib/python3/dist-packages.
/usr/lib/python<version>/site-packages is not used.

If a file named "pyvenv.cfg" exists one directory above sys.executable,
sys.prefix and sys.exec_prefix are set to that directory and
it is also checked for site-packages and site-python (sys.base_prefix and
sys.base_exec_prefix will always be the "real" prefixes of the Python
installation). If "pyvenv.cfg" (a bootstrap configuration file) contains
the key "include-system-site-packages" set to anything other than "false"
(case-insensitive), the system-level prefixes will still also be
searched for site-packages; otherwise they won't.

All of the resulting site-specific directories, if they exist, are
appended to sys.path, and also inspected for path configuration
files.

A path configuration file is a file whose name has the form
<package>.pth; its contents are additional directories (one per line)
to be added to sys.path.  Non-existing directories (or
non-directories) are never added to sys.path; no directory is added to
sys.path more than once.  Blank lines and lines beginning with
'#' are skipped. Lines starting with 'import' are executed.

For example, suppose sys.prefix and sys.exec_prefix are set to
/usr/local and there is a directory /usr/local/lib/python2.5/site-packages
with three subdirectories, foo, bar and spam, and two path
configuration files, foo.pth and bar.pth.  Assume foo.pth contains the
following:

  # foo package configuration
  foo
  bar
  bletch

and bar.pth contains:

  # bar package configuration
  bar

Then the following directories are added to sys.path, in this order:

  /usr/local/lib/python2.5/site-packages/bar
  /usr/local/lib/python2.5/site-packages/foo

Note that bletch is omitted because it doesn't exist; bar precedes foo
because bar.pth comes alphabetically before foo.pth; and spam is
omitted because it is not mentioned in either path configuration file.

The readline module is also automatically configured to enable
completion for systems that support it.  This can be overriden in
sitecustomize, usercustomize or PYTHONSTARTUP.

After these operations, an attempt is made to import a module
named sitecustomize, which can perform arbitrary additional
site-specific customizations.  If this import fails with an
ImportError exception, it is silently ignored.
    Nc              G   sS   t  j j |    } y t  j j |  } Wn t k
 r< Yn X| t  j j |  f S)N)ospathjoinabspathOSErrornormcase)Zpathsdir r	   /usr/lib/python3.4/site.pymakepath_   s    r   c              C   s   x t  t j j    D] }  t t |  d d  d d  d k rF q n  y t j j |  j  |  _ Wn t	 t
 f k
 r| Yn Xy t j j |  j  |  _ Wq t	 t
 f k
 r Yq Xq Wd S)zESet all module __file__ and __cached__ attributes to an absolute path
__loader__N
__module___frozen_importlib)setsysmodulesvaluesgetattrr   r   r   __file__AttributeErrorr   
__cached__)mr	   r	   r
   	abs_pathsh   s    	r   c              C   su   g  }  t    } xL t j D]A } t |  \ } } | | k r |  j |  | j |  q q W|  t j d d  <| S)zK Remove duplicate entries from sys.path along with making them
    absoluteN)r   r   r   r   appendadd)Lknown_pathsr   dircaser	   r	   r
   removeduppathsx   s    	r   c              C   sq   t    }  xa t j D]V } y8 t j j |  rP t |  \ } } |  j |  n  Wq t k
 rh w Yq Xq W|  S)zDReturn a set containing all existing directory entries from sys.path)r   r   r   r   isdirr   r   	TypeError)dr   r   r	   r	   r
   _init_pathinfo   s    	r"   c             C   s  | d k r t    } d } n d } t j j |  |  } y t | d  } Wn t k
 rd d SYn X| _xWt |  D]I\ } } | j d  r qy n  y | j d  r t |  wy n  | j	   } t
 |  |  \ } }	 |	 | k rt j j |  rt j j |  | j |	  n  Wqy t k
 rt d j | d |  d	 t j d d l }
 xK |
 j t j     D]4 } x+ | j   D] } t d
 | d	 t j qWqqWt d d	 t j PYqy Xqy WWd QX| rd } n  | S)zProcess a .pth file within the site-packages directory:
       For each line in the file, either combine it with sitedir to a path
       and add that to known_paths, or execute it if it starts with 'import '.
    N   r   r#import import	z"Error processing line {:d} of {}:
filez  z
Remainder of file ignored)r&   r'   )r"   r   r   r   openr   	enumerate
startswithexecrstripr   existsr   r   r   	Exceptionprintformatstderr	tracebackformat_exceptionexc_info
splitlines)sitedirnamer   resetfullnamefnliner   r   r3   recordr	   r	   r
   
addpackage   sD    			

	r?   c             C   s   | d k r t    } d } n d } t |   \ }  } | | k rb t j j |   | j |  n  y t j |   } Wn t k
 r d SYn Xd d   | D } x$ t	 |  D] } t
 |  | |  q W| r d } n  | S)zTAdd 'sitedir' argument to sys.path if missing and handle .pth files in
    'sitedir'Nr#   r   c             S   s%   g  |  ] } | j  d   r |  q S)z.pth)endswith).0r8   r	   r	   r
   
<listcomp>   s   	 zaddsitedir.<locals>.<listcomp>)r"   r   r   r   r   r   r   listdirr   sortedr?   )r7   r   r9   Zsitedircasenamesr8   r	   r	   r
   
addsitedir   s$    				rF   c               C   s   t  j j r d St t d  rM t t d  rM t j   t j   k rM d Sn  t t d  r t t d  r t j   t j   k r d Sn  d S)a,  Check if user site directory is safe for inclusion

    The function tests for the command line flag (including environment var),
    process uid/gid equal to effective uid/gid.

    None: Disabled for security reasons
    False: Disabled by user (command line option)
    True: Safe and enabled
    FgetuidgeteuidNgetgidgetegidT)	r   flagsno_user_sitehasattrr   rH   rG   rJ   rI   r	   r	   r	   r
   check_enableusersite   s    
rN   c              C   s0   t  d k	 r t  Sd d l m }  |  d  a  t  S)zReturns the `user base` directory path.

    The `user base` directory can be used to store data. If the global
    variable ``USER_BASE`` is not initialized yet, this function will also set
    it.
    Nr   )get_config_varZuserbase)	USER_BASE	sysconfigrO   )rO   r	   r	   r
   getuserbase   s
    rR   c              C   s   t    }  t d k	 r t Sd d l m } t j d k rj d d l m } | d  rj | d d  a t Sn  | d d	 t j  a t S)
zReturns the user-specific site-packages directory path.

    If the global variable ``USER_SITE`` is not initialized yet, this
    function will also set it.
    Nr   )get_pathdarwin)rO   PYTHONFRAMEWORKZpurelibZosx_framework_userz%s_user)	rR   	USER_SITErQ   rS   r   platformrO   r   r8   )	user_baserS   rO   r	   r	   r
   getusersitepackages   s    	rY   c             C   s   t    } t r1 t j j |  r1 t | |   n  t r x_ d D]T } t j j t | d t j	 d d  d  } t j j |  r> t | |   q> q> Wn  |  S)zAdd a per user site-package to sys.path

    Each user has its own python directory with site-packages in the
    home directory.
    lib	local/libpythonN   zdist-packages)rZ   r[   )
rY   ENABLE_USER_SITEr   r   r   rF   r   rP   r   version)r   	user_siteZdist_libdirr	   r	   r
   addusersitepackages  s    		ra   c          	   C   s  g  } t    } |  d k r$ t }  n  x|  D]} | s+ | | k rJ q+ n  | j |  t j d k rgd t j k s t j t j k r | j	 t j
 j | d d t j d d  d   n  | j	 t j
 j | d d t j d d  d	   | j	 t j
 j | d d
 d	   | j	 t j
 j | d d t j d d  d	   | j	 t j
 j | d d   n, | j	 |  | j	 t j
 j | d d   t j d k r+ d d l m } | d  } | r| j	 t j
 j d | t j d d  d   qq+ q+ W| S)a=  Returns a list containing all global site-packages directories
    (and possibly site-python).

    For each directory present in ``prefixes`` (or the global ``PREFIXES``),
    this function will find its `site-packages` subdirectory depending on the
    system environment, and will return a list of full paths.
    N/ZVIRTUAL_ENVrZ   r\   r]   zsite-packagesz	local/libzdist-packagesZpython3zdist-pythonrT   r   )rO   rU   z/Library)r   PREFIXESr   r   sepenvironr   base_prefixprefixr   r   r   r_   rW   rQ   rO   )prefixesZsitepackagesseenrg   rO   Z	frameworkr	   r	   r
   getsitepackages,  sB    		!


"$rj   c             C   sh   xa t  |  D]S } t j j |  r d | k rP d d l } | j d t  n  t | |   q q W|  S)z8Add site-packages (and possibly site-python) to sys.pathzsite-pythonr   Nz>"site-python" directories will not be supported in 3.5 anymore)rj   r   r   r   warningswarnDeprecationWarningrF   )r   rh   r7   rk   r	   r	   r
   addsitepackages]  s    	
rn   c              C   sd   t  j d k r d }  n t  j d k r0 d }  n d }  t j d |   t _ t j d |   t _ d S)	zDefine new builtins 'quit' and 'exit'.

    These are objects which make the interpreter exit when called.
    The repr of each object contains a hint at how it works.

    :zCmd-Q\zCtrl-Z plus ReturnzCtrl-D (i.e. EOF)quitexitN)r   rd   _sitebuiltinsZQuitterbuiltinsrq   rr   )Zeofr	   r	   r
   setquitj  s    		ru   c              C   s   t  j d t j  t _ t j d d  d k rI t  j d d  t _ n t  j d d  t _ g  g  }  } t t d  r t j	 j
 t j  } |  j d	 d
 g  | j t j	 j | t j  | t j g  n  t  j d d |  |  t _ d S)z)Set 'copyright' and 'credits' in builtins	copyrightN   Zjavacreditsz?Jython is maintained by the Jython developers (www.jython.org).z    Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
    for supporting Python development.  See www.python.org for more information.r   zLICENSE.txtZLICENSElicensez&See http://www.python.org/psf/license/)rs   Z_Printerr   rv   rt   rW   rx   rM   r   r   dirnamer   extendr   pardircurdirry   )Zfilesdirsherer	   r	   r
   setcopyright|  s     	.r   c               C   s   t  j   t _ d  S)N)rs   Z_Helperrt   helpr	   r	   r	   r
   	sethelper  s    r   c              C   s   d d   }  |  t  _ d S)ai  Enable default readline configuration on interactive prompts, by
    registering a sys.__interactivehook__.

    If the readline module can be imported, the hook will set the Tab key
    as completion key and register ~/.python_history as history file.
    This can be overriden in the sitecustomize or usercustomize module,
    or in a PYTHONSTARTUP file.
    c              S   s  d d  l  }  y d d  l } d d  l } Wn t k
 r@ d  SYn Xt | d d  } | d  k	 r{ d | k r{ | j d  n | j d  y | j   Wn t k
 r Yn X| j   d k rt	 j
 j t	 j
 j d  d  } y | j |  Wn t k
 rYn X|  j | j |  n  d  S)	Nr   __doc__ Zlibeditzbind ^I rl_completeztab: complete~z.python_history)atexitreadlinerlcompleterImportErrorr   Zparse_and_bindZread_init_filer   Zget_current_history_lengthr   r   r   
expanduserZread_history_fileIOErrorregisterZwrite_history_file)r   r   r   Zreadline_docZhistoryr	   r	   r
   register_readline  s,    		z,enablerlcompleter.<locals>.register_readlineN)r   Z__interactivehook__)r   r	   r	   r
   enablerlcompleter  s    	'r   c              C   s   t  j d k r d d l }  d d l } |  j d  } | j d  r y | j |  Wq t k
 r d d l } | j	 | j
 | <d | j j | <Yq Xq n  d S)zOn Windows, some default encodings are not provided by Python,
    while they are always available as "mbcs" in each locale. Make
    them usable by aliasing to "mbcs" in such a case.Zwin32r   NFZcpmbcs)r   rW   _bootlocalecodecsZgetpreferredencodingr+   lookupLookupError	encodings_unknown_cachealiases)r   r   encr   r	   r	   r
   	aliasmbcs  s    r   z,^(?P<key>(\w|[-_])+)\s*=\s*(?P<value>.*)\s*$c             C   s  t  j } t j d k r4 d | k r4 t  j d } n	 t j } t  j j t  j j |   \ } } t  j j |  } d  t _	 d } d d   t  j j
 | |  t  j j
 | |  f D } | rd d  l } | j t  }	 | d }
 d } t |
   } x | D] } | j   } |	 j |  } | r | j   } | d j   | d	 } } | d
 k ri| j   } q| d k r| t _	 qq q WWd  QX| t _ t _ t |  t j g  | d k rt j d t j  qt j g a d a n  |  S)NrT   Z__PYVENV_LAUNCHER__z
pyvenv.cfgc             S   s(   g  |  ] } t  j j |  r |  q Sr	   )r   r   isfile)rA   Zconffiler	   r	   r
   rB     s   	 zvenv.<locals>.<listcomp>r   Ztruekeyvaluezinclude-system-site-packagesZhomeF)r   re   r   rW   
executabler   splitr   rz   Z_homer   recompileCONFIG_LINEr)   stripmatch	groupdictlowerrg   exec_prefixrn   rc   insertr^   )r   envr   Zexe_dir_Zsite_prefixZconf_basenameZcandidate_confsr   Zconfig_lineZvirtual_confZsystem_siter;   r=   r   r!   r   r   r	   r	   r
   venv  sD    		$		
	r   c              C   s   y d d l  }  Wn~ t k
 r$ Ynm t k
 r } zM t j j d  r^ t j t j     n  t j	 j
 d | j j | f  WYd d } ~ Xn Xd S)z,Run custom site specific code, if available.r   NPYTHONVERBOSEz@Error in sitecustomize; set PYTHONVERBOSE for traceback:
%s: %s
)sitecustomizer   r/   r   re   getr   
excepthookr5   r2   write	__class____name__)r   errr	   r	   r
   execsitecustomize  s    	r   c              C   s   y d d l  }  Wn~ t k
 r$ Ynm t k
 r } zM t j j d  r^ t j t j     n  t j	 j
 d | j j | f  WYd d } ~ Xn Xd S)z,Run custom user specific code, if available.r   Nr   z@Error in usercustomize; set PYTHONVERBOSE for traceback:
%s: %s
)usercustomizer   r/   r   re   r   r   r   r5   r2   r   r   r   )r   r   r	   r	   r
   execusercustomize  s    	r   c              C   s   t    t   }  t |   }  t d k r4 t   a n  t |   }  t |   }  t   t   t	   t
   t   t   t r t   n  d S)zAdd standard site-specific directories to the module search path.

    This function is called automatically when this module is imported,
    unless the python interpreter was started with the -S flag.
    N)r   r   r   r^   rN   ra   rn   ru   r   r   r   r   r   r   )r   r	   r	   r
   main/  s    	r   c              C   s  d }  t  j d d   } | s t   } t   } t d  x" t  j D] } t d | f  qE Wt d  t d | t j j |  r d n d f  t d	 | t j j |  r d n d f  t d
 t  t  j	 d  n  g  } d | k r| j
 t  n  d | k r| j
 t  n  | rt t j j |   t rPt  j	 d  qt d k rlt  j	 d  qt d  k rt  j	 d  qt  j	 d  n@ d d  l } t | j |  t  j d t j f   t  j	 d  d  S)Na      %s [--user-base] [--user-site]

    Without arguments print some useful information
    With arguments print the value of USER_BASE and/or USER_SITE separated
    by '%s'.

    Exit codes with --user-base or --user-site:
      0 - user site directory is enabled
      1 - user site directory is disabled by user
      2 - uses site directory is disabled by super user
          or for security reasons
     >2 - unknown error
    r#   zsys.path = [z    %r,]zUSER_BASE: %r (%s)r.   zdoesn't existzUSER_SITE: %r (%s)zENABLE_USER_SITE: %rr   z--user-basez--user-siteF   r]   
   )r   argvrR   rY   r0   r   r   r   r^   rr   r   rP   rV   pathsepr   textwrapZdedent)r   argsrX   r`   r   bufferr   r	   r	   r
   _scriptL  s@    		

	#	#'r   __main__)%r   r   r   rt   rs   rg   r   rc   r^   rV   rP   r   r   r   r"   r?   rF   rN   rR   rY   ra   rj   rn   ru   r   r   r   r   r   r   r   r   r   rK   no_siter   r   r	   r	   r	   r
   <module>K   sF   	*125
3