
    yf6                     8   S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  S SK	J
r
  SSKJr  SSKJr  SSKJr  S rS*S	 jrS
 r " S S\ R(                  5      r " S S\ R(                  5      r " S S\5      r " S S5      rS r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r\\\\\1r \  V s0 s H  o RB                  U _M     sn r"\" S S!5      r# " S" S#\
5      r$S$ r%S% r&\S&4S' jr'\4S( jr(\4S) jr)gs  sn f )+    N)
namedtuple)Enum   )shellpattern)clean_lines)Errorc                    [        XS9nUR                  [        R                  L a  UR	                  UR
                  5        U$ UR                  [        R                  L a  UR
                  nU$ UR	                  U5        U$ )zKParse a pattern-file line and act depending on which command it represents.)fallback)parse_inclexcl_commandcmd	IECommandRootPathappendvalPatternStyle)linerootsie_commandsr
   
ie_commands        //usr/lib/python3/dist-packages/borg/patterns.pyparse_patternfile_liner      sn    '@J~~+++Z^^$ O 
911	1>> O 	:&O    c                 R    Uc  [         n[        U 5       H  n[        XAX#5      nM     g N)ShellPatternr   r   )fileobjr   r   r
   r   s        r   load_pattern_filer      s(    G$)${M %r   c                 \    [        U 5       H  nUR                  [        U5      5        M     g r   )r   r   parse_exclude_pattern)r   patterns
patternstrs      r   load_exclude_filer"   #   s#    !'*
-j9: +r   c                   6   ^  \ rS rSrSU 4S jjrSS jrSrU =r$ )ArgparsePatternAction(   c                 *   > [         TU ]  " SSU0UD6  g Nnargs super__init__selfr(   kw	__class__s      r   r,   ArgparsePatternAction.__init__)       +u++r   c                 V    [        US   UR                  UR                  [        5        g Nr   )r   pathsr    r   )r.   parserargsvaluesoption_strings        r   __call__ArgparsePatternAction.__call__,   s    vay$**dmm\Rr   r)   r   r   )__name__
__module____qualname____firstlineno__r,   r:   __static_attributes____classcell__r0   s   @r   r$   r$   (   s    ,S Sr   r$   c                   <   ^  \ rS rSrSU 4S jjrSS jrS rSrU =r$ )ArgparsePatternFileAction0   c                 *   > [         TU ]  " SSU0UD6  g r'   r*   r-   s      r   r,   "ArgparsePatternFileAction.__init__1   r2   r   c                     US   n [        U5       nU R                  Xb5        SSS5        g! , (       d  f       g= f! [         a  n[        [	        U5      5      eSnAff = f)zLoad and parse patterns from a file.
Lines empty or starting with '#' after stripping whitespace on both line ends are ignored.
r   N)openparseFileNotFoundErrorr   str)r.   r6   r7   r8   r9   filenamefes           r   r:   "ArgparsePatternFileAction.__call__4   sN     !9	 h1

1#    	 A-	 s)   > -> 
;> > 
A!AA!c                 D    [        XR                  UR                  5        g r   )r   r5   r    r.   fobjr7   s      r   rK   ArgparsePatternFileAction.parse?   s    $

DMM:r   r)   r<   r   )	r=   r>   r?   r@   r,   r:   rK   rA   rB   rC   s   @r   rE   rE   0   s    ,	 ; ;r   rE   c                       \ rS rSrS rSrg)ArgparseExcludeFileActionC   c                 .    [        XR                  5        g r   )r"   r    rS   s      r   rK   ArgparseExcludeFileAction.parseD   s    $.r   r)   N)r=   r>   r?   r@   rK   rA   r)   r   r   rW   rW   C   s    /r   rW   c                   L    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rSrg)PatternMatcherH   zRepresents a collection of pattern objects to match paths against.

*fallback* is a boolean value that *match()* returns if no matching patterns are found.

Nc                     / U l         Xl        0 U l        S U l        SU l        / U l        [        R                  S[        R                  S[        R                  S0U l
        g )NTF)_itemsr
   _path_full_patternsrecurse_dirrecurse_dir_defaultinclude_patternsr   ExcludeExcludeNoRecurseIncludeis_include_cmd)r.   r
   s     r   r,   PatternMatcher.__init__N   sc     ! $& 
   $(  " u&&t
r   c                 x    [        U R                  5      (       + =(       a    [        U R                  5      (       + $ r   )lenr_   r`   r.   s    r   emptyPatternMatcher.emptyi   s'    t{{##IC0H0H,I(IIr   c                     [        U[        5      (       a  UR                  nX R                  U'   gU R                  R                  X45        g)z%*cmd* is an IECommand value.
        N)
isinstancePathFullPatternpatternr`   r_   r   )r.   rq   r   keys       r   _addPatternMatcher._addl   s<     g////C,/$$S)KK~.r   c                 8    U H  nU R                  X25        M     g)zAdd list of patterns to internal list. *cmd* indicates whether the
pattern is an include/exclude pattern, and whether recursion should be
done on excluded folders.
Nrs   )r.   r    r   rq   s       r   addPatternMatcher.addu   s    
  GIIg#  r   c                     U Vs/ s H  n[        U[        5      PM     nnU R                  U[        R                  5        U(       + U l        X0l        gs  snf )zHUsed to add inclusion-paths from args.paths (from commandline).
        N)parse_patternPathPrefixPatternrw   r   rf   r
   rc   )r.   include_pathsprc   s       r   add_includepathsPatternMatcher.add_includepaths}   sN     JWWAM!->?W!9#4#45,, 0 Xs   Ac                     U R                    Vs/ s H.  nUR                  S:X  d  M  [        U[        5      (       a  M,  UPM0     sn$ s  snf )zNote that this only returns patterns added via *add_includepaths* and it
won't return PathFullPattern patterns as we do not match_count for them.
r   )rc   match_countro   rp   )r.   r}   s     r   get_unmatched_include_patterns-PatternMatcher.get_unmatched_include_patterns   sN      00 N0a==A% .8O.L 0 N 	N Ns   AAAc                 <    U H  u  p#U R                  X#5        M     g)zBAdd list of patterns (of type CmdTuple) to internal list.
        Nrv   )r.   r    rq   r   s       r   add_inclexclPatternMatcher.add_inclexcl   s     %LGIIg# %r   c                    [        U5      R                  [        R                  R                  5      n[        5       nU R                  R                  X5      nX2La  [        U5      U l	        U R                  U   $ U R                   H<  u  pEUR                  USS9(       d  M  UR                  U l	        U R                  U   s  $    U R                  U l	        U R                  $ )zReturn True or False depending on whether *path* is matched.

