§
    cÌcp/  ã                   ó–  — d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	m
Z
 ddlmZmZ  G d„ d	ej        ¦  «        Z G d
„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ d¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ de¦  «        Z G d„ d¦  «        Zd„ Ze                     ¦   «          dS )z+Branch opening with URL-based restrictions.é    Né   )ÚerrorsÚtraceÚurlutils)ÚBranch)Ú
ControlDirÚControlDirFormat)Údo_catching_redirectionsÚget_transportc                   ó   — e Zd ZdZdS )ÚBadUrlz.Tried to access a branch from bad URL %(url)s.N©Ú__name__Ú
__module__Ú__qualname__Ú_fmt© ó    ú8/usr/lib/python3/dist-packages/breezy/url_policy_open.pyr   r   %   s   € € € € € à;€D€D€Dr   r   c                   ó   — e Zd ZdZdS )ÚBranchReferenceForbiddenzRTrying to mirror a branch reference and the branch type does not allow references.Nr   r   r   r   r   r   *   s   € € € € € ð)€D€D€Dr   r   c                   ó   — e Zd ZdZdZdS )ÚBranchLoopErrorzýEncountered a branch cycle.

    A URL may point to a branch reference or it may point to a stacked branch.
    In either case, it's possible for there to be a cycle in these references,
    and this exception is raised when we detect such a cycle.
    zEncountered a branch cycleN)r   r   r   Ú__doc__r   r   r   r   r   r   0   s   € € € € € ðð ð *€D€D€Dr   r   c                   ó$   — e Zd ZdZd„ Zd„ Zd„ ZdS )ÚBranchOpenPolicyzÄPolicy on how to open branches.

    In particular, a policy determines which branches are okay to open by
    checking their URLs and deciding whether or not to follow branch
    references.
    c                 ó*   — t          | j        ¦  «        ‚)a  Whether we traverse references when mirroring.

        Subclasses must override this method.

        If we encounter a branch reference and this returns false, an error is
        raised.

        :returns: A boolean to indicate whether to follow a branch reference.
        )ÚNotImplementedErrorÚshould_follow_references©Úselfs    r   r   z)BranchOpenPolicy.should_follow_referencesC   s   € õ " $Ô"?Ñ@Ô@Ð@r   c                 ó*   — t          | j        ¦  «        ‚)a´  Validate, maybe modify, 'url' to be used as a stacked-on location.

        :param branch:  The branch that is being opened.
        :param url: The URL that the branch provides for its stacked-on
            location.
        :return: (new_url, check) where 'new_url' is the URL of the branch to
            actually open and 'check' is true if 'new_url' needs to be
            validated by check_and_follow_branch_reference.
        )r   Útransform_fallback_location©r!   ÚbranchÚurls      r   r#   z,BranchOpenPolicy.transform_fallback_locationO   s   € õ " $Ô"BÑCÔCÐCr   c                 ó*   — t          | j        ¦  «        ‚)zúCheck a URL.

        Subclasses must override this method.

        :param url: The source URL to check.
        :raise BadUrl: subclasses are expected to raise this or a subclass
            when it finds a URL it deems to be unacceptable.
        )r   Úcheck_one_url©r!   r&   s     r   r(   zBranchOpenPolicy.check_one_url[   s   € õ " $Ô"4Ñ5Ô5Ð5r   N)r   r   r   r   r   r#   r(   r   r   r   r   r   ;   sQ   € € € € € ðð ð
Að 
Að 
Að
Dð 
Dð 
Dð	6ð 	6ð 	6ð 	6ð 	6r   r   c                   ó,   — e Zd ZdZdd„Zd„ Zd„ Zd„ ZdS )Ú_BlacklistPolicyzªBranch policy that forbids certain URLs.

    This doesn't cope with various alternative spellings of URLs,
    with e.g. url encoding. It's mostly useful for tests.
    Nc                 óB   — |€t          ¦   «         }|| _        || _        d S ©N)ÚsetÚ	_bad_urlsÚ_should_follow_references)r!   r   Úbad_urlss      r   Ú__init__z_BlacklistPolicy.__init__n   s&   € ØÐÝ‘u”uˆHØ!ˆŒØ)AˆÔ&Ð&Ð&r   c                 ó   — | j         S r-   ©r0   r    s    r   r   z)_BlacklistPolicy.should_follow_referencest   ó   € ØÔ-Ð-r   c                 ó6   — || j         v rt          |¦  «        ‚d S r-   )r/   r   r)   s     r   r(   z_BlacklistPolicy.check_one_urlw   s#   € Ø�$”.Ð Ð Ý˜‘+”+Ðð !Ð r   c                 ó:   — t          j        |j        |¦  «        dfS )zõSee `BranchOpenPolicy.transform_fallback_location`.

        This class is not used for testing our smarter stacking features so we
        just do the simplest thing: return the URL that would be used anyway
        and don't check it.
        F©r   ÚjoinÚbaser$   s      r   r#   z,_BlacklistPolicy.transform_fallback_location{   s   € õ Œ}˜Vœ[¨#Ñ.Ô.°Ð5Ð5r   r-   ©r   r   r   r   r2   r   r(   r#   r   r   r   r+   r+   g   sc   € € € € € ðð ðBð Bð Bð Bð.ð .ð .ðð ð ð6ð 6ð 6ð 6ð 6r   r+   c                   ó"   ‡ — e Zd ZdZˆ fd„Zˆ xZS )ÚAcceptAnythingPolicyz(Accept anything, to make testing easier.c                 ód   •— t          ¦   «                              dt          ¦   «         ¦  «         d S ©NT)Úsuperr2   r.   )r!   Ú	__class__s    €r   r2   zAcceptAnythingPolicy.__init__ˆ   s'   ø€ Ý‰Œ×Ò˜�s™uœuÑ%Ô%Ð%Ð%Ð%r   )r   r   r   r   r2   Ú__classcell__)rA   s   @r   r=   r=   …   s>   ø€ € € € € Ø2Ð2ð&ð &ð &ð &ð &ð &ð &ð &ð &r   r=   c                   ó0   — e Zd ZdZ	 	 dd„Zd„ Zd„ Zd„ ZdS )	ÚWhitelistPolicyz,Branch policy that only allows certain URLs.NFc                 ó>   — |€g }d„ |D ¦   «         | _         || _        d S )Nc                 ó8   — h | ]}|                      d ¦  «        ’ŒS )ú/)Úrstrip)Ú.0r&   s     r   ú	<setcomp>z+WhitelistPolicy.__init__.<locals>.<setcomp>“   s"   € ÐEÐEÐE°˜SŸZšZ¨™_œ_ÐEÐEÐEr   )Úallowed_urlsÚcheck)r!   r   rK   rL   s       r   r2   zWhitelistPolicy.__init__�   s.   € àÐØˆLØEÐE¸ÐEÑEÔEˆÔØˆŒ
ˆ
ˆ
r   c                 ó   — | j         S r-   r4   r    s    r   r   z(WhitelistPolicy.should_follow_references–   r5   r   c                 ó\   — |                      d¦  «        | j        vrt          |¦  «        ‚d S )NrG   )rH   rK   r   r)   s     r   r(   zWhitelistPolicy.check_one_url™   s.   € Ø�:Š:�c‰?Œ? $Ô"3Ð3Ð3Ý˜‘+”+Ðð 4Ð3r   c                 óD   — t          j        |j        |¦  «        | j        fS )z—See `BranchOpenPolicy.transform_fallback_location`.

        Here we return the URL that would be used anyway and optionally check
        it.
        )r   r9   r:   rL   r$   s      r   r#   z+WhitelistPolicy.transform_fallback_location�   s   € õ Œ}˜Vœ[¨#Ñ.Ô.°´
