
    i	L              
       ~   d dl Z d dlZd dlZd dlZd dlZd dlZd dl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mZmZmZmZmZ d dlZd dlZd dlmZ d dlmZmZmZmZ d dlmZ d d	l m!Z! d d
l"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z) d dl*m+Z+m,Z,m-Z- d dl.m/Z/ d dl0m1Z1m2Z2  ejf                  e4      Z5dede6fdZ7dede8fdZ9dedefdZ: G d de      Z;dee;   fdZ< G d de      Z=de6de6de=fdZ> G d de6e	      Z? G d  d!e      Z@d"e6de@fd#ZA G d$ d%e      ZBd&e6d'eddfd(ZCd)e6dee=   fd*ZDde6dee=   fd+ZEd&e6dee6ddf   fd,ZFg d-ZGg d.ZHd/e!d0ede+fd1ZId/e!d"e6d2e@ddfd3ZJ G d4 d5e      ZKd6eKd/e!ddfd7ZLd/e!ddfd8ZM	 	 d?de%eedf    ej                  d9:      f   d;e%e8 ejF                  d<d=:      f   defd>ZOy)@    N)Enum)cycle)Path)AnyDict	GeneratorListOptionalUnion)Client)	BaseModelEmailStrTypeAdapterValidationError)Text)RichToolkit)Option)	Annotated)login)	APIClient)	AppConfigget_app_configwrite_app_config)is_logged_in)get_rich_toolkithandle_http_errorspathreturnc                     | j                   S N)name)r   s    /var/www/html/hwPaymentPortal-be-dev/hw-payment-portal-api/venv/lib/python3.12/site-packages/fastapi_cloud_cli/commands/deploy.py_get_app_namer#       s    99    c                 X     g d}t         fd|D              ry j                  dk(  ryy)N)z.venv__pycache__z.mypy_cachez.pytest_cachec              3   :   K   | ]  }|j                   v   y wr    )parts).0partr   s     r"   	<genexpr>z(_should_exclude_entry.<locals>.<genexpr>(   s     
;$44::
;s   Tz.pycF)anysuffix)r   parts_to_excludes   ` r"   _should_exclude_entryr/   %   s-    O

;*:
;;{{fr$   c                    t         j                  d|        t        j                  | t        dg      }t        j                         }t         j                  d|       dt        j                          }t        |      | dz  }t         j                  d|       d}t        j                  |d	      5 }|D ]_  }|j                         rt         j                  d
|j                  |              |j                  ||j                  |              |dz  }a 	 d d d        t         j                  d|       |S # 1 sw Y   !xY w)Nz&Starting archive creation for path: %sz.fastapicloudignore)should_exclude_entryadditional_ignore_pathszCreated temp directory: %szfastapi-cloud-deploy-z.tarzArchive will be created at: %sr   wzAdding %s to archive)arcname   z*Archive created successfully with %s files)loggerdebugrignorewalkr/   tempfilemkdtempuuiduuid4r   tarfileopenis_dirrelative_toadd)r   filestemp_dirr!   tar_path
file_counttarfilenames           r"   archiverI   1   s   
LL94@LL2!6 7E !H
LL-x8"4::<.1DH~4&-H
LL18<J	h	$  	H LL/1E1Ed1KLGGHh&:&:4&@GA!OJ	 LL=zJO s   4A%D::Ec                   ,    e Zd ZU eed<   eed<   eed<   y)Teamidslugr!   N__name__
__module____qualname__str__annotations__ r$   r"   rK   rK   N   s    G
I
Ir$   rK   c                      t               5 } | j                  d      }|j                          |j                         d   }d d d        D cg c]  }t        j                  |       c}S # 1 sw Y   ,xY wc c}w )Nz/teams/data)r   getraise_for_statusjsonrK   model_validate)clientresponserV   teams       r"   
_get_teamsr^   T   sj    	 '::i(!!#}}v&	' 377$D%77' ' 8s   5A+A7+A4c                   "    e Zd ZU eed<   eed<   y)AppResponserL   rM   NrN   rT   r$   r"   r`   r`   ^   s    G
Ir$   r`   team_idapp_namec                     t               5 }|j                  d|| d      }|j                          t        j	                  |j                               cd d d        S # 1 sw Y   y xY w)N/apps/)r!   ra   rY   )r   postrX   r`   rZ   rY   )ra   rb   r[   r\   s       r"   _create_apprg   c   s^    	 ;;;"w7  

 	!!#))(--/:; ; ;s   A	AA'c                   Z    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZedd defd       Zy)DeploymentStatuswaiting_uploadready_for_buildbuilding
