
    {bjI                      `   S r SSKrSSKrSSKJr  SSKJrJrJr  SSK	r	SSK
JrJrJrJrJrJrJrJrJrJrJrJr  SSKJr  SSKJrJr  SS	KJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&  SS
K'J(r(J)r)  SSK*J+r+  SSK,J-r-J.r.J/r/J0r0  \&" SS9r1\&" SS9r2\1Rg                  \2SS9  \&" SS9r4\1Rg                  \4SS9  \\5\(" SS94   r6\S\5S\5S-  S\5S-  S\\   4S j5       r7\1Rq                  S/ SQS9             SsS\\5S-  \(" SS94   S\\5S-  \)" SS S94   S!\-S"\\5S-  \)" S#S94   S$\S%\ S&\.S'\S(\S)\"S\/S*\\9\)" S+S,S94   S\!SS4S- jj5       r:\1Rq                  S.S/S00S1S2/S39      StS\6S4\\;\5   \(" S5S94   S6\\5S-  \)" S7S8S9S94   S$\S'\S:\\<S-  \)" S;S<S94   S\/S\!SS4S= jj5       r=\1Rq                  S>S/S00S?S@/S39     SuS\6S4\\;\5   \(" SAS94   S6\\5S-  \)" S7S8S9S94   S$\S'\S\/S\!SS4SB jj5       r>\1Rq                  SCSDSE/S9  SvSF\\5\(" SGS94   SH\\5\(" SIS94   S\/S\!SS4
SJ jj5       r?\1Rq                  SK/ SLQS9     SwS\\5S-  \(" SMS94   SN\\9\)" SOSPS94   SQ\\9\)" SRSSSTS94   S\/S\!SS4SU jj5       r@\2Rq                  S/ SVQS9SS\SSSS4S\\5S-  \(" SWS94   S!\-SX\\A\)" SYSSZ\ S[3S\94   S]\\AS-  \)" S^SS_S\94   S"\\5S-  \)" S`S94   S\/S\!SS4Sa jj5       rB\2Rq                  SbSc/S9  SvS\/S\!SS4Sd jj5       rC\2Rq                  SeSf/S9   SxSg\\5\(" ShS94   SQ\\9\)" SRSSSTS94   S\/S\!SS4
Si jj5       rDSj\5\;\5   -  S\54Sk jrES\S\54Sl jrF\4Rq                  SbSm/S9  SvS\6S\/S\!SS4Sn jj5       rG\4Rq                  SKSo/S9  SvS\6Sp\\A\(" SqS94   S\/S\!SS4
Sr jj5       rHg)yzCContains commands to run and manage sandboxes on Hugging Face Jobs.    N)contextmanager)	AnnotatedAnyIterator)DEFAULT_IDLE_TIMEOUTDEFAULT_IMAGEDEFAULT_SANDBOXES_PER_HOST
MODE_LABEL	MODE_POOL
POOL_LABELSANDBOX_LABELSHARED_ID_SEPSandboxSandboxPoolSandboxProcess_split_sandbox_id)delete_pool_cache)CLIErrorSandboxError   )