Ð:Ð:r   )NFr;   r   r   r   rD   rD   Œ   sa   € € € € € Ø6Ð6à>BØðð ð ð ð.ð .ð .ðð ð ð;ð ;ð ;ð ;ð ;r   rD   c                   ó*   — e Zd ZdZd„ Zd„ Zd„ Zd„ ZdS )ÚSingleSchemePolicyz=Branch open policy that rejects URLs not on the given scheme.c                 ó   — || _         d S r-   )Úallowed_scheme)r!   rS   s     r   r2   zSingleSchemePolicy.__init__©   s   € Ø,ˆÔÐÐr   c                 ó   — dS r?   r   r    s    r   r   z+SingleSchemePolicy.should_follow_references¬   s   € Øˆtr   c                 ó:   — t          j        |j        |¦  «        dfS r?   r8   r$   s      r   r#   z.SingleSchemePolicy.transform_fallback_location¯   s   € ÝŒ}˜Vœ[¨#Ñ.Ô.°Ð4Ð4r   c                 ó˜   — t           j                             t          |¦  «        ¦  «        j        | j        k    rt          |¦  «        ‚dS )z!Check that `url` is okay to open.N)r   ÚURLÚfrom_stringÚstrÚschemerS   r   r)   s     r   r(   z SingleSchemePolicy.check_one_url²   s>   € åŒ<×#Ò#¥C¨¡H¤HÑ-Ô-Ô4¸Ô8KÒKÐKÝ˜‘+”+Ðð LÐKr   N)r   r   r   r   r2   r   r#   r(   r   r   r   rQ   rQ   ¦   sV   € € € € € ØGÐGð-ð -ð -ðð ð ð5ð 5ð 5ðð ð ð ð r   rQ   c                   ó„   — e Zd ZdZ ej        ¦   «         Zdd„Zed„ ¦   «         Z	d„ Z
ed„ ¦   «         Zd„ Zd„ Zd	„ Zdd„ZdS )ÚBranchOpenera0  Branch opener which uses a URL policy.

    All locations that are opened (stacked-on branches, references) are
    checked against a policy object.

    The policy object is expected to have the following methods:
    * check_one_url
    * should_follow_references
    * transform_fallback_location
    Nc                 ór   — || _         t          ¦   «         | _        |€t          j        ¦   «         }|| _        dS )zÆCreate a new BranchOpener.

        :param policy: The opener policy to use.
        :param probers: Optional list of probers to allow.
            Defaults to local and remote bzr probers.
        N)Úpolicyr.   Ú