extractingextracting_failedbuilding_imagebuilding_image_failed	deployingdeploying_failed	verifyingverifying_failedsuccessfailedstatusr   c                 D   | j                   d| j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  d| j                  d	| j                  d
| j                  d| j                  d| j                  di|   S )NzWaiting for uploadzReady for buildBuilding
ExtractingzExtracting failedzBuilding imagezBuild failed	DeployingzDeploying failed	VerifyingzVerifying failedSuccessFailed)rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   )clsrw   s     r"   to_human_readablez"DeploymentStatus.to_human_readable~   s      4!2LL*NNL!!#6 0%%~MM;  "4MM;  "4KKJJ
  	r$   N)rO   rP   rQ   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   classmethodrR   r   rT   r$   r"   ri   ri   o   sh    %N'OHJ+%N3I)I)GF'9 c  r$   ri   c                   J    e Zd ZU eed<   eed<   eed<   eed<   eed<   eed<   y)CreateDeploymentResponserL   app_idrM   rw   dashboard_urlurlN)rO   rP   rQ   rR   rS   ri   rT   r$   r"   r   r      s"    GK
I	Hr$   r   r   c                     t               5 }|j                  d|  d      }|j                          t        j	                  |j                               cd d d        S # 1 sw Y   y xY w)Nrd   /deployments/)r   rf   rX   r   rZ   rY   )r   r[   r\   s      r"   _create_deploymentr      sY    	 H;;x}=>!!#'66x}}G	H H Hs   AAA&c                   ,    e Zd ZU eed<   eeef   ed<   y)RequestUploadResponser   fieldsN)rO   rP   rQ   rR   rS   r   rT   r$   r"   r   r      s    	HcNr$   r   deployment_idarchive_pathc           
         t         j                  d|        t         j                  d||j                         j                         t	               5 }t               5 }t         j                  d       |j                  d|  d      }|j                          t        j                  |j                               }t         j                  d|j                         t         j                  d       |j                  |j                  |j                  d|j                  d	      i
      }|j                          t         j                  d       t         j                  d       |j                  d|  d      }|j                          t         j                  d       d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nz-Starting deployment upload for deployment: %sz Archive path: %s, size: %s byteszRequesting upload URL from APIr   z/uploadzReceived upload URL: %szStarting file upload to S3filerb)rV   rC   z"File upload completed successfullyz%Notifying API that upload is completez/upload-completez%Upload notification sent successfully)r6   r7   statst_sizer   r   rf   rX   r   rZ   rY   r   r   r?   )r   r   fastapi_clientr[   r\   upload_dataupload_responsenotify_responses           r"   _upload_deploymentr      su   
