
    jf"                       d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
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 dd	lmZmZmZmZmZmZmZmZmZmZm Z m!Z! dd
l"m#Z#m$Z$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+ ddl,m-Z-m.Z.m/Z/m0Z0 g dZ1ddZ2ddZ3ddZ4ddZ5ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ6dd	 	 	 	 	 	 	 ddZ7	 	 	 	 	 	 	 	 	 	 	 	 ddZ8y)z@Crash-safe, forward-only customer authority archive and cleanup.    )annotationsN)CallableIterable)UTCdatedatetimePath)Any)_require_customer_key_resolve_profile_root_resolve_registry_pathprofile_authority_lockwithdraw_customer)PHASESappend_phase	canonicalcopy_inventorydigest_bytesfreeze_archiveprune_sourcesread_journalsource_inventoryvalidate_fileverify_archivewrite_private) post_cleanup_authority_inventoryread_registry_staterelevant_rows)AuthorityInventoryCleanupBlockedErrorCleanupErrorCleanupIntegrityErrorCleanupReceipt)SharedLedgerProjectioncopy_shared_projectionsprepare_shared_projectionsverify_shared_projections)	r    r!   r"   r#   r$   r%   archive_customer_cleanupr   resume_customer_cleanupc                ,    | dz  dz  }|| dz  |dz  fS )Ndatazcustomer-cleanupz.journal.jsonlarchives )rootkeycleanups      /home/cube/projects/richard/traning coach/task-12-independent-verification-r9/build/hermes-full/dualcoach/profile/checkin_cli/customer_cleanup.py_pathsr3   :   s-    Vm00GuN++Wz-AAA    c                   t        |       }| dz  | dz  f}| dz  }g }|D ]o  }|j                         r|n| |z  }|j                  |j                         j	                  |       j                         t        |j                               d       q |j	                  |       j                         t        |j                               d|D cg c]@  }|j	                  |       j                         t        t        t        ||                  dB c}|j	                  |       j                         t        t        t        ||                  dt        |d       dS c c}w )	Nz%data/customer-activation-journal.jsonz$data/customer-activation-audit.jsonlzdata/scheduled-deliveries.jsonl)pathsha256)r6   rows_sha256c                    | d   S )Nr6   r.   )rows    r2   <lambda>z_bindings.<locals>.<lambda>T   s    CKr4   )r0   )registry
activation
sent_event	candidate)r   is_absoluteappendabsoluterelative_toas_posixr   
read_bytesr   r   sorted)	r/   r0   
candidatesr<   activation_paths	sent_pathcandidate_rows	requestedr6   s	            r2   	_bindingsrL   ?   su   %d+H6655 88I+-N	%113y	9IMMO//5>>@"4??#45
 	   &11$7@@Bl[c[n[n[pNqr )
( %%d+446|T]^klpru^vTwGxy(
  )44T:CCEVbclmz  |E  GJ  nK  dL  WM  NN0GH 
s   AE!c                x   	 t        |        t        j                  | j                  d            }t	        |t
              r?t	        |j                  d      t              r t	        |j                  dg       t              st        	 |S # t        t        t        j                  f$ r}t        d      |d }~ww xY w)Nzutf-8)encoding	inventoryshared_ledger_projectionszcleanup manifest is invalid)r   jsonloads	read_text
isinstancedictgetlist	TypeErrorOSErrorJSONDecodeErrorr#   )r6   valueexcs      r2   _load_manifestr]   X   s    
Ld

4>>7>;<5$'eii4d;eii(CRH$OO P
 L Y 4 45 L#$ABKLs   BB
 
B9(B44B9c           
     T    t        | |      d   |z  }t        |||||dz  ||dk(        S )N   manifest.json	committed)r3   r$   )r/   r0   journal	operationphasearchives         r2   _receiptrf   g   s<    T3"Y.G#y%'O:SU\^cgr^rssr4   r.   )candidate_pathsrP   kst_datefault_injectorc                  t        t        |             }t        |      }t        d |D              }t        |      }	t	        |      5  t        ||      \  }
}}|
r|s9|xs' t        j                  t              j                         }t        |||       t        ||      \  }}t        |      }|rt        |d   d         }nt        j                         j                   }t#        |||      }t%        |||	      }||z  }d||t        |      t        |      t'        |||      ||d}t)        |      dz   }t+        |dz  |d	
       t-        |d|t/        |             |r |d       t1        |||||      cddd       S # 1 sw Y   yxY w)zEArchive, byte-verify, and prune one withdrawn customer's authorities.c              3  2   K   | ]  }t        |        y w)Nr	   ).0r6   s     r2   	<genexpr>z+archive_customer_cleanup.<locals>.<genexpr>x   s     >odtDzos   )rh   r   operation_idzcustomer-cleanup-archive-v2)schema_versioncustomer_keyrn   source_rootdestinationinputsrO   rP      
