
Y_Sc        	   @  s  d  Z  d d k l Z d Z d Z d Z d i e e e   Z	 d d k
 Z
 d d k Z d d k Z d d k Z d	 e f d
     YZ d   Z d   Z d d  Z d d d d  Z d d d d  Z d d d d  Z d   Z e d j o e i e e i   n d S(   s  Find the full path to commands.

which(command, path=None, verbose=0, exts=None)
    Return the full path to the first match of the given command on the
    path.

whichall(command, path=None, verbose=0, exts=None)
    Return a list of full paths to all matches of the given command on
    the path.

whichgen(command, path=None, verbose=0, exts=None)
    Return a generator which will yield full paths to all matches of the
    given command on the path.
    
By default the PATH environment variable is searched (as well as, on
Windows, the AppPaths key in the registry), but a specific 'path' list
to search may be specified as well.  On Windows, the PATHEXT environment
variable is applied as appropriate.

If "verbose" is true then a tuple of the form
    (<fullpath>, <matched-where-description>)
is returned for each match. The latter element is a textual description
of where the match was found. For example:
    from PATH element 0
    from HKLM\SOFTWARE\...\perl.exe
i(   t   print_functions  
    Show the full path of commands.

    Usage:
        which [<options>...] [<command-name>...]

    Options:
        -h, --help      Print this help and exit.
        -V, --version   Print the version info and exit.

        -a, --all       Print *all* matching paths.
        -v, --verbose   Print out how matches were located and
                        show near misses on stderr.
        -q, --quiet     Just print out matches. I.e., do not print out
                        near misses.

        -p <altpath>, --path=<altpath>
                        An alternative path (list of directories) may
                        be specified for searching.
        -e <exts>, --exts=<exts>
                        Specify a list of extensions to consider instead
                        of the usual list (';'-separate list, Windows
                        only).

    Show the full path to the program that would be run for each given
    command name, if any. Which, like GNU's which, returns the number of
    failed arguments, or -1 when no <command-name> was given.

    Near misses include duplicates, non-regular files and (on Un*x)
    files without executable access.
s/   $Id: which.py 430 2005-08-20 03:11:58Z trentm $i   i    t   .Nt
   WhichErrorc           B  s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyR   Q   s   c         C  s   d } t i i d  o t i i |   d i   d j o |  d 7}  n d d k } y3 d |  } | i	 | i
 |  } | d | f } Wn | i j
 o n X| o" t i i | d  o
 d } q n | S(	   sA   Windows allow application paths to be registered in the registry.t   wini   s   .exeiNs4   SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\s
   from HKLM\i    (   t   Nonet   syst   platformt
   startswitht   ost   patht   splitextt   lowert   _winregt
   QueryValuet   HKEY_LOCAL_MACHINEt   errort   exists(   t   exeNamet
   registeredR   t   keyt   value(    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt   _getRegisteredExecutableX   s    #c         C  se   t  i i d  o; t i i t i i |    t i i t i i |   j St i i |  |  Sd  S(   NR   (   R   R   R	   R
   R   t   normpatht   normcaset   samefile(   t   fname1t   fname2(    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt	   _samefilej   s     c         C  s   x | D]B } t  |  d | d  o$ | o t i i d |   n d Sq Wt i t i |  d  i  p# | o t i i d |   q nO t i	 |  d t i
  p# | o t i i d |   q n | i |   |  Sd S(   s  Cull inappropriate matches. Possible reasons:
        - a duplicate of a previous match
        - not a disk file
        - not executable (non-Windows)
    If 'potential' is approved it is returned and added to 'matches'.
    Otherwise, None is returned.
    i    s   duplicate: %s (%s)
s   not a regular file: %s (%s)
s   no executable access: %s (%s)
N(   R   R   t   stderrt   writeR   t   statt   S_ISREGR
   t   st_modet   accesst   X_OKt   append(   t	   potentialt   matchest   verboset   match(    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt   _cullq   s     	 c         c  s5  g  } | d j oU d } t i i d d  i t i  } t i i d  o | i	 d t i
  qn n d } t i i d  o | d j o] t i i d d  i t i  } xY | D] } | i   d j o Pq q Wd d	 d
 g } q9t | t  p t d   q9n+ | d j	 o t d t i   n g  } t i |  j p t i o t i |  j o nxtt t |   D]`} | | } t i i d  oF t |  d j o3 | d d j o" | d d j o | d d !} n x d g | D] } t i i t i i t i i | |  |    }	 t i i |	  o | o d | }
 nG t i i d  p d | }
 n& | d j o
 d }
 n d | d }
 t |	 |
 f | |  } | o | o	 | Vq| d VqqqWqzWt |   } | d j	 o: t | | |  } | o | o	 | Vq-| d Vq1n d S(   s  Return a generator of full paths to the given command.
    
    "command" is the name of the executable to search for.
    "path" is an optional alternate path list to search. The default is
        to use the PATH environment variable.
    "verbose", if true, will cause a 2-tuple to be returned for each
        match. The second element is a textual description of where the
        match was found.
    "exts" optionally allows one to specify a list of extensions to use
        instead of the standard list for this system. This can
        effectively be used as an optimization to, for example, avoid
        stat's of "foo.vbs" when searching for "foo" and you know it is
        not a VisualBasic script but ".vbs" is on PATHEXT. This option
        is only supported on Windows.

    This method returns a generator which yields either full paths to
    the given command or, if verbose, tuples of the form (<path to
    command>, <where path found>).
    i    t   PATHt    R   i   t   PATHEXTs   .exes   .COMs   .EXEs   .BATs&   'exts' argument must be a list or Nones1   'exts' argument is not supported on platform '%s'i   t   "is   from given path element %ds   from PATH element %ds   from current directoryN(   R   R
   t   environt   gett   splitt   pathsepR   R   R	   t   insertt   curdirR   t
   isinstancet   listt	   TypeErrorR   t   sept   altsept   ranget   lenR   t   abspathR   t   joint   isfileR*   R   (   t   commandR   R(   t   extsR'   t   usingGivenPatht   extt   it   dirNamet   absNamet	   fromWhereR)   (    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt   whichgen   sj    !! 	* 
&" 	(
		c      	   C  sI   y t  t |  | | |   } Wn# t j
 o t d |    n X| S(   s^  Return the full path to the first match of the given command on
    the path.
    
    "command" is a the name of the executable to search for.
    "path" is an optional alternate path list to search. The default it
        to use the PATH environment variable.
    "verbose", if true, will cause a 2-tuple to be returned. The second
        element is a textual description of where the match was found.
    "exts" optionally allows one to specify a list of extensions to use
        instead of the standard list for this system. This can
        effectively be used as an optimization to, for example, avoid
        stat's of "foo.vbs" when searching for "foo" and you know it is
        not a VisualBasic script but ".vbs" is on PATHEXT. This option
        is only supported on Windows.

    If no match is found for the command, a WhichError is raised.
    s    Could not find '%s' on the path.(   t   nextRG   t   StopIterationR   (   R?   R   R(   R@   R)   (    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt   which   s
    c         C  s   t  t |  | | |   S(   s3  Return a list of full paths to all matches of the given command
    on the path.  

    "command" is a the name of the executable to search for.
    "path" is an optional alternate path list to search. The default it
        to use the PATH environment variable.
    "verbose", if true, will cause a 2-tuple to be returned for each
        match. The second element is a textual description of where the
        match was found.
    "exts" optionally allows one to specify a list of extensions to use
        instead of the standard list for this system. This can
        effectively be used as an optimization to, for example, avoid
        stat's of "foo.vbs" when searching for "foo" and you know it is
        not a VisualBasic script but ".vbs" is on PATHEXT. This option
        is only supported on Windows.
    (   R6   RG   (   R?   R   R(   R@   (    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt   whichall   s    c         C  sF  d } d } d  } d  } y8 t i |  d d d d d d d d	 d
 g  \ } } WnC t i j
 o4 } t i i d | |  f  t i i d  d SXx | D] \ } }	 | d  j o t t  d S| d! j o t d t  d S| d" j o
 d } q | d# j o
 d } q | d$ j o
 d } q | d% j o' |	 o |	 i	 t
 i  } qg  } q | d& j o' |	 o |	 i	 t
 i  } qg  } q q Wt |  d j o d Sd }
 x | D] } d } x_ t | d | d | d | D]? } | o t d |  n t |  | d 7} | p PqqW| p |
 d 7}
 qqW|
 S('   Ni    i   s	   haVvqp:e:t   helpt   allt   versionR(   t   quiets   path=s   exts=s*   which: error: %s. Your invocation was: %s
s   Try 'which --help'.
s   -hs   --helps   -Vs	   --versions   which %ss   -as   --alls   -vs	   --verboses   -qs   --quiets   -ps   --paths   -es   --extsiR   R@   s   %s (%s)(   s   -hs   --help(   s   -Vs	   --version(   s   -as   --all(   s   -vs	   --verbose(   s   -qs   --quiet(   s   -ps   --path(   s   -es   --exts(   R   t   getoptt   GetoptErrorR   R   R   t   printt   _cmdlnUsaget   __version__R1   R
   R2   R;   RG   (   t   argvRM   R(   t   altpathR@   t   optlistt   argst   msgt   optt   optargt   failurest   argt   nmatchesR)   (    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt   main  sf    ( 




  

	t   __main__(   i   i   i    (   t   __doc__t
   __future__R    RS   t   __revision__t   __version_info__R=   t   mapt   strRT   R
   R   RP   R    t	   ExceptionR   R   R   R*   R   RG   RJ   RK   R_   R   t   exitRU   (    (    (    sB   /home/psgendb/BIRCHDEV/install/weblogo-3.4/corebio/utils/_which.pyt   <module>"   s&    		W	9