LL7 LL*## 
 > >F56!&&}oW'MN!!#+::8==?K.@ 	12 ++OO##<,,T23 & 
 	((*9: 	<=(--M?*:;
 	((*<=7> > > > > >s%   F8E F,F8,F5	1F88Gapp_slugc                    t               5 }|j                  d|        }|j                  dk(  r
	 d d d        y |j                          |j	                         }d d d        t
        j                        S # 1 sw Y   xY w)Nrd   i  )r   rW   status_coderX   rY   r`   rZ   )r   r[   r\   rV   s       r"   _get_appr      sz    	 ::xj123&	  	!!#}} %%d++ s   %A6 A66A?c                     t               5 }|j                  dd| i      }|j                          |j                         d   }d d d        D cg c]  }t        j                  |       c}S # 1 sw Y   ,xY wc c}w )Nrd   ra   )paramsrV   )r   rW   rX   rY   r`   rZ   )ra   r[   r\   rV   apps        r"   	_get_appsr      ss    	 '::h	7/C:D!!#}}v&	' 8<<K&&s+<<' ' =s   9A/A;/A8c              #      K   t               5 }|j                  dd|  dd      5 }|j                          |j                         E d {    d d d        d d d        y 7 # 1 sw Y   xY w# 1 sw Y   y xY ww)NGETr   z/build-logs<   )timeout)r   streamrX   
iter_lines)r   r[   r\   s      r"   _stream_build_logsr      s     	 -]]]=/=r  
 	-%%'**,,,	-- - -	- 	-- -sE   A:A.$A"
A A"A.	A: A""A+	'A..A73A:)
u+   🚀 Preparing for liftoff! Almost there...uA   👹 Sneaking past the dependency gremlins... Don't wake them up!u>   🤏 Squishing code into a tiny digital sandwich. Nom nom nom.u?   📉 Server space running low. Time to delete those cat videos?uM   🐢 Uploading at blazing speeds of 1 byte per hour. Patience, young padawan.uN   🔌 Connecting to server... Please stand by while we argue with the firewall.uQ   💥 Oops! We've angered the Python God. Sacrificing a rubber duck to appease it.u3   🧙 Sprinkling magic deployment dust. Abracadabra!uB   👀 Hoping that @tiangolo doesn't find out about this deployment.uF   🍪 Cookie monster detected on server. Deploying anti-cookie shields.)uS   😅 Well, that's embarrassing. We're still waiting for the deployment to finish...u9   🤔 Maybe we should have brought snacks for this wait...u   🥱 Yawn... Still waiting...uK   🤯 Time is relative... Especially when you're waiting for a deployment...toolkitpath_to_deployc                 |   | j                  d| dd      st        j                  d      | j                          | j	                  d      5 }t        |d      5  t               }d d d        d d d        | j                          | j                  d	d
D cg c]  }t        |j                  |d       c}      }| j                          | j                  ddd      }| j                          |s| j	                  d      5 }t        |d      5  t        |j                        }d d d        d d d        | j                          s&| j                  d       t        j                  d      | j                  d|D cg c]  }t        |j                  |d       c}      }n| j                  dt        |            }| j                          | j	                  d      5 }t        |      5  t!        |j                  |      }d d d        |j#                  dj                          d d d        t%        j                  |j                        }	t'        ||	       |	S # 1 sw Y   xY w# 1 sw Y   xY wc c}w # 1 sw Y   _xY w# 1 sw Y   dxY wc c}w # 1 sw Y   xY w# 1 sw Y   xY w)NzSetup and deploy [blue]z[/]?dirtagr   zFetching teams...z-Error fetching teams. Please try again later.)messagez&Select the team you want to deploy to:r]   r!   valuer   optionsz Do you want to create a new app?r   T)r   defaultzFetching apps...z,Error fetching apps. Please try again later.z=No apps found in this team. You can create a new app instead.r5   z%Select the app you want to deploy to:)r   zWhat's your app name?)titler   zCreating app...r   z$App created successfully! App slug: )r   ra   )confirmtyperExit
print_lineprogressr   r^   askr   r!   r   rL   printrM   inputr#   rg   logr   r   )
r   r   r   teamsr]   create_new_appappsr   rb   
app_configs
             r"   _configure_appr     s   ??4^4DDIu?Ujjm			-	. !(M
 	! LE	!! ;;0HMNT:;N  D __*t % N 01 	*X#"P * !)*	* 	MMO **Q-kk3IMN#VSXX<=N  

 ==)!.1 ! 

 	$56 	L(#H- 5!$''845 LL?zJK		L #&&$'':J^Z0w	! 	!! ! O* *	* 	*" O5 5	L 	Lsx   I5I('I5JJJ*JJ!
(J24J&&J2(I2	-I55I?J	JJ&J/	+J22J;
deploymentc           
      p   t        t              }| j                  dd       | j                          | j                  d|j                   d|j                   d       | j                          d}t        j                         }t        j                         }| j                  t        |      dd	
      5 }t        |      5  t        |j                        D ]  }t        j                         |z
  }t        j                  |      }	d|	v r5|j                  t        j                   |	d   j#                                      |	j%                  d      dk(  r||j                  d       |j                  d|j&                   d|j&                   d       |j                  d       |j                  d|j                   d|j                   d        n|	j%                  d      dk(  rR|j                  d       |j                  d|j                   d|j                   d       t)        j*                  d      |dkD  rt        t,              }t        j                         |z
  dkD  st        |      |_        t        j                         } d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   y xY w)Nu+   Checking the status of your deployment 👀cloudr   z'You can also check the status at [link=][/link]g        T   )inline_logslines_to_show)r   r   typecomplete u3   🐔 Ready the chicken! Your app is ready at [link=z)You can also check the app logs at [link=rv   u>   😔 Oh no! Something went wrong. Check out the logs at [link=r5         )r   WAITING_MESSAGESr   r   r   time	monotonicr   nextr   r   rL   rY   loadsr   r   	from_ansirstriprW   r   r   r   LONG_WAIT_MESSAGESr   )