EnvFileOptEnvOptSecretsFileOpt
SecretsOptTokenOpt
VolumesOpt
get_hf_apiparse_env_mapparse_volumestyper_factory)ArgumentOption)out)	FlavorOpt	LabelsOptNamespaceOpt_parse_labels_mapz;Run and manage experimental sandboxes on Hugging Face Jobs.)helpzhWarm host VM pools and spawn experimental shared sandboxes for workloads within the same trust boundary.pool)namez8List and stop background processes running in a sandbox.processz1The sandbox id as printed by `hf sandbox create`.
sandbox_id	namespacetokenreturnc             #      #    [         R                  " XUS9n Uv   UR                  5         g! UR                  5         f = f7f)zIReattach to a sandbox and close the HTTP client when the command is done.r-   r.   N)r   connectclose)r,   r-   r.   sandboxs       ڗ/tmp/claude-0/-home-danvics-docker-quiz/c1e0577a-e42c-4a3d-b1ea-3edd61103a4e/scratchpad/stt/lib/python3.13/site-packages/huggingface_hub/cli/sandbox.py_connectr6   D   s3      oojUKGs   A. AA  Acreate)zhf sandbox createzhf sandbox create ubuntu:24.04z%hf sandbox create --flavor a10g-smallz/hf sandbox create --label controller-run=run-42z@hf sandbox create --pool pool-ab12cd34ef56 --env LOG_LEVEL=debug)examplesimagezDocker image (needs /bin/sh).z--poolzJSpawn a cheap shared sandbox in this pool (from `hf sandbox pool create`).flavoridle_timeoutzTAuto-terminate the sandbox after this much inactivity (e.g. '10m'). Defaults to 10m.envsecretslabelenv_filesecrets_filevolumeforward_hf_tokenz--forward-hf-tokenz0Inject your HF token as HF_TOKEN in the sandbox.c                 0   [         R                   " 5       nUb  UO[        nUb  U c
  Uc  U	(       a  [        S5      eU(       d  U(       a  [        S5      eU(       a  [        S5      e[        R                  " XUS9R                  [        XG5      UUS9n[        R                  " SUR                  UR                  U[         R                   " 5       U-
  S S	3S
9  [        R                  " SUR                   S35        [        R                  " SUR                   S35        g[        R
                  " U =(       d    [        U=(       d    SU[        XG5      [        XX5      [        U	5      U
U[        U5      US9
nUR!                  5         [        R                  " SUR                  UR"                  [         R                   " 5       U-
  S S	3S9  [        R                  " SUR                   S35        [        R                  " SUR                   S35        g)a~  Create a sandbox: a dedicated VM by default, or a cheap shared one with `--pool`.

Env and idle-timeout apply to the sandbox in both modes. With `--pool`, the image and
flavor come from the pool, so passing them here is an error; `--secrets` is also
rejected since pooled sandboxes have no encrypted-secrets channel (use `--env`). Define
a pool first with `hf sandbox pool create`.
NzQ--pool fixes the image/flavor (and volumes aren't supported); drop those options.zF--pool can't encrypt secrets; pass them with --env/--env-file instead.z2--label is only supported for dedicated sandboxes.r1   )r<   r;   rB   zSandbox ready.1fs)idhostr)   elapsedz$Run a command with `hf sandbox exec z -- echo hello`.z#Terminate it with `hf sandbox kill `.	cpu-basic)
r9   r:   r;   r<   r=   volumesr-   rB   labelsr.   )rF   r9   rH   )timer   r   r   r2   r7   r   r#   resultrF   host_idhintr   r   r   r'   r3   r9   )r9   r)   r:   r;   r<   r=   r>   r?   r@   rA   r-   rB   r.   startidlesbxr4   s                    r5   sandbox_createrT   N   s   P IIKE'3<9MD 2fnoolcddOPP!!$5IPPc,- Q 

 	

?svvCKKdW[W`W`WbejWjknVoopTqr7x?OPQ6svvhbABnn$}$#(g4f%) 'G MMOJJ7::W]]tyy{]bObcfNgghLijHH3GJJ<?OPQHH27::,bAB    execignore_unknown_optionsTz5hf sandbox exec <sandbox_id> -- python -c "print(42)"z1hf sandbox exec -w /app <sandbox_id> -- pytest -x)context_settingsr8   commandzThe command to run.workdirz-wz	--workdirzWorking directory.exec_timeoutz	--timeoutz)Kill the command after this many seconds.c                    S[         SS4S jnS[         SS4S jn	[        XUS9 n
U
R                  [        U5      [	        X45      UUUU	SS9nSSS5        WR
                  (       aJ  [        R                  " S	U S
35        [        R                  R                  UR                  =(       d    SS9eUR                  S:w  a8  [        R                  R                  UR                  b  UR                  S9eSS9eg! , (       d  f       N= f)zRun a command in a sandbox, streaming output. Exits with the command's exit code.

To start a long-running command in the background instead of waiting for it, use
`hf sandbox spawn`.
datar/   Nc                 ~    [         R                  R                  U 5        [         R                  R                  5         g N)sysstdoutwriteflushr]   s    r5   write_stdout"sandbox_exec.<locals>.write_stdout   $    



rU   c                 ~    [         R                  R                  U 5        [         R                  R                  5         g r_   )r`   stderrrb   rc   rd   s    r5   write_stderr"sandbox_exec.<locals>.write_stderr   rg   rU   r1   F)r<   cwdtimeout	on_stdout	on_stderrcheckzCommand timed out after zs.|   )coder   r   )strr6   runlistr   	timed_outr#   errorclick
exceptionsExit	exit_code)r,   rY   rZ   r<   r?   r[   r-   r.   re   rj   r4   rN   s               r5   sandbox_execr|      s   43 4 3 4  
*	?7Mc, ""  
 
@ 		,\N"=>##)9)9)@S#AA1##V=M=M=Y)9)9#aa_`#aa  
@	?s   (C99
Dspawnz;hf sandbox spawn <sandbox_id> -- python -m http.server 8000z8hf sandbox spawn -w /app <sandbox_id> -- uvicorn app:appz%The command to run in the background.c                 V   [        XUS9 nUR                  [        U5      [        X45      USS9nSSS5        [        R
                  " SU WR                  S9  [        R                  " SU  S35        [        R                  " S	U  S
UR                   S35        g! , (       d  f       Np= f)zStart a long-running command in the background and return its pid (don't wait).

List a sandbox's processes with `hf sandbox process ls` and stop one with
`hf sandbox process kill`.
r1   T)r<   rl   
backgroundNzProcess startedr4   pidz+List processes with `hf sandbox process ls rI   z&Stop it with `hf sandbox process kill  )r6   rt   ru   r   r#   rN   r   rP   )	r,   rY   rZ   r<   r?   r-   r.   r4   r+   s	            r5   sandbox_spawnr      s    , 
*	?7++d7ms1MSZgk+l 
@JJ *'++FHH::,bIJHH5j\7;;-rRS	 
@	?s   %B
B(cpz2hf sandbox cp data.csv <sandbox_id>:/data/data.csvz7hf sandbox cp <sandbox_id>:/app/result.json result.jsonsrcz,Source: a local path or <sandbox_id>:<path>.dstz1Destination: a local path or <sandbox_id>:<path>.c                    S[         S[        [         S-  [         4   4S jnU" U 5      u  pVU" U5      u  pxUSL USL :X  a  [        S5      eUb/  [        XRUS9 n	U	R                  R                  Xh5        SSS5        O3Uc   e[        XrUS9 n	U	R                  R                  Xh5        SSS5        [        R                  " SXS9  g! , (       d  f       N$= f! , (       d  f       N5= f)	zCCopy a file between the local machine and a sandbox (docker-style).refr/   Nc                     SU ;   a<  U R                  S5      (       d&  U R                  SS5      u  p[        U5      S:  a  X4$ S U 4$ )N:)./~r   )
startswithsplitlen)r   r,   paths      r5   parsesandbox_cp.<locals>.parse   sK     #:cnn_=="yya0J:"!''SyrU   zHExactly one of SRC and DST must be a sandbox path (<sandbox_id>:<path>).r1   Copied)r   r   )	rs   tupler   r6   filesdownloaduploadr#   rN   )
r   r   r-   r.   r   src_sandboxsrc_pathdst_sandboxdst_pathr4   s
             r5   
sandbox_cpr      s    3 5tS1  "#JK!#JKt!45abbkeDMM""86 ED &&&keDMM  4 EJJxS* ED EDs   CC
C
C%kill)zhf sandbox kill <sandbox_id>zKhf sandbox kill <host_id>   # kills a whole shared host (all its sandboxes)zhf sandbox kill --allz$The sandbox or host id to terminate.all_z--allz2Terminate every sandbox and host in the namespace.yesz-yz--yesz$Answer Yes to prompts automatically.c                    [        US9nU(       a  [        UR                  SS/[        S0US95      nU(       d  [        R
                  " S5        g[        R                  " S[        U5       S	3US
9  U H1  nUR                  UR                  UR                  R                  S9  M3     [        R                  " S[        U5      S9  gU c  [        S5      e[        X5      u  p[        U;   a:   [!        XUS9 n
U
R#                  5         SSS5        [        R                  " SU S9  gUR)                  XS9nUR*                  =(       d    0 R-                  [.        5      [0        :X  a]  [        R                  " SU S3US
9  UR                  UR                  UR                  R                  S9  [        R                  " SUS9  g [!        XUS9 n
U
R#                  5         SSS5        [        R                  " SU S9  g! , (       d  f       N= f! [$         a  n[        ['        U5      5      UeSnAff = f! , (       d  f       N\= f! [$         a  n[        ['        U5      5      UeSnAff = f)z@Terminate a sandbox, a whole shared host, or everything (--all).r.   RUNNING
SCHEDULING1statusrL   r-   zNo running sandboxes.N
Terminate zA sandbox job(s) (including shared hosts and all their sandboxes)?r   job_idr-   
Terminated)jobsz*Provide a sandbox id, a host id, or --all.r1   zSandbox terminated)rF   zTerminate shared host z and all of its sandboxes?zHost terminated)r   ru   	list_jobsr   r#   textconfirmr   
cancel_jobrF   ownerr*   rN   r   r   r   r6   r   r   rs   inspect_jobrL   getr
   r   )r,   r   r   r-   r.   apir   jobsidnsr4   es               r5   sandbox_killr     s     5
!CCMM)\)BM[^K_ktMuvHH,-jT+lmsvwCNN#&&CIINNNC 

<c$i0CDD
6GC	**G7 H 	

'J7 ///
3C

bj)Y6,SE1KLRUVcff		?

$-&jUCwLLN D JJ#
3' HG 	*3q6")	* DC &s1vA%&sl   /
H 9G>
H 
I H6 I >
HH H 
H3H..H36
I I I 
I+I&&I+)zhf sandbox pool createz5hf sandbox pool create python:3.12 --flavor cpu-basicz7hf sandbox pool create --per-host 50 --idle-timeout 30mz+Docker image for the hosts (needs /bin/sh).per_hostz
--per-hostz&Sandboxes packed per host VM (default z).)minr(   	max_hostsz--max-hostsz'Optional cap on the number of host VMs.zZShut a host down once it has had no sandboxes for this long (e.g. '10m'). Defaults to 10m.c           
         [         R                   " 5       nU =(       d    [        n [        U U=(       d    SUUUb  UO[        UUS9nUR                  n	UR
                  n
[        R                  " SU	U U=(       d    SU
S   [         R                   " 5       U-
  S S3S9  [        R                  " S	U	 S
35        [        R                  " SU	 S
35        g)zRWarm a pool: boot one host VM now, tagged so it can be found later by its pool id.rJ   N)r9   r:   sandboxes_per_hostr   r;   r-   r.   zPool createdr   rD   rE   )rF   r9   r:   rG   rH   z/Spawn a sandbox with `hf sandbox create --pool rI   z=Delete the pool (and its hosts) with `hf sandbox pool delete )	rM   r   r   r   r*   host_idsr#   rN   rP   )r9   r:   r   r   r;   r-   r.   rQ   r)   pool_idr   s              r5   pool_creater   N  s    4 IIKE"]E$#%1%=\CWD iiG}}HJJ$a[99;&s+1- HH>wirJKHHLWIUWXYrU   z	ls | listzhf sandbox pool lsc                    [        US9n0 nUR                  S[        [        0U S9 H  nUR                  =(       d    0 R                  [        5      nU(       d  M4  [        UR                  [        5      (       a  UR                  O0 nUR                  UUUR                  =(       d    UR                  UR                  UR                  SS5      SS.5      nUS==   S	-  ss'   M     [        UR                  5       5      n[         R"                  " US
S9  U(       d  [         R$                  " S5        g[         R$                  " S5        g)z9List running sandbox pools (grouped from their host VMs).r   r   r   SBX_CAPACITY r   )rF   r9   r:   r   hostsr   r   rF   id_keyz)Create one with `hf sandbox pool create`.z5Spawn a sandbox with `hf sandbox create --pool <id>`.N)r   r   r
   r   rL   r   r   
isinstanceenvironmentdict
setdefaultdocker_imagespace_idr:   ru   valuesr#   tablerP   )	r-   r.   r   poolsr   r   r<   inforowss	            r5   pool_lsr     s     5
!C')E}}Iz96MYb}czzR$$Z0!+COOT!B!Bcoo))9S\\**GGNB7	
 	W d  DIId4 <=HIrU   zdelete | rmz hf sandbox pool delete <pool_id>r   zPool id to delete.c           	         [        US9n[        UR                  SS/[        [        [
        U 0US95      nU(       d&  [        U 5        [        R                  " SU  S35        g[        R                  " S[        U5       S	U  S
3US9  U H1  nUR                  UR                  UR                  R                  S9  M3     [        U 5        [        R                  " SU [        U5      S9  g)zDTerminate every host VM of a pool (and therefore all its sandboxes).r   r   r   r   zNo running hosts for pool 'z'.Nr   z host(s) of pool 'z' (and all their sandboxes)?r   r   zPool deleted)rF   hosts_terminated)r   ru   r   r
   r   r   r   r#   r   r   r   r   rF   r   r*   rN   )r   r   r-   r.   r   r   r   s          r5   pool_deleter     s     5
!C|,	:w? 	 	
E '".wir:;KK*SZL(:7)C_`fijcff		? gJJ~'CJGrU   cmdc                 R    [        U [        5      (       a  U $ SR                  U 5      $ )Nr   )r   rs   join)r   s    r5   _fmt_cmdr     s!    S#&&39CHHSM9rU   c                 d    U R                   (       a  gU R                  c  S$ SU R                   S3$ )Nrunningexitedzexited ())r   r{   )r+   s    r5   _fmt_statusr     s3    ((08UARAR@SST6UUrU   z"hf sandbox process ls <sandbox_id>c                    [        XUS9 nUR                  5       nSSS5        W Vs/ s H.  oUR                  [        U5      [	        UR
                  5      S.PM0     nn[        R                  " USS9  U(       d  [        R                  " SU  S35        g[        R                  " SU  S	35        g! , (       d  f       N= fs  snf )
zUList the background processes running in a sandbox (started with `hf sandbox spawn`).r1   N)r   r   r   r   r   z!Start one with `hf sandbox spawn z -- <cmd>`.z'Stop one with `hf sandbox process kill z <pid>`.)	r6   	processesr   r   r   r   r#   r   rP   )r,   r-   r.   r4   r   pr   s          r5   
process_lsr     s     
*	?7%%'	 
@V_`V_QREE[^HQUUOLV_D`IId5!4ZLLM::,hOP 
@	?`s   B15C1
B?z*hf sandbox process kill <sandbox_id> <pid>r   z.The pid as printed by `hf sandbox process ls`.c                   ^ [        XUS9 n[        U4S jUR                  5        5       S5      nUc  [        ST SU  S35      eUR	                  5         SSS5        [
        R                  " SU TS9  g! , (       d  f       N%= f)	z/Stop a background process running in a sandbox.r1   c              3   J   >#    U  H  oR                   T:X  d  M  Uv   M     g 7fr_   )r   ).0r   r   s     r5   	<genexpr>process_kill.<locals>.<genexpr>  s     G#6a%%3,#6s   #	#NzNo process with pid z in sandbox r   zProcess stoppedr   )r6   nextr   r   r   r#   rN   )r,   r   r-   r.   r4   r+   s    `    r5   process_killr     sx     
*	?7G7#4#4#6GN?1#l:,aPQQ	 
@
 JJ *#> 
@	?s   A
A55
B)NNNNNNNNNNNFN)NNNNNN)NNNNN)NN)NFFNN)FNN)I__doc__r`   rM   
contextlibr   typingr   r   r   rx   huggingface_hub._sandboxr   r   r	   r
   r   r   r   r   r   r   r   r   huggingface_hub._sandbox_cacher   huggingface_hub.errorsr   r   
_cli_utilsr   r   r   r   r   r   r   r   r   r    
_frameworkr!   r"   _outputr#   r   r$   r%   r&   r'   sandbox_clipool_cli	add_groupprocess_clirs   SandboxIdArgr6   rY   boolrT   ru   floatr|   r   r   r   intr   r   r   r   r   r   r    rU   r5   <module>r      sP   J 
  % + +     = 9   )  G G !^_	s   hV  ,![\   k	  2h,_``a  C$J sTz hW^N_     	 TX 	 	#'" 	+BCS4Z/N!OOPBC
d
xjk	mBC BC d
jk	mBC 
BC BC BC BC !BC  !BC" #BC$  f)0bcc%BC* +BC, 
-BC	BCJ 
.5?;   \` 	"(b(btCy(0E"FFG(b sTz6$BV#WWX(b 
	(b
 (b f[/Z[[(b (b (b 
(b(bV .5EB   \`"TTtCy(0W"XXYT sTz6$BV#WWXT 
	T
 T T T 
TT* <A   #	+	3&TUU	V+	3&YZZ	[+ + 	+
 
++@ 
   `dhm_d"/4#*h4Z&[[\/4
D&/cdd
e/4 
4g4Z[[	\/4 	/4
 /4 
/4/4d 
   bf 	# 	 	"(ZS4Z/\!]]^(Z(Z |+QRlQmmo)pq	s(Z d
F=a6_``(Z d
pq	s(Z (Z (Z  
!(Z(ZV 
+)=(>?"JJJ 
J @J> 
01   `e"	HsH*>??@H	4g4Z[[	\H H 	H
 
H	H4:#S	/ :c :V VC V [,P+QR #QQQ Q 
	Q SQ  V'S&TU #	??	3&VWW	X? ? 	?
 
? V?rU   