If no match is found among the patterns in this matcher, then the value
in self.fallback is returned (defaults to None).

F)	normalize)normalize_pathlstripospathsepobjectr`   getcommand_recurses_dirra   rg   r_   matchrb   r
   )r.   r   non_existentvaluerq   r   s         r   r   PatternMatcher.match   s     d#**277;;7x((,,T@$3E:D&&u-- #kkNW}}TU}33#*#6#6 **3// *  33}}r   )r_   r`   r
   rc   rg   ra   rb   r   )r=   r>   r?   r@   __doc__r,   rl   rs   rw   r~   r   r   r   rA   r)   r   r   r\   r\   H   s1    

6J/$1N$r   r\   c                 \    [         R                  S:X  a  [        R                  " SU 5      $ U $ )z=normalize paths for MacOS (but do nothing on other platforms)darwinNFD)sysplatformunicodedatar   )r   s    r   r   r      s'     251I;  -StSr   c                   H    \ rS rSrSr\rSS jrSS jrS r	S r
S rS rS	rg
)PatternBase   z3Shared logic for inclusion/exclusion patterns.
    c                 b    Xl         SU l        [        U5      nU R                  U5        X l        g r4   )pattern_origr   r   _preparera   )r.   rq   ra   s      r   r,   PatternBase.__init__   s,    # )g&r   c                     U(       a  [        U5      nU R                  U5      nU(       a  U =R                  S-  sl        U$ )zReturn a boolean indicating whether *path* is matched by this pattern.