r   r   r   messagestime_elapsed
started_atlast_message_changed_atr   linerV   s
             r"   _wait_for_deploymentr   L  s    %&HMM5   MM
1*2J2J1K1ZMeMeLffmn L!J"nn.			XD 
 
 '?	2 $	?*:==9 #?#~~/*<zz$'$LLY0F0F0H!IJ88F#z1LL$LLMjnnM]]^_i_m_m^nnuv LL$LLCJD\D\C]]^_i_w_w^xx  A 88F#x/LL$LLXYcYqYqXrrst~  uM  uM  tN  NU  V  **Q-'"$$%78HNN$'>>!C%)(^HN.2nn.>+G#?$	?'? '?$	? $	?'? '?s+   6J,F#J ('J J, J)	%J,,J5c                       e Zd ZU eed<   dZee   ed<   dZee   ed<   dZ	ee   ed<   dZ
ee   ed<   dZee   ed<   dZee   ed<   dZee   ed	<   y)
SignupToWaitingListemailNr!   organizationrole	team_sizelocationuse_casesecret_code)rO   rP   rQ   r   rS   r!   r
   rR   r   r   r   r   r   r   rT   r$   r"   r   r     si    OD(3-"&L(3-&D(3-#Ix}#"Hhsm""Hhsm"!%K#%r$   r   resultc           	      V   |j                  d      5 }t               5 }t        |      5  |j                  d| j	                  d            }|j                          d d d        d d d        |j                  d       d d d        y # 1 sw Y   +xY w# 1 sw Y   /xY w# 1 sw Y   y xY w)NzSending your request...z/users/waiting-listrY   )modere   u9   Let's go! Thanks for your interest in FastAPI Cloud! 🚀)r   r   r   rf   
model_dumprX   r   )r   r   r   r[   r\   s        r"   _send_waitlist_formr     s     
		3	4 	R[ 	,F#H- ,!;;)0A0Av0A0N '  ))+,	, 	PQ	R 	R, ,	, 	,	R 	Rs9   BB4BB%BBBB	BB(c                 4   ddl m} | j                  dd       | j                          | j	                  ddt        t                    }| j                          t        |	      }| j                  d
d      r| j                           |d| j                        }|j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd       |j                  ddd       |j                  d d!d"       |j                         }	 t        j                  d#|i|      }| j                          | j                  d%d&      rv| j                          t        ||        t        j                   t"              5  t%        j                  d'd(gt$        j&                  t$        j&                  d)*       d d d        y y # t        $ r | j                  d$       Y y w xY w# 1 sw Y   y xY w)+Nr   )FormzkWe're currently in private beta. If you want to be notified when we launch, please fill out the form below.waitlistr   zEnter your email:T)required	validator)r   z?Do you want to get access faster by giving us more information?zWaitlist form)styler!   NamezJohn Doe)labelplaceholderr   Organizationz	Acme Inc.r]   rK   zTeam Ar   Role	Developerr   LocationzSan Franciscor   z%How do you plan to use FastAPI Cloud?zI'm building a web appr   zSecret code123456r   z.[error]Invalid form data. Please try again.[/]zDo you agree to
- Terms of Service: [link=https://fastapicloud.com/legal/terms]https://fastapicloud.com/legal/terms[/link]
- Privacy Policy: [link=https://fastapicloud.com/legal/privacy-policy]https://fastapicloud.com/legal/privacy-policy[/link]
termsr?   zraycast://confettiF)stdoutstderrcheck)rich_toolkit.formr   r   r   r   r   r   r   r   r   	add_inputrunrZ   r   r   
contextlibsuppress	Exception
subprocessDEVNULL)r   r   r   r   forms        r"   _waitlist_formr    s   &MMu  
 MMh'  E  u-FI   	O7==9vVD~^UvVBvVEzQ90 	 	

 	}MxP	(77UF K    		

   + 	NN-.!))!))		 	  	MM@ 	0	 	s   ;G. ,8H.H