_seen_urlsr	   Úall_probersÚprobers)r!   r^   ra   s      r   r2   zBranchOpener.__init__Æ   s4   € ð ˆŒÝ™%œ%ˆŒØˆ?Ý&Ô2Ñ4Ô4ˆGØˆŒˆˆr   c                 óR   — t           j                             d| j        d¦  «         dS )aØ  Install the ``transform_fallback_location`` hook.

        This is done at module import time, but transform_fallback_locationHook
        doesn't do anything unless the `_active_openers` threading.Local
        object has a 'opener' attribute in this thread.

        This is in a module-level function rather than performed at module
        level so that it can be called in setUp for testing `BranchOpener`
        as breezy.tests.TestCase.setUp clears hooks.
        r#   z,BranchOpener.transform_fallback_locationHookN)r   ÚhooksÚinstall_named_hookÚtransform_fallback_locationHook)Úclss    r   Úinstall_hookzBranchOpener.install_hookÓ   s4   € õ 	Œ×'Ò'Ø)ØÔ/Ø:ñ	<ô 	<ð 	<ð 	<ð 	<r   c                 ó"  — 	 || j         v rt          ¦   «         ‚| j                              |¦  «         | j                             |¦  «         |                      |¦  «        }|€|S |}| j                             ¦   «         st          |¦  «        ‚Œ�)a  Check URL (and possibly the referenced URL).

        This method checks that `url` passes the policy's `check_one_url`
        method, and if `url` refers to a branch reference, it checks whether
        references are allowed and whether the reference's URL passes muster
        also -- recursively, until a real branch is found.

        :param url: URL to check
        :raise BranchLoopError: If the branch references form a loop.
        :raise BranchReferenceForbidden: If this opener forbids branch
            references.
        )r_   r   Úaddr^   r(   Úfollow_referencer   r   )r!   r&   Únext_urls      r   Ú!check_and_follow_branch_referencez.BranchOpener.check_and_follow_branch_referenceä   s˜   € ð
	4Ø�d”oÐ%Ð%Ý%Ñ'Ô'Ð'ØŒO×Ò Ñ$Ô$Ð$ØŒK×%Ò% cÑ*Ô*Ð*Ø×,Ò,¨SÑ1Ô1ˆHØÐØ�
ØˆCØ”;×7Ò7Ñ9Ô9ð 4Ý.¨sÑ3Ô3Ð3ð
	4r   c                 óÂ   — 	 t          | j        d¦  «        }n# t          $ r |cY S w xY w|j                             ||¦  «        \  }}|r|                     |¦  «        S |S )a  Installed as the 'transform_fallback_location' Branch hook.

        This method calls `transform_fallback_location` on the policy object
        and either returns the url it provides or passes it back to
        check_and_follow_branch_reference.
        Úopener)ÚgetattrÚ_threading_dataÚAttributeErrorr^   r#   rl   )rf   r%   r&   rn   Únew_urlrL   s         r   re   z,BranchOpener.transform_fallback_locationHooký   s{   € ð	Ý˜SÔ0°(Ñ;Ô;ˆFˆFøÝð 	ð 	ð 	ØˆJˆJˆJð	øøøàœ×BÒBÀ6È3ÑOÔO‰ˆ�Øð 	Ø×;Ò;¸GÑDÔDÐDàˆNs   ‚ ˜'¦'c                 óî   — | j         t          j        d         vrt          d¦  «        ‚| | j        _        	  ||i |¤Ž| j        `t          ¦   «         | _        S # | j        `t          ¦   «         | _        w xY w)Nr#   zhook not installed)re   r   rc   ÚAssertionErrorrp   rn   r.   r_   )r!   ÚcallableÚargsÚkws       r   Ú3run_with_transform_fallback_location_hook_installedz@BranchOpener.run_with_transform_fallback_location_hook_installed  s‹   € àÔ0Ý”Ð:Ô;ð<ð <å Ð!5Ñ6Ô6Ð6Ø&*ˆÔÔ#ð	$Ø�8˜TÐ( RÐ(Ð(àÔ$Ð+õ "™eœeˆDŒOˆOøð Ô$Ð+õ "™eœeˆDŒOÐ#Ð#Ð#Ð#s   ¶A ÁA4c                 óˆ   ‡ — ˆ fd„}ˆ fd„}t          |¦  «        }t          |||¦  «        \  }}|                     |¦  «        S )zˆSimple BzrDir.open clone that only uses specific probers.

        :param url: URL to open
        :return: ControlDir instance
        c                 ó  •— ‰j                              |j        ¦  «         |                      |j        |j        ¦  «        }|€t          j        |j        ¦  «        ‚t          j        d| j	        |j
        |j	        ¦  «         |S )Nz%s is%s redirected to %s)r^   r(   ÚtargetÚ_redirected_toÚsourcer   ÚNotBranchErrorr   Únoter:   Úpermanently)Ú	transportÚeÚredirection_noticeÚredirected_transportr!   s       €r   Ú
