Xׯd3ddlZddlZddlZejdkrddlmZddlZddlZddlZddlZddl m Z GddZ Gdde Z Gd d e ZdS) Nwin32)wintypes)PermissionDeniedErrorceZdZdZdZdZdZdS) FunctionTyperN)__name__ __module__ __qualname__SuccessBooleanStringData..\workbench\os_utils.pyrr!s"GG F DDDrrc8eZdZdZdZedZedZedZedZ edZ edZ ed Z ed Z edd Zed ZedZedZedZdS) FileUtilsa Provides implementation of functions to perform operations on the Windows File System. Exception handling is expected to be done by the callers: - FileOpsLocalWindows class for operations done as the CURRENT user - The wbadminhelper for operations done as ADMIN Some exception handling is done on these functions but only to be able to generate the PermissionDeniedError as it is used on further validations on the admin code. The functions can be divided in the next groups: - Success functions: they are expected to succeed, they don't return any value - Boolean functions: on success execution return a boolean value - String functions: on success execution return a string The functions will return the proper value on success or generate an exception in case of failure. The function names are self descriptive, however the function type will be indicated on each of them using the criteria explained above cdS)Nr)selfs r__init__zFileUtils.__init__@s rcld}tjdtjd}}tjjtj|dtj|tj|r)||j}||j}|d|d}nd}|S)( Function Type : String cgd}d}|t|kr6|dkr0t|dz }|dz}|t|kr|dk0d|||fzS)N)BKBMBGBTBrirz%.2f %s)lenfloat)totalmeasuresindexs rget_readable_formatz5FileUtils.get_free_space..get_readable_formatHsv444HE#h--''EDLLedl++ #h--''EDLLx77 7rrNz of z availablezCould not determine)ctypes c_ulonglongwindllkernel32GetDiskFreeSpaceExW c_wchar_ppointervalue)rpathr' total_bytes free_bytesfreer$ret_vals rget_free_spacezFileUtils.get_free_spaceCs  8 8 8#)"4Q"7"79KA9N9NZ = ! 5 5f6Ft6L6LdTZTbcnToToqwqAKrLrL M M ,&&z'788D'' (9::E.2ddEEE:GG+Grcd}tj|rtj|d} t |d}|tj|n#t$r#}|jtj krd}nYd}~n[d}~wwxYwtj |rttj dttj d|S)) Function Type : Boolean Tz.wb_write_testzw+FNz%The indicated path is not a directoryz!The indicated path does not exist) osr0isdirjoinopencloseremoveIOErrorerrnoEACCESexistsENOTDIRENOENT)rr0r4 dummy_filefes rcheck_dir_writablezFileUtils.check_dir_writable[s  7==   Md,<==J T**  *%%%%   7el**#GGGGGG  W^^D ! ! M%-)PQQ Q%,(KLL Ls8A<< B)B$$B)c@tj|Sr7)r8r0rArr0s rcheck_path_existszFileUtils.check_path_existsts w~~d###rc@tj|tjSrI)r8accessR_OKrJs rcheck_file_readablezFileUtils.check_file_readable{s yrw'''rc tj|dS#ttf$r-}|jtjkrt d|zd}~wwxYw) Function Type : Success zCould not create directory %sN)r8mkdirr>OSErrorr?r@rrr0errs rcreate_directoryzFileUtils.create_directorysb   HTNNNNN!   yEL((+,Kd,RSSS  A(AAc tj|dS#ttf$r-}|jtjkrt d|zd}~wwxYwrQ)r8makedirsr>rTr?r@rrUs rcreate_directory_recursivez$FileUtils.create_directory_recursivesg   K     !   yEL((+,Kd,RSSS  rXc tj|dS#ttf$r.}|jtjkrt d|z|d}~wwxYwrRzCould not remove directory %sN)r8rmdirr>rTr?r@rrUs rremove_directoryzFileUtils.remove_directorysd   HTNNNNN!   yEL((+,Kd,RSSSI A)AAc tj|dS#ttf$r.}|jtjkrt d|z|d}~wwxYwr])shutilrmtreer>rTr?r@rrUs rremove_directory_recursivez$FileUtils.remove_directory_recursivesi   M$     !   yEL((+,Kd,RSSSI r`c |rtj||tj||dS#ttf$r0}|jtjkrt d|d|d}~wwxYw)rRz Can't copy z to N)rbcopyr>rTr?r@r)rsourcetarget target_backuprFs r copy_filezFileUtils.copy_files  3 FM222 K ' ' ' ' '!   w%,&&++VVVVV,TUUU  s,0A1+A,,A1c tj|dS#ttf$r.}|jtjkrt d|z|d}~wwxYw)rRzCould not delete file %sN)r8r=r>rTr?r@rrUs r delete_filezFileUtils.delete_filesd   IdOOOOO!   yEL((+,F,MNNNI r`c $tjjj}tjjj}tjjj}d}d}d}d}t j} t j|_ t j t jtj t j t jtj t jg|_ |||ddtj| | jr>t j | jz} |||| | tj| rd}|rltj t j } t j} || tj| tj| rd}|rd} tj| }tj| }t j| }t j| }t j}|d| |tj||tj|tj|r|jd|j}nt#d |st%t&jd |S) rFrerNrT\zFailed to lookup userz)The given path is not a file or directory)r(r*advapi32GetFileSecurityWGetSecurityDescriptorOwnerLookupAccountSidWrDWORDBOOLrestypeLPCWSTRPOINTERBYTEargtypesbyrefr/create_unicode_bufferprintr>r?EINVAL)rr0rrrsrtlookup_accountget_owner_descriptorr4SECURITY_INFORMATION_OWNERlengthsdsdo sdo_defaultedSIZEnamedomaincch_name cch_domainsdo_types rget_file_ownerzFileUtils.get_file_ownersM "=1B%+]%;%V""M2D$%/"!!#+= $NN8=11NN8>22 %! 94FLQWDXDXYYY < ,-&,.11B &@"ffl[aNbNbcc ,'+$  &/&.//11C$MOOM))"fl3.?.?mA\A\]] &!%  /D/55D1$77F~d++H!--J~''H  sD$*L$:$:$*FL,D,D$*L$:$:<< / '-lllDJJ?-... U%,(STT Trc tj|rC|r4tj|}|jd|j|fz}||dS||dStj|}|D]}d}tj||}tj|}|j} tj| r$|dz }|rt|jd|}nI|}nFtj | stj | r|rt|jd|}n|}|r ||dS#ttf$r-} | jtjkrt!d|zd} ~ wwxYw)Nz%i %sre/ zPermission denied accessing %s)r8r0isfilestatst_sizelistdirr:st_modeS_ISDIRstrS_ISREGS_ISLNKr>rTr?r@r) rr0 include_sizeoutput_handler stat_infolinedlistitem item_path item_statrFs rlist_dirzFileUtils.list_dirs$ w~~d## -) " I%%"i&7%>>D"N4((((("N4((((( 4((!--DD " T4 8 8I " 2 2I ) 1I|I.. ( '(.1)2C.D.D.D.Ddd#KDD#'DDi00(DL4K4K('(.1)2C.D.D.D.Ddd#KDD#'D-&t,,,%--&!   w%,&&+,Lt,STTT  s%AE E$C!EF(FFc t|d}d}|D]}|r||kr |||dz}|dS#ttf$r-}|jtjkrt d|zd}~wwxYw)NrrrzCan't open file '%s')r;r<r>rTr?r@r)rr0skiprrEskippedrrFs rget_file_lineszFileUtils.get_file_lines;s T3AG * **w$"N4((((%kGG GGIIIII!   w%,&&+,BT,IJJJ  sAAB(BBN)re)r r r __doc__r classmethodr5rGrKrOrWr[r_rdrkrmrrrrrrrr(s.   [.[0$$[$ (([(   [   [   [   [    [   [ AA[AF%%[%N[rrc(eZdZdZedZdS)OSUtilsz Provides the exec_command functiono wich will execute whatever command is passed as long as it is valid for the OS. In case of failure this function will also raise the proper exception c.d} tj|tjdtjtjd}|r(t |jjdD] }||||j}n#t$r}d}~wwxYw|S)z Executes any OS valid command and sends the output to the command listener Syntax: EXEC command: A valida OS command. rzutf-8T)stdinencodingstdoutstderrshellreN) subprocessPopenPIPESTDOUTiterrreadlinewait returncode Exception)rcommandrretcodeprocessrexcs r exec_commandzOSUtils.exec_commandVs !&w RYdndsIPX\]]]G ) !8"==))D"N4(((( LLNNN(GG     sA=B B B  BN)r r r rrrrrrrrOs9 [rr)r8sysr(platformrr?rrbr wb_commonrrobjectrrrrrrs , <7  ++++++        ccccccccN #####f#####r