³ò
	 Pc           @   s   d  Z  d d k Z d d k Z d d k Z y d d k l Z Wn) e j
 o d e f d „  ƒ  YZ n Xe Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d e f d „  ƒ  YZ d e d e i e i e i d „ Z d e d e d „ Z d „  Z d „  Z d „  Z e d „ Z d „  Z d S(   s”   Misc. useful functionality used by the rest of this package.

This module provides common functionality used by the other modules in
this package.

iÿÿÿÿN(   t   CalledProcessErrorR    c           B   s    e  Z d  Z d „  Z d „  Z RS(   s¨   This exception is raised when a process run by check_call() returns
        a non-zero exit status.  The exit status will be stored in the
        returncode attribute.c         C   s   | |  _  | |  _ d  S(   N(   t
   returncodet   cmd(   t   selfR   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   __init__   s    	c         C   s   d |  i  |  i f S(   Ns-   Command '%s' returned non-zero exit status %d(   R   R   (   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   __str__   s    (   t   __name__t
   __module__t   __doc__R   R   (    (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyR       s   	c         G   s   t  i |  | IJd S(   s   Print a message to stderr.N(   t   syst   stderr(   t   msgt   args(    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   notify!   s    c         G   s    t  o t i |  | IJn d S(   s6   Print a debug message to stderr when DEBUG is enabled.N(   t   DEBUGR	   R
   (   R   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   debug%   s    c         G   s   t  i d I|  | IJd S(   s!   Print an error message to stderr.s   ERROR:N(   R	   R
   (   R   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   error*   s    c         G   s   t  i d I|  | IJd S(   s"   Print a warning message to stderr.s   warning:N(   R	   R
   (   R   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   warn.   s    c         G   s   t  |  | Œ t i d ƒ d S(   s6   Print as error message to stderr and exit the program.i   N(   R   R	   t   exit(   R   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   die2   s    t   ProgressIndicatorc           B   sV   e  Z d  Z d Z d e i d „ Z d „  Z d „  Z d e
 d	 „ Z d
 e
 d „ Z RS(   s¯   Simple progress indicator.

    Displayed as a spinning character by default, but can be customized
    by passing custom messages that overrides the spinning character.

    t   |t   /t   -s   \t    c         C   s1   d |  _  | |  _ d |  _ | |  _ g  |  _ d S(   s?   Create a new ProgressIndicator, bound to the given file object.i    N(   t   nt   ft   prev_lent   prefixt   prefix_lens(   R   R   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyR   C   s
    				c         C   s,   |  i  i t |  i ƒ ƒ |  i | 7_ d S(   s.   Append the given prefix onto the prefix stack.N(   R   t   appendt   lenR   (   R   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt
   pushprefixK   s    c         C   s#   |  i  i ƒ  } |  i |  |  _ d S(   s-   Remove the last prefix from the prefix stack.N(   R   t   popR   (   R   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt	   popprefixP   s    c         C   s˜   | d j o! |  i |  i t |  i ƒ } n |  i | } |  i d |  i | f It | i ƒ  ƒ |  _ | o |  i Jd |  _ n |  i d 7_ d S(   s2   Indicate progress, possibly with a custom message.s   %-*si    i   N(   t   Nonet   StatesR   R   R   R   R   t
   expandtabs(   R   R   t   lf(    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   __call__U   s    !t   donec         C   s%   | o d |  _  n |  | t ƒ d S(   s4   Finalize progress indication with the given message.R   N(   R   t   True(   R   R   t   noprefix(    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   finisha   s    (   R   R   R   s   \N(   R   R   R   R$   R	   t   stdoutR   R    R"   R#   t   FalseR'   R+   (    (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyR   8   s   		c         C   sp   d
 } | d
 j	 o  t i i ƒ  } | i | ƒ n t i |  d d d | d | d | d | d | d | d	 t ƒS(   s   Start the given command, and return a subprocess object.

    This provides a simpler interface to the subprocess module.

    t   bufsizei   t   stdinR,   R
   t   cwdt   shellt   envt   universal_newlinesN(   R#   t   ost   environt   copyt   updatet
   subprocesst   PopenR)   (   R   R0   R1   t   add_envR/   R,   R
   R2   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   start_commandh   s    c   	      C   s“   t  |  | | | ƒ } | i ƒ  \ } } | i } | o$ | o t d d i |  ƒ | ƒ n | o$ | o t d d i |  ƒ | ƒ n | | | f S(   s›  Run the given command to completion, and return its results.

    This provides a simpler interface to the subprocess module.

    The results are formatted as a 3-tuple: (exit_code, output, errors)

    If flag_error is enabled, Error messages will be produced if the
    subprocess terminated with a non-zero exit code and/or stderr
    output.

    The other arguments are passed on to start_command().

    s   '%s' returned errors:
---
%s---t    s   '%s' returned exit code %i(   R;   t   communicateR   R   t   join(	   R   R0   R1   R:   t
   flag_errort   processt   outputt   errorst	   exit_code(    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   run_commandy   s    	c          O   s   t  i |  | Ž  i ƒ  S(   sÓ   Run command with arguments.  Wait for command to complete, then
    return the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example:

    retcode = call(["ls", "-l"])
    (   R8   R9   t   wait(   t	   popenargst   kwargs(    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   call“   s    c          O   sW   t  |  | Ž  } | o= | i d ƒ } | d j o |  d } n t | | ƒ ‚ n d S(   sS  Run command with arguments.  Wait for command to complete.  If
    the exit code was zero then return, otherwise raise
    CalledProcessError.  The CalledProcessError object will have the
    return code in the returncode attribute.

    The arguments are the same as for the Popen constructor.  Example:

    check_call(["ls", "-l"])
    R   i    N(   RH   t   getR#   R    (   RF   RG   t   retcodeR   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt
   check_callŸ   s    
c          O   s¡   d | j o t  d ƒ ‚ n t i d t i |  | Ž } | i ƒ  \ } } | i ƒ  } | o@ | i d ƒ } | d j o |  d } n t i | | ƒ ‚ n | S(   s  Run command with arguments and return its output as a byte string.

    If the exit code was non-zero it raises a CalledProcessError.  The
    CalledProcessError object will have the return code in the returncode
    attribute and output in the output attribute.

    The arguments are the same as for the Popen constructor.  Example:

    >>> check_output(["ls", "-l", "/dev/null"])
    'crw-rw-rw- 1 root root 1, 3 Oct 18  2007 /dev/null\n'

    The stdout argument is not allowed as it is used internally.
    To capture standard error in the result, use stderr=STDOUT.

    >>> check_output(["/bin/sh", "-c",
    ...               "ls -l non_existent_file ; exit 0"],
    ...              stderr=STDOUT)
    'ls: non_existent_file: No such file or directory\n'
    R,   s3   stdout argument not allowed, it will be overridden.R   i    N(	   t
   ValueErrorR8   R9   t   PIPER=   t   pollRI   R#   R    (   RF   RG   R@   RA   t
   unused_errRJ   R   (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   check_output³   s    c            s   ‡  f d †  } | S(   s  Decorator for simplifying reading of files.

    If missing_ok is True, a failure to open a file for reading will
    not raise the usual IOError, but instead the wrapped method will be
    called with f == None.  The method must in this case properly
    handle f == None.

    c            s   ‡ ‡  f d †  } | S(   s9  Teach given method to handle both filenames and file objects.

        The given method must take a file object as its second argument
        (the first argument being 'self', of course).  This decorator
        will take a filename given as the second argument and promote
        it to a file object.

        c            sœ   t  | t ƒ o
 | } n> y t | d ƒ } Wn' t j
 o ˆ  o
 d  } qW ‚  n Xz ˆ |  | | | Ž SWd  t  | t ƒ o | o | i ƒ  n Xd  S(   Nt   r(   t
   isinstancet   filet   opent   IOErrorR#   t   close(   R   t   filenameR   RG   R   (   t
   missing_okt   method(    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   _wrapped_methodæ   s    

(    (   RY   RZ   (   RX   (   RY   sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   _wrapÝ   s    	(    (   RX   R[   (    (   RX   sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   file_reader_methodÔ   s    	c            s   ‡  f d †  } | S(   s\  Decorator for simplifying writing of files.

    Enables the given method to handle both filenames and file objects.

    The given method must take a file object as its second argument
    (the first argument being 'self', of course).  This decorator will
    take a filename given as the second argument and promote it to a
    file object.

    c            sœ   t  | t ƒ o
 | } nF t i i | ƒ } t i i | ƒ p t i | ƒ n t | d ƒ } z ˆ  |  | | | Ž SWd  t  | t ƒ p | i ƒ  n Xd  S(   Nt   w(	   RR   RS   R4   t   patht   dirnamet   isdirt   makedirsRT   RV   (   R   RW   R   RG   R   t
   parent_dir(   RY   (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   _new_method  s    
(    (   RY   Rc   (    (   RY   sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pyt   file_writer_methodú   s    (   R   R	   R4   R8   R    t   ImportErrort	   ExceptionR-   R   R   R   R   R   R   t   objectR   R#   RM   R;   R)   RD   RH   RK   RP   R\   Rd   (    (    (    sA   /usr/local/lib/python2.5/site-packages/git_remote_helpers/util.pys   <module>   s0   					0					!&