redirectedz*BranchOpener._open_dir.<locals>.redirected#  s„   ø€ ØŒK×%Ò% a¤hÑ/Ô/Ð/Ø#,×#;Ò#;Ø”˜!œ(ñ$$ô $$Ð à#Ð+ÝÔ+¨A¬HÑ5Ô5Ð5ÝŒJØ*Ø” ¤Ð/CÔ/HñJô Jð Jð (Ð'r   c                 óÐ   •— t          j        | j        ¦  «        }‰j        D ]C} |¦   «         }	 | |                     | ¦  «        fc S # t           j        $ r}|}Y d }~Œ<d }~ww xY w|‚r-   )r   r~   r:   ra   Úprobe_transport)r�   Ú
last_errorÚ
prober_klsÚproberr‚   r!   s        €r   Úfind_formatz+BranchOpener._open_dir.<locals>.find_format.  sŽ   ø€ ÝÔ.¨y¬~Ñ>Ô>ˆJØ"œlð !ð !�
Ø#˜™œ�ð#Ø$ f×&<Ò&<¸YÑ&GÔ&GÐGÐGÐGÐGøÝÔ,ð #ð #ð #Ø!"�J�J�J�J�J�Jøøøøð#øøøð !Ð s   ¯AÁA#ÁAÁA#)r   r
   Úopen)r!   r&   r…   r‹   r�   Úformats   `     r   Ú	_open_dirzBranchOpener._open_dir  sq   ø€ ð		(ð 		(ð 		(ð 		(ð 		(ð		!ð 		!ð 		!ð 		!ð 		!õ " #Ñ&Ô&ˆ	Ý4Ø˜ Jñ0ô 0Ñˆ	�6à�{Š{˜9Ñ%Ô%Ð%r   c                 óT   — |                       |¦  «        }|                     ¦   «         S )z‹Get the branch-reference value at the specified url.

        This exists as a separate method only to be overriden in unit tests.
        )rŽ   Úget_branch_reference)r!   r&   Ú
controldirs      r   rj   zBranchOpener.follow_reference=  s'   € ð
 —^’^ CÑ(Ô(ˆ
Ø×.Ò.Ñ0Ô0Ð0r   Fc                 óž   ‡ — t          |t          ¦  «        st          ‚‰                      |¦  «        }ˆ fd„}‰                      |||¦  «        S )z¯Open the Bazaar branch at url, first checking it.

        What is acceptable means is defined by the policy's `follow_reference`
        and `check_one_url` methods.
        c                 óZ   •— ‰                      | ¦  «        }|                     |¬¦  «        S )N)Úignore_fallbacks)rŽ   Úopen_branch)r&   r”   Údirr!   s      €r   r•   z&BranchOpener.open.<locals>.open_branchP  s)   ø€ Ø—.’. Ñ%Ô%ˆCØ—?’?Ð4D�?ÑEÔEÐEr   )Ú
isinstancerY   Ú	TypeErrorrl   rx   )r!   r&   r”   r•   s   `   r   rŒ   zBranchOpener.openE  so   ø€ õ ˜#�sÑ#Ô#ð 	ÝˆOà×4Ò4°SÑ9Ô9ˆð	Fð 	Fð 	Fð 	Fð 	Fð ×GÒGØ˜Ð.ñ0ô 0ð 	0r   r-   )F)r   r   r   r   Ú	threadingÚlocalrp   r2   Úclassmethodrg   rl   re   rx   rŽ   rj   rŒ   r   r   r   r\   r\   ¸   sÊ   € € € € € ð	ð 	ð &�i”oÑ'Ô'€Oðð ð ð ð ð<ð <ñ „[ð<ð 4ð 4ð 4ð2 ðð ñ „[ðð"$ð $ð $ð&ð &ð &ð@1ð 1ð 1ð0ð 0ð 0ð 0ð 0ð 0r   r\   c                 ó`   — t          t          | ¦  «        ¦  «                             |¦  «        S )z¨Open the branch at `url`, only accessing URLs on `allowed_scheme`.

    :raises BadUrl: An attempt was made to open a URL that was not on
        `allowed_scheme`.
    )r\   rQ   rŒ   )rS   r&   s     r   Úopen_only_schemer�   W  s)   € õ Õ*¨>Ñ:Ô:Ñ;Ô;×@Ò@ÀÑEÔEÐEr   )r   r™   Ú r   r   r   r%   r   r‘   r   r	   r�   r
   r   ÚBzrErrorr   r   r   r   r+   r=   rD   rQ   r\   r�   rg   r   r   r   ú<module>r       se  ðð" 2Ð 1à Ð Ð Ð ðð ð ð ð ð ð ð ð ð ð
 Ð Ð Ð Ð Ð ðð ð ð ð ð ð ð ðð ð ð ð ð ð ð ð<ð <ð <ð <ð <ˆVŒ_ñ <ô <ð <ð
*ð *ð *ð *ð *˜vœñ *ô *ð *ð*ð *ð *ð *ð *�f”oñ *ô *ð *ð)6ð )6ð )6ð )6ð )6ñ )6ô )6ð )6ðX6ð 6ð 6ð 6ð 6Ð'ñ 6ô 6ð 6ð<&ð &ð &ð &ð &Ð+ñ &ô &ð &ð;ð ;ð ;ð ;ð ;Ð&ñ ;ô ;ð ;ð4ð ð ð ð Ð)ñ ô ð ð$\0ð \0ð \0ð \0ð \0ñ \0ô \0ð \0ð~Fð Fð Fð × Ò Ñ Ô Ð Ð Ð r   