If normalize is True (default), the path will get normalized using normalize_path(),
otherwise it is assumed that it already is normalized using that function.
r   )r   _matchr   )r.   r   r   matchess       r   r   PatternBase.match   s8     !$'D++d#!r   c                 8    [        U 5       SU R                   S3$ )N())typerq   rk   s    r   __repr__PatternBase.__repr__   s    t*Qt||nA..r   c                     U R                   $ r   )r   rk   s    r   __str__PatternBase.__str__   s       r   c                     [         e)z$Should set the value of self.patternNotImplementedErrorr.   rq   s     r   r   PatternBase._prepare   s    !!r   c                     [         er   r   r.   r   s     r   r   PatternBase._match   s    !!r   )r   r   ra   N)F)T)r=   r>   r?   r@   r   NotImplementedPREFIXr,   r   r   r   r   r   rA   r)   r   r   r   r      s*    F'/!""r   r   c                   (    \ rS rSrSrSrS rS rSrg)rp      zFull match of a path.pfc                     [         R                  R                  U5      R                  [         R                  R                  5      U l        g r   )r   r   normpathr   r   rq   r   s     r   r   PathFullPattern._prepare   s+    ww''077Dr   c                     XR                   :H  $ r   rq   r   s     r   r   PathFullPattern._match   s    ||##r   r   N	r=   r>   r?   r@   r   r   r   r   rA   r)   r   r   rp   rp      s    FE$r   rp   c                   (    \ rS rSrSrSrS rS rSrg)r{      zLiteral files or directories listed on the command line
for some operations (e.g. extract, but not create).
If a directory is specified, all paths that start with that
path match as well.  A trailing slash makes no difference.
ppc                     [         R                  R                  n[         R                  R                  U5      R	                  U5      U-   R                  U5      U l        g r   )r   r   r   r   rstripr   rq   r.   rq   r   s      r   r   PathPrefixPattern._prepare   s@    ggkk((188=CKKCPr   c                 n    U[         R                  R                  -   R                  U R                  5      $ r   )r   r   r   
startswithrq   r   s     r   r   PathPrefixPattern._match   s$    rww{{"..t||<<r   r   Nr   r)   r   r   r{   r{      s    
 FQ
=r   r{   c                   (    \ rS rSrSrSrS rS rSrg)FnmatchPattern   ~Shell glob patterns to exclude.  A trailing slash means to
exclude the contents of a directory, but not the directory itself.
fmc                    UR                  [        R                  R                  5      (       a  [        R                  R	                  U5      R                  [        R                  R                  5      [        R                  R                  -   S-   [        R                  R                  -   nO=[        R                  R	                  U5      [        R                  R                  -   S-   nUR                  [        R                  R                  5      U l        [        R                  " [        R                  " U R                  5      5      U l        g )N*)endswithr   r   r   r   r   r   rq   recompilefnmatch	translateregexr   s     r   r   FnmatchPattern._prepare  s    BGGKK((gg&&w/66rww{{CbggkkQTWWZ\ZaZaZeZeeGgg&&w/"''++=CG~~bggkk2 ZZ 1 1$,, ?@
r   c                 r    U R                   R                  U[        R                  R                  -   5      S L$ r   r   r   r   r   r   r   s     r   r   FnmatchPattern._match  )    

  !34D@Ar   rq   r   Nr   r)   r   r   r   r      s     F
ABr   r   c                   (    \ rS rSrSrSrS rS rSrg)r   i  r   shc                    [         R                  R                  nUR                  U5      (       a>  [         R                  R	                  U5      R                  U5      U-   S-   U-   S-   U-   nO+[         R                  R	                  U5      U-   S-   U-   S-   nUR                  U5      U l        [        R                  " [        R                  " U R                  5      5      U l        g )Nz**r   )r   r   r   r   r   r   r   rq   r   r   r   r   r   r   s      r   r   ShellPattern._prepare  s    ggkkC  gg&&w/66s;cADH3NQTTWZZGgg&&w/#5<sBSHG~~c*ZZ 6 6t|| DE
r   c                 r    U R                   R                  U[        R                  R                  -   5      S L$ r   r   r   s     r   r   ShellPattern._match$  r   r   r   Nr   r)   r   r   r   r     s     F	FBr   r   c                   (    \ rS rSrSrSrS rS rSrg)RegexPatterni(  z#Regular expression to exclude.
    r   c                 F    Xl         [        R                  " U5      U l        g r   )rq   r   r   r   r   s     r   r   RegexPattern._prepare-  s    ZZ(
r   c                     [         R                  R                  S:w  a*  UR                  [         R                  R                  S5      nU R                  R                  U5      S L$ )N/)r   r   r   replacer   searchr   s     r   r   RegexPattern._match1  sD    77;;#<<S1D

!!$'t34r   r   Nr   r)   r   r   r   r   (  s    F)5r   r   CmdTuplezval cmdc                   ,    \ rS rSrSrSrSrSrSrSr	Sr
g	)
r   iF  z8A command that an InclExcl file line can represent.
    r               r)   N)r=   r>   r?   r@   r   r   r   rf   rd   re   rA   r)   r   r   r   r   F  s!    HLGGr   r   c                 *    U [         R                  4;  $ r   )r   re   )r   s    r   r   r   P  s    y11222r   c                 P     [         U    $ ! [         a    [        SU  35      S ef = f)NzUnknown pattern style: )_PATTERN_CLASS_BY_PREFIXKeyError
ValueError)prefixs    r   get_pattern_classr   U  s9    G'// G26(;<$FGs    %Tc                     [        U 5      S:  a6  U S   S:X  a-  U SS R                  5       (       a  U SS U SS p[        U5      nOUnU" X5      $ )z^Read pattern from string and return an instance of the appropriate implementation class.

    r   :Nr   )rj   isalnumr   )rq   r
   ra   styleclss        r   rz   rz   \  s\     7|aGAJ#-'"1+2E2E2G2G#BQK&w$$r   c                 J    [        XSS9n[        U[        R                  5      $ )z]Read pattern from string and return an instance of the appropriate implementation class.
    F)ra   )rz   r   r   re   )pattern_strr
   epattern_objs      r   r   r   h  s#     !EJLL)"<"<==r   c                    [         R                  [         R                  [         R                  [         R                  [         R                  [         R
                  [         R
                  S.nU (       d  [        R                  " S5      eUR                  U S   5      nUc(  [        R                  " SSR                  U5      -  5      eU SS R                  5       nU(       d  [        R                  " S5      eU[         R                  L a  UnO7U[         R
                  L a   [        U5      nO[        U5      n[        XAU5      n[        XS5      $ ! [         a    [        R                  " S	U 35      ef = f)
zHRead a --patterns-from command from string and return a CmdTuple object.)-!+RrPr}   z$A pattern/command must not be empty.r   Nz,A pattern/command must start with any of: %sz, r   z)A pattern/command must have a value part.zInvalid pattern style: )r   rd   re   rf   r   r   argparseArgumentTypeErrorr   joinr   r   r   r   rz   r   )cmd_line_strr
   cmd_prefix_mapr   remainder_strr   ra   s          r   r   r   o  sP    ''####N (()OPP


\!_
-C
{(()W)->)B*C D 	D !$++-M(()TUU
i   		&&	&	X#M2C
 +3/M[AC  	X,,/F}o-VWW	Xs   #E $E5r   )*r  r   os.pathr   r   r   r   collectionsr   enumr    r   helpersr   helpers.errorsr   r   r   r"   Actionr$   rE   rW   r\   r   r   rp   r{   r   r   r   _PATTERN_CLASSESr   r   r   r   r   r   rz   r   r   )is   0r   <module>r     sF      	 
  "     !
N;
SHOO S; ;&/ 9 /
c cLT$" $"N$k $ = ="B[ B,B; B*5; 5$   2BB1AAHHaK1AB j), 3
G %3 	% 1? > 3? '] Cs   D