o

������g�&����������������������@���s����d�Z�ddlmZ�ddlmZ�ddlmZ�ddlZddlZddlZddlZddl	m
Z
�ddlmZ�d	d
��Z
dd��Zd$d
d�Zdd��Zdd��Zdd��Zg�d�Zdd��Zdd��Zdd��ZdZdd��Z		d%d d!�Zd$d"d#�ZdS�)&z,
Common public utilities for manic package

�����)�absolute_import)�unicode_literals)�print_functionN)�Timer����)�LOCAL_PATH_INDICATORc�����������������C���s"���|���d�}�|�D�]}t�|��qdS�)z�Log each line of process output at debug level so it can be
    filtered if necessary. By default, output is a single string, and
    logging.debug(output) will only put log info heading on the first
    line. This makes it hard to filter with grep.

    �
N)�split�logging�debug)�output�line��r����S/lfs/h1/nco/ptmp/ethan.collins/aqm/aqm.v7.0.11/sorc/manage_externals/manic/utils.py�log_process_output���s���
�r���c�����������������K���s6���t��|���|rt|�fi�|���nt|���tj����dS�)zgWrapper script around print to ensure that everything printed to
    the screen also gets logged.

    N)r
����info�print�sys�stdout�flush)�msg�kwargsr���r���r����printlog&���s
���
r���c�����������������C���sN���|���d�}t|�|kr|�}|S�||�d��}d�|�}|r#|d�|�}|}|S�)av��Returns the last n lines of the given string

    Args:
        the_string: str
        n_lines: int
        truncation_message: str, optional

    Returns a string containing the last n lines of the_string

    If truncation_message is provided, the returned string begins with
    the given message if and only if the string is greater than n lines
    to begin with.
    TN��r���)�
splitlines�len�join)�
the_stringZn_lines�truncation_message�linesZ
return_valZlines_subsetZ
str_truncatedr���r���r����last_n_lines3���s���
�
r ���c��������������������s.���|���d�}d|�����fdd�|D��}d�|�S�)a&��Indents the given string by a given number of spaces

    Args:
       the_string: str
       indent_level: int

    Returns a new string that is the same as the_string, except that
    each line is indented by 'indent_level' spaces.

    In python3, this can be done with textwrap.indent.
    T� c��������������������s���g�|�]}��|��qS�r���r���)�.0r
����Zpaddingr���r����
<listcomp>^���s����z!indent_string.<locals>.<listcomp>r���)r���r���)r����indent_levelr���Zlines_indentedr���r#���r����
indent_stringO���s���


r&���c�����������������C���s���t��|���td�tj|����)z
    Error output function
    z
{0}ERROR: {1})r
����error�RuntimeError�format�os�linesep)�messager���r���r����fatal_errorh���s���
r-���c�����������������C���sD���d}|�����}|dv�r
d}n|dv�rd}|du�r d�|��}t|��|S�)zrConvert a sting representation of as boolean into a true boolean.

    Conversion should be case insensitive.
    N)�true�tT)�false�fFzDERROR: invalid boolean string value "{0}". Must be "true" or "false")�lowerr)���r-���)Zbool_str�valueZ	str_lowerr���r���r���r����str_to_boolu���s����r4���)zhttp://zhttps://zssh://zgit@c�����������������C���s ���d}t�D�]	}|��|�r
d}q|S�)z�check if the user provided a local file path instead of a
       remote. If so, it must be expanded to an absolute
       path.

    FT)�REMOTE_PREFIXES�
startswith��url�
remote_url�prefixr���r���r����
is_remote_url����s���
�r;���c�����������������C���sV���t�|��}|s|�S�tD�]}|��|d�}�q
d|�v�r|��d�d�}�d|�v�r)|��d�d�}�|�S�)zqcheck if the user provided a local file path or a
       remote. If remote, try to strip off protocol info.

    r����@r����:)r;���r5����replacer	���r7���r���r���r����split_remote_url����s���r?���c�����������������C���sj���t�|��}|s3|����tkr	�|�S�tj�|��}�tj�|��}�tj�|��s-d�||��}t	|��|�S�tj�
|��}�|�S�)a��check if the user provided a local file path instead of a
    remote. If so, it must be expanded to an absolute
    path.

    Note: local paths of LOCAL_PATH_INDICATOR have special meaning and
    represent local copy only, don't work with the remotes.

    z�WARNING: Externals description for "{0}" contains a url that is not remote and does not expand to an absolute path. Version control operations may fail.

url={1})r;����stripr���r*����path�
expandvars�
expanduser�isabsr)���r����normpath)r8����fieldr9���r���r���r���r����expand_local_url����s���	���rG���i,��c�����������������C���s���t�dj||�td���d�S�)NaF��

Command '{command}'
from directory {working_directory}
has taken {hanging_sec} seconds. It may be hanging.

The command will continue to run, but you may want to abort
manage_externals with ^C and investigate. A possible cause of hangs is
when svn or git require authentication to access a private
repository. On some systems, svn and git requests for authentication
information will not be displayed to the user. In this case, the program
will appear to hang. Ensure you can run svn and git manually and access
all repositories without entering your authentication information.

)�command�working_directoryZhanging_sec)r���r)����_HANGING_SEC�rI���rH���r���r���r����_hanging_msg����s
����rL���Fc�����������
���
���C���s���t����}d�|�}t�|��d�|��}t�|��|p|}d}d}ttt||d�d�}	|	�	���z�zt
j|�t
jdd�}t
|��d	}W�nw�ty`�}
�ztd
|��}t�|
��t|��W�Y�d}
~
n\d}
~
w�ty�}
�ztd|��}t�|
��t|��W�Y�d}
~
n=d}
~
w�t
jy��}
�z,|s�d
�|
j�}t||�|
jd�}t�|
��t�|��t
|
j��t|��|
j}W�Y�d}
~
nd}
~
ww�W�|	����n|	����w�|r�|r�||f}|S�|r�|}|S�|r�|}|S�d}|S�)a���Wrapper around subprocess.check_output to handle common
    exceptions.

    check_output runs a command with arguments and waits
    for it to complete.

    check_output raises an exception on a nonzero return code.  if
    status_to_caller is true, execute_subprocess returns the subprocess
    return code, otherwise execute_subprocess treats non-zero return
    status as an error and raises an exception.

    z5In directory: {0}
execute_subprocess running command:r!��������r���rK���)r���T)�stderrZuniversal_newlinesr���z4Command execution failed. Does the executable exist?Nz5DEV_ERROR: Invalid arguments trying to run subprocessz5Process did not run successfully; returned status {0})r���)r*����getcwdr)���r
���r���r���r���rJ���rL����start�
subprocessZcheck_outputZSTDOUTr����OSError�failed_command_msgr'���r-����
ValueErrorZCalledProcessError�
returncoder����cancel)
ZcommandsZstatus_to_caller�output_to_caller�cwdr���Zcommands_strZreturn_to_caller�statusr���Z
hanging_timerr'����msg_contextZ	ret_valuer���r���r����execute_subprocess����s����



��
��
��
���


������r[���c�����������������C���s\���|rt�|ddd�}dt|d��d�}nd}d�|�}|d	jt���|�|d
�7�}|r,|d7�}|S�)z�Template for consistent error messages from subprocess calls.

    If 'output' is given, it should provide the output from the failed
    command
    ����z&[... Output truncated for brevity ...])r���zFailed with output:
����z
ERROR: r���r!���z0In directory
    {cwd}
{context}:
    {command}
)rX����contextrH���z*See above for output from failed command.
)r ���r&���r���r)���r*���rO���)rZ���rH���r���Zoutput_truncatedZerrmsgZcommand_strr���r���r���rS���0��s"������
�rS���)N)FF)�__doc__�
__future__r���r���r���r
���r*���rQ���r����	threadingr����global_constantsr���r���r���r ���r&���r-���r4���r5���r;���r?���rG���rJ���rL���r[���rS���r���r���r���r����<module>���s4���	



#
�J