HHz:A path to the folder containing the app you want to deploy)help	skip_waitz	--no-waitz"Skip waiting for deployment statusc                    t         j                  d       t         j                  d| |       t               5 }t               st         j                  d       |j	                  dd       |j                          |j                  dd       |j                          |j                  d	d
t        ddd      t        ddd      g      }|j                          |dk(  rt                n t        |       t        j                  d      |j	                  dd       |j                          | xs t        j                         }t         j                  d|       t        |      }|s3t         j                  d       t!        ||      }|j                          n6t         j                  d       |j                  d       |j                          |j#                  dd      5 }t%        |      5  t         j                  d|j&                         t)        |j&                        }ddd       s&t         j                  d       |j+                  d       ddd       s8|j                          |j                  dd        t        j                  d      t         j                  d!       t-        | xs t        j                               }|j#                  d"#      5 }t%        |      5  t         j                  d$|j.                         t1        |j.                        }	|j3                  d%|	j4                          |j3                  d&       t7        |	j.                  |       ddd       |j3                  d'       ddd       |j                          |s.t         j                  d(       t9        ||j.                  	)       nAt         j                  d*       |j                  d+	j:                   d,|	j:                   d-       ddd       y# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   yxY w).uB   
    Deploy a [bold]FastAPI[/bold] app to FastAPI Cloud. 🚀
    zDeploy command startedzDeploy path: %s, skip_wait: %sz3User not logged in, prompting for login or waitlistzWelcome to FastAPI Cloud!FastAPIr   z4You need to be logged in to deploy to FastAPI Cloud.infozWhat would you like to do?authzLogin to my existing accountr   r   zJoin the waiting listr   r   r5   zStarting deploymentzDeploying from path: %sz(No app config found, configuring new app)r   z5Existing app config found, proceeding with deploymentzDeploying app...zChecking app...T)	transientzChecking app with ID: %sNzApp not found in APIz>App not found. Make sure you're logged in the correct account.z_If you deleted this app, you can run [bold]fastapi unlink[/] to unlink the local configuration.tipzCreating archive for deploymentzCreating deploymentr   zCreating deployment for app: %sz2Deployment created successfully! Deployment slug: zUploading deployment...z!Deployment uploaded successfully!z"Waiting for deployment to complete)r   z%Skipping deployment wait as requestedz-Check the status of your deployment at [link=r   r   )r6   r7   r   r   print_titler   r   r   r   r   r  r   r   r   cwdr   r   r   r   r   r   	set_errorrI   rL   r   r   rM   r   r   r   )
r   r
  r   choicer   r   r   r   r   r   s
             r"   deployr    s    LL)*
LL14C		 ]w~LLNO ;K MMF    [[,$BWUV$;jQR ! F   w'jjm#1yA+.?#N3
LLCD'OJ LLPQMM,- /4@ 		H#H- 279J9JKz0012 34""T		  MMq   **Q-67t1txxz2$9: 	>h#H- 
@>G/7
HHYZ 67":==,?
@ LL<=	> 	LL=> #&&ZHLL@AMM?
@X@X?YYZ[e[s[sZtt{|w] ]`2 2		 		.
@ 
@	> 	>K] ]so   F.Q%P 16P'0P B
Q!P9-A;P-(P9B	QPP  P*	%Q-P62P99Q	>QQ)NF)Pr  rY   loggingr  r>   r:   r   r<   enumr   	itertoolsr   pathlibr   typingr   r   r   r	   r
   r   r8   r   httpxr   pydanticr   r   r   r   	rich.textr   rich_toolkitr   rich_toolkit.menur   typing_extensionsr    fastapi_cloud_cli.commands.loginr   fastapi_cloud_cli.utils.apir   fastapi_cloud_cli.utils.appsr   r   r   fastapi_cloud_cli.utils.authr   fastapi_cloud_cli.utils.clir   r   	getLoggerrO   r6   rR   r#   boolr/   rI   rK   r^   r`   rg   ri   r   r   r   r   r   r   r   r   r   r   r   r   r   r  Argumentr  rT   r$   r"   <module>r)     s              > >    F F  $ $ ' 2 1 T T 5 L			8	$  
	 	 	$ 4 :9 8DJ 8) 
	; 	; 	; 	;sD Dy Hs H'? HI 
&>c &> &>$ &>R,s ,x4 ,=s =tK0 =-c -iT4.H -  BK B B) BJ=?=?"%=?3K=?	=?@&) &RRR 
R NK ND Nn 	 	n
dDjM	
	n lell;-QRRn 	nr$   