U g@sNdZdgZddlZddlZddlZddlZddlZddlZGdddeZ dS)z4!Contains the Listing class, which emulates "ls -l".ListingNc@s\eZdZdZdddZddZd d Zd d Zdd dZddZ ddZ ddZ ddZ dS)ra!Imitates the shell "ls -l" program. Imitate ls -l, but with a longer mtime string: @code print Listing("/usr/local") @endcode To include files whose names begin with "." add hidden=True: @code print Listing("/usr/local",hidden=True) @endcode To log messages related to failures of lstat and readlink, pass a logging.Logger: @code print Listing("/usr/local",hidden=True,logger=logger) @endcode.FNcCs$||_t|_|jt||ddS)z!Constructor for Listing: @param path The directory path to list. @param hidden If True, files with names beginning with "." are listed. @param logger A logging.Logger for error messages.)hiddenloggerN)_Listing__pathdict_Listing__contentslistbool)selfpathrrr =/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/listing.py__init__szListing.__init__ccs|jD] }|Vq dSz1!Iterates over filenames in the listed directory.Ncontentskeysr namer r r__iter__%szListing.__iter__ccs"|jD]\}}||fVq dS)z!Iterates over name,data pairs in the listed directory. The "data" will be a tuple containing the output of lstat and the output of readlink.N)ritems)r rdatar r r iteritems)szListing.iteritemsccs|jD] }|Vq dSrrrr r riterkeys/szListing.iterkeysc CsBt|}|j}t|}t}|D]}|ddkr:|s:q"tj||}zt|}WnLtk r} z.|dk r|j d|t | fddWYq"W5d} ~ XYnXt |j s|df||<q"zt|} Wn\tk r(} z<|dk r|j d|t | fdd|df||<WYq"W5d} ~ XYnX|| f||<q"||_dS) a !Updates the internal data structures with a new listing of the directory. Arguments are the same as for the constructor. @param hidden If True, files with names beginning with "." are listed. @param logger A logging.Logger for error messages.rrNz%s: cannot lstat: %sT)exc_infoz%s: cannot readlink: %s (**UNKNOWN**))r roslistdirrr joinlstatEnvironmentErrorinfostrstatS_ISLNKst_modereadlinkr) r rrr listingritemlocr elinkpathr r rr 4s@      z Listing.listc Cs|j}ddddddg}t}|D]N\}}|||}tt|dD]}t||t||||<qH||q$d}|dd}|t|}t } |D]}| ||q| } | | S)z3!Generates an ls -l style listing of the directory.rz) %%%ds %%%ds %%%ds %%%ds %%%ds %%%ds %%s N)rr r_stritemrangelenmaxappendtupleioStringIOwritegetvalueclose) r csizesrowsrr)rowcolformatsstr r r__str__Vs"    zListing.__str__c Cs|d}|d}|dkrd}|j}d}t|r:|d7}nlt|rN|d7}nXt|rb|d7}nDt|rv|d 7}n0t|r|d 7}nt|r|d 7}n|d 7}||tj@rd nd 7}||tj @rdnd 7}|tj @r|tj @r|d 7}n|d7}n|tj @r|d7}n|d 7}||tj @r*d nd 7}||tj @rBdnd 7}|tj@rt|tj@rj|d 7}n|d7}n|tj@r|d7}n|d 7}||tj@rd nd 7}||tj@rdnd 7}|tj@r|tj@r|d7}n|d7}n|tj@r|d7}n|d 7}tt|j}||j}||j} tt|j} tt|j} t|jrz|||| | | |d|fS|||| | | |fSdS)a!This is an internal implementation function. Do not call it directly. It generates one line of ls output as a tuple of strings, one string per column of information (the mtime is one column). The __str__ turns the lines into one big string. @param name the filename @param item details about the file @returns data for each column of the listingrr-Nrdr9bplr?-rwxStTz -> ) r&r$S_ISDIRS_ISCHRS_ISBLKS_ISFIFOr%S_ISSOCKS_IRUSRS_IWUSRS_IXUSRS_ISUIDS_IRGRPS_IWGRPS_IXGRPS_ISGIDS_IROTHS_IWOTHS_IXOTHS_ISVTXr#intst_nlink _usernamest_uid _groupnamest_gidst_sizetimeasctime localtimest_mtime) r rr)r r,moder?ZnlinkusernameZ groupnamesizewhenr r rr.jsl                          zListing._stritemc Cs>zt|}t|jWStttfk r8t|YSXdS)z4!Return the group name for a group ID from getgrgid.N)grpgetgrgidr#gr_nameKeyError ValueErrorr!)r gidZgrr r rrcs   zListing._groupnamec Cs>zt|}t|jWStttfk r8t|YSXdS)z3!Return the user name for a group ID from getpwuid.N)pwdgetpwuidr#pw_namerqrrr!)r uidpwr r rras   zListing._username)rFN)FN) __name__ __module__ __qualname____doc__rrrrr rAr.rcrar r r rr s  "K) r|__all__rr$r4rnrtrfobjectrr r r rs0