a oe$@sddlZddlZddlZddlZddlZddlZddlZddlZejddZ ejdde fddZ ddZ ejej fdd Z ejdd e fd d Zejd dZGdddZGdddejejZGdddejZdS)Nc cs8t}t|z|VWt|n t|0dS)z >>> tmp_path = getfixture('tmp_path') >>> with pushd(tmp_path): ... assert os.getcwd() == os.fspath(tmp_path) >>> assert os.getcwd() != os.fspath(tmp_path) N)osgetcwdchdir)dirZorigrBC:\Program Files\Certbot\pkgs\setuptools\_vendor\jaraco\context.pypushd s  rc cs|dur$tj|dddd}|dur>tjtjdd}n t dt |dj fit z|d }d }d ||f}||j fd t|it |||VWdn1s0YW|d j fit n|d j fit 0dS)z Get a tarball, extract it, change to that directory, yield, then clean up. `runner` is the function to invoke commands. `pushd` is a context manager for changing the directory. Nz.tar.gzz.tgzT)shellzrunner parameter is deprecatedzmkdir {target_dir}zwget {url} -O -z7tar x{compression} --strip-components=1 -C {target_dir}z | compressionzrm -Rf {target_dir})rpathbasenamereplace functoolspartial subprocess check_callwarningswarnDeprecationWarningformatvarsjoininfer_compression)urlZ target_dirZrunnerrgetterextractcmdrrrtarball_contexts  &rcCs&|dd}tdddd}||dS)a Given a URL or filename, infer the compression code for tar. >>> infer_compression('http://foo/bar.tar.gz') 'z' >>> infer_compression('http://foo/bar.tgz') 'z' >>> infer_compression('file.bz') 'j' >>> infer_compression('file.xz') 'J' NzjJ)gzZbzxz)dictget)rZcompression_indicatormappingrrrr:s rccs*t}z|VW||n ||0dS)aN Create a temporary directory context. Pass a custom remover to override the removal behavior. >>> import pathlib >>> with temp_dir() as the_dir: ... assert os.path.isdir(the_dir) ... _ = pathlib.Path(the_dir).joinpath('somefile').write_text('contents') >>> assert not os.path.exists(the_dir) N)tempfileZmkdtemp)Zremovertemp_dirrrrr)Ns r)Tc csd|vr dnd}|\}|d||g}|r6|d|gttjjd}|rL|nd}tj||d|VWdn1sx0YdS)z Check out the repo indicated by url. If dest_ctx is supplied, it should be a context manager to yield the target directory for the check out. gitZhgZclonez--branchwN)stdout)extendopenrr devnullrr) rbranchquietZdest_ctxZexeZrepo_dirrr/r,rrr repo_contextas  r2ccs dVdS)z A null context suitable to stand in for a meaningful context. >>> with null() as value: ... assert value is None Nrrrrrnulltsr3c@steZdZdZdZeffddZddZeddZ ed d Z ed d Z d dZ ddZ edddZddZdS) ExceptionTrapa A context manager that will catch certain exceptions and provide an indication they occurred. >>> with ExceptionTrap() as trap: ... raise Exception() >>> bool(trap) True >>> with ExceptionTrap() as trap: ... pass >>> bool(trap) False >>> with ExceptionTrap(ValueError) as trap: ... raise ValueError("1 + 1 is not 3") >>> bool(trap) True >>> trap.value ValueError('1 + 1 is not 3') >>> trap.tb >>> with ExceptionTrap(ValueError) as trap: ... raise Exception() Traceback (most recent call last): ... Exception >>> bool(trap) False )NNNcCs ||_dSN) exceptions)selfr6rrr__init__szExceptionTrap.__init__cCs|Sr5rr7rrr __enter__szExceptionTrap.__enter__cCs |jdSNrexc_infor9rrrtypeszExceptionTrap.typecCs |jdS)Nr<r9rrrvalueszExceptionTrap.valuecCs |jdS)Nr<r9rrrtbszExceptionTrap.tbcGs&|d}|ot||j}|r"||_|Sr;) issubclassr6r=)r7r=r>matchesrrr__exit__s zExceptionTrap.__exit__cCs t|jSr5)boolr>r9rrr__bool__szExceptionTrap.__bool___testcstfdd}|S)a Wrap func and replace the result with the truth value of the trap (True if an exception occurred). First, give the decorator an alias to support Python 3.8 Syntax. >>> raises = ExceptionTrap(ValueError).raises Now decorate a function that always fails. >>> @raises ... def fail(): ... raise ValueError('failed') >>> fail() True cs@tj}|i|Wdn1s.0Y|Sr5)r4r6)argskwargsZtraprIfuncr7rrwrappers ,z%ExceptionTrap.raises..wrapper)rwraps)r7rMrIrNrrLrraisesszExceptionTrap.raisescCs|j|tjdS)a Wrap func and replace the result with the truth value of the trap (True if no exception). First, give the decorator an alias to support Python 3.8 Syntax. >>> passes = ExceptionTrap(ValueError).passes Now decorate a function that always fails. >>> @passes ... def fail(): ... raise ValueError('failed') >>> fail() False rH)rPoperatornot_)r7rMrrrpassesszExceptionTrap.passesN)__name__ __module__ __qualname____doc__r= Exceptionr8r:propertyr>r@rBrErGrFrPrSrrrrr4s!   r4c@seZdZdZdS)suppressz A version of contextlib.suppress with decorator support. >>> @suppress(KeyError) ... def key_error(): ... {}[''] >>> key_error() N)rTrUrVrWrrrrrZsrZc@s*eZdZdZd ddZddZdd Zd S) on_interrupta Replace a KeyboardInterrupt with SystemExit(1) >>> def do_interrupt(): ... raise KeyboardInterrupt() >>> on_interrupt('error')(do_interrupt)() Traceback (most recent call last): ... SystemExit: 1 >>> on_interrupt('error', code=255)(do_interrupt)() Traceback (most recent call last): ... SystemExit: 255 >>> on_interrupt('suppress')(do_interrupt)() >>> with __import__('pytest').raises(KeyboardInterrupt): ... on_interrupt('ignore')(do_interrupt)() errorr?cCs||_||_dSr5)actioncode)r7r]r^rrrr8szon_interrupt.__init__cCs|Sr5rr9rrrr:szon_interrupt.__enter__cCs6|tus|jdkrdS|jdkr,t|j||jdkS)Nignorer\rZ)KeyboardInterruptr] SystemExitr^)r7exctypeexcinstexctbrrrrEs   zon_interrupt.__exit__N)r\r?)rTrUrVrWr8r:rErrrrr[s  r[)rr contextlibrr(shutilrQrcontextmanagerrrrrmtreer)r2r3r4rZContextDecoratorr[rrrrs*  q