3
_Af<                  @   sh   d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 G dd deZG d	d
 d
eZdS )    )print_functionN)Coloring)PagedCommand)GitError)
GitCommandc               @   s   e Zd Zdd ZdS )GrepColoringc             C   s2   t j| |d | jddd| _| jddd| _d S )Ngrepprojectbold)attrfailred)fg)r   __init__printerr	   r   )selfconfig r   >/home/qytech/356xLinux510-101server/.repo/repo/subcmds/grep.pyr      s    zGrepColoring.__init__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   c               @   s,   e Zd ZdZdZdZdZdd Zdd Zd	S )
GrepTzPrint lines matching a patternz-
%prog {pattern | -e pattern} [<project>...]
a  
Search for the specified patterns in all project files.

# Boolean Options

The following options can appear as often as necessary to express
the pattern to locate:

 -e PATTERN
 --and, --or, --not, -(, -)

Further, the -r/--revision option may be specified multiple times
in order to scan multiple trees.  If the same file matches in more
than one tree, only the first result is reported, prefixed by the
revision name it was found under.

# Examples

Look for a line that has '#define' and either 'MAX_PATH or 'PATH_MAX':

  repo grep -e '#define' --and -\( -e MAX_PATH -e PATH_MAX \)

Look for a line that has 'NODE' or 'Unexpected' in files that
contain a line that matches both expressions:

  repo grep --all-match -e NODE -e Unexpected

c             C   s  dd }|j d}|jdd|dd |jdd	d
dddd |j d}|jdd|dddd |jddd|dd |jddd|dd |jdd|dd |jddd|dd |jd d!d|d"d |jd#d$d|d%d |jd&d'd|d(d |jd)d*d|d+d |j d,}|jd-d|d.d |jd/d0d1d|d2d |jd3d4d|d5d |j d6}|jd7d|d8d |jd9d|d:dd;d |jd<d|d:dd=d |jd>d|d:dd?d |jd@dAdBd|dCd |jdDdEd|dFd d S )GNc             S   sp   t |jdd }|d kr(g }t|jd| |dkr<|jd n|dkrP|jd n
|j| |d k	rl|j| d S )Ncmd_argvz-((z-)))getattrvaluessetattrappend)optionopt_strvalueparserptr   r   r   carryF   s    
zGrep._Options.<locals>.carryZSourcesz--cachedcallbackz*Search the index, instead of the work tree)actionr&   helpz-rz
--revisionrevisionr   ZTREEishz(Search TREEish, instead of the work tree)destr'   metavarr(   Patternz-eZPATTERNstrzPattern to search for)r'   r&   r+   typer(   z-iz--ignore-casezIgnore case differencesz-az--textz)Process binary files as if they were textz-Iz'Don't match the pattern in binary filesz-wz--word-regexpz)Match the pattern only at word boundariesz-vz--invert-matchzSelect non-matching linesz-Gz--basic-regexpz-Use POSIX basic regexp for patterns (default)z-Ez--extended-regexpz&Use POSIX extended regexp for patternsz-Fz--fixed-stringsz*Use fixed strings (not regexp) for patternzPattern Groupingz--all-matchz+Limit match to lines that have all patternsz--andz--orz--notz%Boolean operators to combine patternsz-(z-)zBoolean operator groupingOutputz-nz(Prefix the line number to matching linesz-CZCONTEXTzShow CONTEXT lines around matchz-BzShow CONTEXT lines before matchz-AzShow CONTEXT lines after matchz-lz--name-onlyz--files-with-matchesz.Show only file names containing matching linesz-Lz--files-without-matchz2Show only file names not containing matching lines)add_option_group
add_option)r   pr%   gr   r   r   _OptionsE   s    





zGrep._Optionsc             C   s  t | jjj}dg}|jr$|jd |jt|dg  d|krn|sJ| j  |jd |j|d  |dd  }| j	|}d}t
|dkr|jd d	}d}|jrd
|krtdtjd tjd d	}|j|j |jd d}d}	d}
x|D ]}yt||dd	d	d}W nZ tk
rd } z<d	}|jd|j  |j  |jdt| |j  wW Y d d }~X nX |j dkr|jr|rd|jkrd	}	q|jd|j  |j  |jd|jj  |j  qd	}
|jjd}|dd }|rP|rPx|D ]V}|jdd\}}|jd| |jd |j|j |jd |jd| |j  qW q|rxR|D ]0}|j|j |jd |jd| |j  q\W qx|D ]}t| qW qW |rtjd nX|
rtjd nF|r|	rx"|jD ]}td| tjd qW tjd n
tjd d S )Nr   z--colorr   z-er      Fz--full-nameTz--cachedz-fatal: cannot combine --cached and --revision)filez--)barecapture_stdoutcapture_stderrz--- project %s ---z%szfatal: ambiguous argument
:/zerror: can't search revision %s)r   manifestmanifestProjectr   is_onr   extendr   UsageGetProjectslenr)   printsysstderrexitr   r   r	   relpathnlr   r-   Waitstripstdoutsplitwrite)r   optargsoutr   projectsZ	full_nameZhave_revZ
git_failedZbad_revZ
have_matchr	   r2   erlinerevr   r   r   Execute   s    











zGrep.ExecuteN)	r   r   r   commonhelpSummary	helpUsagehelpDescriptionr4   rX   r   r   r   r   r   "   s   \r   )
__future__r   rF   colorr   commandr   errorr   git_commandr   r   r   r   r   r   r   <module>   s   