8_5ZsddlZddlZddlmZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZmZdd lmZe eZd Zd efd ZGd dZGddeZGddeZGddeZdZdZdS)N)Path)Dict) WposError) USER_WPOS_DIR) setup_logging)ALL_OPTIMIZATION_FEATURES)home_diris_run_under_user)write_file_via_tempfilez.progress-statusreturncPttttS)N)rr rPROGRESS_STATUS_FILE_NAME py/clwpos/user/progress_check.pyget_progress_file_pathrs  M+D E EErceZdZUdZeed<fdZdZdee effdZ d dZ d d Z d d Z d d Zedee effd ZxZS)CommandProgresszK Class to represent user's AccelerateWP utility commands progress. total_stagesc d|D}||vr4td|dt|||}t t ||}|S)Nci|] }|j| Sr)command).0subclasss r z+CommandProgress.__new__..$s*   +3H h   rzInternal Error: command "z&" is not allowed, available commands: )__subclasses__rlistkeyssuperr__new__)clsrcommand_subclass_maprinstance __class__s rr zCommandProgress.__new__#s  7:7I7I7K7K    . . .WWW378L8Q8Q8S8S3T3TWWXX X'0(33;;HEErctstdt|_d|_d|_dS)Nz;Internal Error: trying to use CommandProgress class as rootFr)r rr progress_file in_progresscompleted_stages)selfargss r__init__zCommandProgress.__init__.sD "" [YZZ Z355  !rr c |j|jdS)zF Return number of total and completed stages as dict. rr(r-r)s ras_dictzCommandProgress.as_dict5s !- $ 5   rNcd|_d|_|jjddd|dS)z= Start tracking the progress of the command. Tri)modeparentsexist_okN)r'r(r&parentmkdir_update_progress_filer.s rstartzCommandProgress.start>sM  ! !''UD4'PPP ""$$$$$rc|jsdS|xjdz c_|j|jkrtd|dS)zg Update progress status by incrementing the number of completed stages by one. NzYInternal Error: the number of completed stages is greater than the total number of stages)r'r(rrr6r.s rupdatezCommandProgress.updateGsj   F "  4#4 4 4=  ""$$$$$rc|jsdSd|_|j|_|jr|jdSdS)zF Complete progress, delete file with progress status. NF)r'rr(r&existsunlinkr.s rcompletezCommandProgress.completeVsc  F  $ 1   $ $ & & (   % % ' ' ' ' ' ( (rczttj||jddS)Ni)r jsondumpsr/r&r.s rr6z%CommandProgress._update_progress_filebsA Jt||~~ & &(:E C C C C Crct}ddd}|s|S t|d5}tj|}dddn #1swxYwYt |dt |ddS#t$r'}t d||cYd}~Sd}~wwxYw) zu Return current progress status of the command execution, getting it from from special file. r9rr-rNrr(z$Can't parse progress status json: %s) rr<openr@loadsreadint Exceptionloggererror)r&defaultsfstatus_from_filees r get_statuszCommandProgress.get_statusis- /00 $%1==##%% O mS)) 8Q#':affhh#7#7  8 8 8 8 8 8 8 8 8 8 8 8 8 8 8!$$4^$D E E$'(89K(L$M$M     LL@! D D DOOOOOO s@B$'A." B$.A22B$5A26-B$$ C.C CC)r N)__name__ __module__ __qualname____doc__rG__annotations__r r+rstrr/r7r:r>r6 staticmethodrO __classcell__)r$s@rrrs     """ c3h    %%%% % % % % ( ( ( (CCCCS#X\rrc0eZdZUdZdZeed<dZeed<dS)EnableCommandProgresszS Class to represent user's AccelerateWP utility "enable" command progress. enabler rN rPrQrRrSrrUrTrrGrrrrYrYs>GSL#rrYc0eZdZUdZdZeed<dZeed<dS)DisableCommandProgresszL Class to represent user's WPOS utility "disable" command progress. disablerrNr\rrrr^r^s>GSL#rr^c<eZdZUdZdZeed<dZeed<de fdZ dS) GetCommandProgresszH Class to represent user's WPOS utility "get" command progress. getr r user_infocd}t|}td|D}||z}||ttzz|_dS)a" Recalculate and change total number of progress stages depending on the number of user's docroots and wordpresses. so we could update progress after checking every docroot and wordpress. Example of user_info structure: { 'docroot1': {'wps': [{'path': '', 'version': '5.9'}, {'path': 'wp2', 'version': '5.9'}], ... }, 'docroot2': {'wps': [{'path': '', 'version': '5.9'}], ... }, } c3@K|]}t|dVdS)wpsN)len)r doc_root_infos r zHGetCommandProgress.recalculate_number_of_total_stages..s@" " *7C e$ % %" " " " " " rN)rjsumvaluesrr)r)renumber_of_initial_stagesnumber_of_docrootsnumber_of_wordpresstotal_checks_per_modules r"recalculate_number_of_total_stagesz5GetCommandProgress.recalculate_number_of_total_stagess$%  ^^!" " ;D;K;K;M;M" " "   #57J"J4%,E(F(FFGrN) rPrQrRrSrrUrTrrGdictrsrrrrbrbsdGSL#GDGGGGGGrrbcFtjfd}|S)zX Decorator for the WPOS utility commands we want to track the progress for. ctjd|_|j |g|Ri||jS#|jwxYw)N_)rrPstripcommand_progressr7r>)r)r*kwargsfuncs rwrapperz'track_command_progress..wrappers / 0C0CC0H0H I I ##%%% -4.t...v..  ! * * , , , ,D ! * * , , , ,s  A--B functoolswrapsr{r|s` rtrack_command_progressrs: _T----- NrcFtjfd}|S)zu Decorator for the WPOS utility methods after which the counter of completed operations must be updated. cr|g|Ri|}t|d|j|S)Nry)getattrryr:)r)r*rzresultr{s rr|z(update_command_progress..wrappersMd,T,,,V,, 4+ , , 8  ! ( ( * * * rr}rs` rupdate_command_progressrs: _T Nr)r~r@pathlibrtypingrclwpos.cl_wpos_exceptionsrclwpos.constantsrclwpos.logsetuprclwpos.optimization_featuresr clwpos.utilsr r secureior rPrIrrrrYr^rbrrrrrrs //////******))))))BBBBBB44444444,,,,,, x .FFFFFaaaaaaaaHO_GGGGGGGG>        r