
    <Zyi.              
          S r SSKrSSKrSSKJr  SSK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   SSKJr  SS	KJr  SS
KJr  \R.                  " \R0                  S\R2                  " S5      \R4                  " \R6                  5      /S9  \R8                  " \5      rSS\S\S\\\\
4      4S jjr S\S\S\4S jr!S r"\S:X  a  \"" 5         gg! \ a    SSKJr  SS	KJr  SS
KJr   Nf = f)z
Fast script to check if top 20 active repositories (from local repos) are checked out and have latest pull.
This version checks local repositories first, then verifies they're up to date.
    N)Path)ListDictTupleOptional)datetime)RepoGitCommandError)InvalidGitRepositoryError)ConfigManager)BitbucketClient)
GitManager   z4%(asctime)s - %(name)s - %(levelname)s - %(message)sztop_repos_check_fast.log)levelformathandlers	repos_dirlimitreturnc                    / nU R                  5       (       d  [        R                  SU  35        / $ [        R                  SU  S35        U R	                  5        H  nUR                  5       (       d  M  US-  nUR                  5       (       d  M6   [        U5      n UR                  R                  5       (       ao  UR                  R                  n[        R                  " UR                  5      nUR                  X745        [        R                  SUR                   SU 35        M  UR                  U[        R                   45        M     UR)                  S SS9  US	U n	[        R                  S[+        U	5       S35        U	$ ! ["         aR  n[        R                  SUR                   SU 35        UR                  U[        R                   45         S	nAGM  S	nAff = f! [$        [&        4 a/  n[        R                  S
UR                   S35         S	nAGM  S	nAf["         a1  n[        R                  SUR                   SU 35         S	nAGM  S	nAff = f)z
Get local repositories sorted by most recent commit date.

Args:
    repos_dir: Path to repositories directory
    limit: Maximum number of repos to return
    
Returns:
    List of tuples (repo_path, last_commit_date) sorted by date (most recent first)
z'Repositories directory does not exist: zScanning local repositories in ....gitzFound z: last commit zCould not get commit date for : Nz	Skipping z: not a valid git repositoryzError processing c                     U S   $ )Nr    )xs    4C:\wamp64\www\sonar_scan\src\check_top_repos_fast.py<lambda>4get_local_repos_sorted_by_activity.<locals>.<lambda>[   s    !    T)keyreversez( local repositories (sorted by activity))existsloggerwarninginfoiterdiris_dirr	   headis_validcommitr   fromtimestampcommitted_dateappenddebugnamemin	Exceptionr   r
   sortlen)
r   r   repos_with_datesitemgit_dirrepolatest_commitcommit_dateeselecteds
             r   "get_local_repos_sorted_by_activityr=   $   s    57@LM	
KK1)C@A !!#{{}} -~~	:D>99%%''$(II$4$4M"*"8"89U9U"VK$++T,?@LL6$))N;-!PQ %++T8<<,@A- $F nd;  'H
KK&X'OPQO%  >!?		{"QCPQ ''x||(<==> *?; 	LL9TYYK/KLM 	NN.tyykA3?@	sP   G*BF,!F
G'AG"G*"G''G**I":#H$$I"1%II"	repo_path	bitbucketc                    U R                   nUSSSSSSS.nU R                  5       (       d  SUS'   U$ U S-  R                  5       (       d  SUS'   U$ SUS	'    [        U 5      n UR                  R	                  5       (       aZ  UR                  R
                  (       d  UR                  R                   OS
US'   UR                  R                  R                  US'   OSUS'   U$  UR                  SS9nUR                  5          UR                  U5      nU(       a  UR                  S5      US'   OPUS   (       aF  US   S
:w  a=  SUS    3nXR                  ;   a&  UR                  U   R                  R                  US'   US   (       a  US   (       a  US   US   :H  US'   U$ SUS'    U$ ! [         a  nSU 3US'   Us SnA$ SnAff = f! [         a  n[        R!                  SU SU 35        US   (       a`  US   S
:w  aW   SUS    3nXR                  ;   a&  UR                  U   R                  R                  US'    SnAN! [         a      SnANf = f SnANSnAff = f! ["         a	    SUS'    U$ [         a  nSU 3US'    SnAU$ SnAff = f! [$         a	    SUS'    U$ [         a  nSU 3US'    SnAU$ SnAff = f)z
Check if repository is checked out and up to date.

Args:
    repo_path: Path to repository directory
    bitbucket: BitbucketClient instance
    
Returns:
    Dictionary with status information
FN)	repo_slugchecked_out
up_to_datelocal_commitremote_commitbrancherrorz#Repository directory does not existrG   r   zNo .git directoryTrB   detachedrF   rD   zInvalid HEADzCould not get local commit: origin)r0   hashrE   zorigin/z+Could not fetch remote commit from API for r   rC   z:Could not compare commits (missing local or remote commit)zOrigin remote not configuredzError checking remote: zInvalid git repositoryUnexpected error: )r0   r#   r	   r)   r*   is_detachedactive_branchr+   hexshar2   remotefetchget_latest_commitgetrefsr$   r/   
ValueErrorr   )	r>   r?   rA   statusr8   r;   rI   r9   
remote_refs	            r   check_repo_status_fastrW   d   s    I F ?w&&((-w F=;3I		yy!!##BF))BWBW4#5#5#:#:]gx )-)9)9)@)@~&"0w%	<[[h[/FLLN ) ; ;I F .;.?.?.GF?+ h'F8,<
,J'.vh/?.@%A
%26:ii
6K6R6R6Y6YF?3 n%&*A(.~(>&BY(Y|$ M #_w Mc  	 <QC@F7OM	*  	J9+UWXYWZ[\(#x(8J(F'.vh/?.@%A
%26:ii
6K6R6R6Y6YF?3$ 	"  	=<F7O M  	< 7s;F7O M	< % 32w M  3.qc2wM3s   J $A8F. J F. %I+ ,G 1I+ 2AG "I+ &I+ .
G8	GGJ GJ 
I(.I#=I
I+ 

II#I+ II#I+ #I((I+ +J:J =	JJJ JJ K
.	K
7KK
c            
          [         R                  S5        [         R                  S5        [         R                  S5         [        5       n U R                  S5      (       d+  [         R	                  S5        [
        R                  " S5        [        U R                  S5      5      n[        U R                  S5      5      nU R                  S5      (       d+  [         R	                  S	5        [
        R                  " S5        U(       d2  U(       d+  [         R	                  S
5        [
        R                  " S5        [        U R                  S5      U R                  S5      U(       d  U R                  S5      OSU R                  S5      S9n[        U R                  S5      5      n[        USS9nU(       d  [         R                  S5        g[         R                  S5        [         R                  S[        U5       S35        [         R                  S5        / nSnSnSn	Sn
[        US5       GH{  u  nu  pUR                  n[         R                  SU S[        U5       SU S35        [         R                  SUR                  S5       35        [!        X5      nUR#                  U5        US   (       a  US-  nUS   (       a#  US-  n[         R                  SUS    S35        M  U	S-  n	[         R                  SUS    S35        US    (       aI  US!   (       a?  [         R                  S"US    SS#  35        [         R                  S$US!   SS#  35        GM0  [         R                  S%UR                  S&S'5       35        GM[  U
S-  n
[         R	                  S(US&    35        GM~     [         R                  S)5        [         R                  S*5        [         R                  S5        [         R                  S+[        U5       35        [         R                  S,U 35        [         R                  S-U	 35        [         R                  S(U
 35        [         R                  S5        U	S:  d  U
S:  a  [         R                  S.5        U Hk  nUS   (       d#  [         R                  S/US0    S1US&    35        M0  US   (       a  M<  [         R                  S2US0    S3UR                  SS45       S35        Mm     U
S:  d  U	S:  a  [
        R                  " S5        g[         R                  S55        [
        R                  " S5        g! [$         a7  n[         R	                  S6U 3S7S89  [
        R                  " S5         SnAgSnAff = f)9z2Main function to check top 20 active repositories.z<============================================================z/Checking Top 20 Active Repositories (Fast Mode)zbitbucket.workspacez"Bitbucket workspace not configuredr   zbitbucket.api_tokenzbitbucket.app_passwordzbitbucket.usernamez'Bitbucket username/email not configuredz'Bitbucket authentication not configuredN)	workspaceusernameapp_password	api_tokenzscanning.repos_dir   )r   zNo local repositories foundzChecking status of z repositories...r   z
[/z] Checking r   z    Last commit: z%Y-%m-%d %H:%M:%SrB   rC   z+  [OK] Checked out and up to date (branch: rF   )z/  [WARN] Checked out but needs update (branch: rD   rE   z    Local:     z    Remote: z    rG   zUnknown errorz  [ERROR] Not checked out: z=
============================================================SummaryzTotal repositories checked: z#  [OK] Checked out and up to date: z'  [WARN] Checked out but needs update: z 
Repositories needing attention:z
  [ERROR] rA   r   z	  [WARN] z: Needs update (branch: zN/AzB
[SUCCESS] All top 20 repositories are checked out and up to date!rK   T)exc_info)r$   r&   r   rR   rG   sysexitboolr   r   r=   r%   r4   	enumerater0   strftimerW   r.   r2   )confighas_api_tokenhas_app_passwordr?   r   	top_reposresultschecked_out_countup_to_date_countneeds_update_countnot_checked_out_countir>   last_commit_daterA   rU   r;   s                    r   mainrs      s   
KK
KKAB
KKe zz/00LL=>HHQKVZZ(=>?

+C DEzz.//LLBCHHQK%5LLBCHHQK $jj!67ZZ 45ER$<=X\jj!67	
	 $89:	 7yK	NN89H)#i.)99IJKH  !09)Q0G,A,	!IKK#aS#i.!1YKsKLKK+,<,E,EFY,Z+[\]+IAFNN6"m$!Q&!,'$)$KK"MfU]N^M__` ab&!+&NN%TU[\dUeTffg#hin-&2If^6LRa6P5Q'RSf_6Mbq6Q5R'STfjj/.R-S'TU%*%:6'?:KLM/ 1H4 	O$IH23y>2BCD9:J9KLM=>P=QRS12G1HIJH !%:Q%>KK;<!m,KK*VK-@,AF7OCT UV--KK)F;,?+@@XY_YcYcdlnsYtXuuv wx	 " !1$(:Q(>HHQKKK]^HHQK )!-=s-   F
U K:U 
AU  +U 
V-VV__main__)r]   )#__doc__rc   loggingpathlibr   typingr   r   r   r   r   gitr	   r
   git.excr   config_managerr   bitbucket_clientr   git_managerr   ImportErrorbasicConfigINFOFileHandlerStreamHandlerstdout	getLogger__name__r$   intr=   rW   rs   r   r    r   <module>r      s      . .  % -(,0&   
,,A67cjj) 
		8	$=$ =s =DQVW[]eWeQfLg =@_d _ _4 _Dk\ zF E	  (-1'(s   C C+*C+