r`   T)	exclusiveprepared)manifest_sha256N)r   r
   r   tupler   r   r   nowr   r   r   r3   r   struuiduuid4hexr   r'   rL   r   r   r   r   _resume_locked)profile_rootrp   rg   rP   rh   ri   r/   r0   rG   shareddisablednonconsenting_withdrawal_dayrb   r-   rowsrc   rO   shared_bindingsre   manifestmanifest_payloads                          r2   r)   r)   l   s    !l!34D

-C>o>>J,-F		%%8s%C"-}%Ac):)?)?)ANdC.A"4-G$DGN34I

((I(sJ?I8sFKO*G"? # )"4y"7|#D#z:&-<	H  )2U:'O35EQUV ,-= >	 z*dCG^LE 
&	%	%s   D0FF)ri   c          	        t        t        |             }t        |      }t        |      5  t	        ||      \  }}t        |      }|st        d      t        ||t        |d   d         ||      cddd       S # 1 sw Y   yxY w)zBResume the sole durable cleanup operation without moving backward.z0customer cleanup operation has not been preparedr   rn   N)	r   r
   r   r   r3   r   r"   r~   rz   )r   rp   ri   r/   r0   rb   r   r   s           r2   r*   r*      sy     !l!34D

-C		%D#&
G$QRRdCT!W^-D)EwP^_ 
&	%	%s   AA::Bc                   t        | |      d   |z  }|dz  }t        |      }t        |      }t        |j	                               |d   j                  d      k7  rt        d      |j                  d      |k7  s|j                  d      |k7  rt        d      |d	   }	|j                  d
g       }
t        |d   d         }t        j                  |      t        j                  d      k\  rt        ||	       t        | |||
       |dk(  rNt        | ||	       t        | |||
       t        ||	       t        | |||
       t        |d|       d}|r ||       |dk(  rUt        | |      }|j                   s|j"                  rt%        d      t'        | |	|       t        |d|       d}|r ||       |dk(  ryt        ||	       t        | |||
       t        | |      }|j(                  s|j                   s|j"                  rt%        d      t+        |       t        |d|       d}|r ||       t-        | ||||      S )Nr_   r`   r   rw   z cleanup manifest digest mismatchrp   rn   z#cleanup manifest authority mismatchrO   rP   rd   copied_verifiedrv   z4pending or unknown customer authority blocks cleanupsource_prunedz/customer authority remains after source pruningra   )r3   r]   r   r   rE   rV   r#   rz   r   indexr   r(   r   r&   r   r   pending_countunknown_countr!   r   active_countr   rf   )r/   r0   rc   rb   faultre   manifest_pathr   r   rO   r   rd   	authoritys                r2   r~   r~      s,    T3"Y.Go-Mm,H DM,,./47;;?P3QQ#$FGG||N#s*hll>.Ji.W#$IJJ%I\\5r:FR!"E||Efll+<==w	*!$f=
tWi0gsF;w	*!$f=W/;!%L!!4T3?	""i&=&=%&\]]dIs+Woy9%Lw	*!$f=4T3?	!!Y%<%<	@W@W%&WXXwWk95%LD#w	599r4   )r/   r
   r0   rz   returnztuple[Path, Path])r/   r
   r0   rz   rG   zIterable[Path]r   zdict[str, object])r6   r
   r   zdict[str, Any])r/   r
   r0   rz   rb   r
   rc   rz   rd   rz   r   r$   )r   
Path | strrp   rz   rg   zIterable[Path | str]rP   z Iterable[SharedLedgerProjection]rh   zdate | Noneri   Callable[[str], None] | Noner   r$   )r   r   rp   rz   ri   r   r   r$   )r/   r
   r0   rz   rc   rz   rb   r
   r   r   r   r$   )9__doc__
__future__r   rQ   r{   collections.abcr   r   r   r   r   pathlibr
   typingr   checkin_cli.customer_adminr   r   r   r   r   checkin_cli.customer_cleanup_fsr   r   r   r   r   r   r   r   r   r   r   r   &checkin_cli.customer_cleanup_inventoryr   r   r   #checkin_cli.customer_cleanup_modelsr    r!   r"   r#   r$   #checkin_cli.customer_cleanup_sharedr%   r&   r'   r(   __all__r3   rL   r]   rf   r)   r*   r~   r.   r4   r2   <module>r      sB   F "   . ( (       
  B
2t -/BD 370M0M0M *	0M
  @0M 0M 10M 0Mn 48	``` 1	`
 `"/:
/:/:%(/:37/:'/: /:r4   