ó Ë Rc@sÛdZddlZddlZddlZddljjZddlj Z ddl j j Z dej fd„ƒYZdej fd„ƒYZdejfd„ƒYZd ej fd „ƒYZd efd „ƒYZdS( sŽcentralized object for insert, lookup, and removal of files. The purpose of the FileManager class is to provide a central location to insert, lookup, and remove files, stored in the download directory of the client or the file directory of the repo. It provides a way to change the way files are placed into the directory structure by allowing for an ordered sequence of layouts. The FileManager overlays the layouts on top of each other. This means that layouts have certain requirements (described in the layout module), but allows the reuse of shared directory structures when possible. When a file is inserted, it is placed in the directory structure according to the first layout. When a file is retrieved, each layout is checked in turn to determine whether the file is present. If the file is present but not located according to where it should be located according to the first layout and the FileManager has permission to move the file, it wil be moved to that location. When a file is removed, the layouts are checked in turn until a file is found and removed. The FileManager also provides a way to generate all hashes stored by the FileManager.iÿÿÿÿNtNeedToModifyReadOnlyFileManagercBs#eZdZdd„Zd„ZRS(s\This exception is raised when the caller attempts to modify a read-only FileManager.tcreatecCs&tjj|ƒ||_||_dS(sCreate a NeedToModifyReadOnlyFileManager exception. The "thing_to_change" parameter is the entity that the file manager was asked to modify. The "create" parameter describes what kind of modification was being attempted.N(tapxt ApiExceptiont__init__tentR(tselftthing_to_changeR((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyR6s  cCs"tdƒi|jd6|jd6S(NsJThe FileManager cannot %(cre)s %(ent)s because it is configured read-only.tcreR(t_RR(R((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyt__str__Cs (t__name__t __module__t__doc__RR (((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyR2s tFMInsertionFailurecBs eZdZd„Zd„ZRS(s«Used to indicate that an in-progress insert failed because the item to be inserted went missing during the operation and wasn't already found in the cache.cCs&tjj|ƒ||_||_dS(N(RRRtsrctdest(RRR((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRNs cCstdƒ|jS(Ns]%(src)s was removed while FileManager was attempting to insert it into the cache as %(dest)s.(R t__dict__(R((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyR Ss (R R R RR (((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRIs tFMPermissionsExceptioncBseZdZd„ZRS(szThis exception is raised when a FileManager does not have the permissions to operate as needed on the file system.cCstdƒ|jS(NsEFileManager was unable to create %s or the directories containing it.(R tpath(R((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyR \s (R R R R (((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRXstUnrecognizedFilePathscBs eZdZd„Zd„ZRS(sqThis exception is raised when files are found under the FileManager's root which cannot be accounted for.cCstjj|ƒ||_dS(N(RRRtfps(Rt filepaths((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRescCstdƒdj|jƒS(NsZThe following paths were found but cannot be accounted for by any of the known layouts: %ss (R tjoinR(R((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyR is (R R R RR (((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRas t FileManagercBsVeZdZdd„Zd„Zd„Zeed„Z d„Z d„Z d„Z RS( s—The FileManager class handles the insertion and removal of files within its directory according to a strategy for organizing the files.cCsp|stdƒ‚n||_||_|dk r]t|tjƒsQ|g}n||_ntj ƒ|_dS(såInitialize the FileManager object. The "root" parameter is a path to the directory to manage. The "readonly" parameter determines whether files can be inserted, removed, or moved.sroot must not be noneN( t ValueErrortroottreadonlytNonet isinstancet collectionstIterabletlayoutstlayouttget_default_layouts(RRRR ((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRts     cCs t|_dS(sMake the FileManager read only.N(tTrueR(R((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyt set_read_only‡scCsd}d}d}xp|jD]e}|j|ƒ}tjj|j|ƒ}|dkr^|}n| swtjj|ƒr||fSqWd|fS(s Find the path to the file with name hashval. The "hashval" parameter is the name of the file to find. The "check_existence" parameter determines whether the function will ensure that a file exists at the returned path.N(RR tlookuptosRRRtexists(Rthashvaltcheck_existencetcur_patht cur_full_pathtdest_full_pathtl((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyt __select_path‹s  c Cs¥|j||ƒ\}}|s"dS||krŽ|j rŽtjj|ƒ}yytj||ƒWnétk rL}|j t j kr‹‚ntjj|ƒ}tjj |ƒrµ‚nytj |ƒWnpt k r8}|j t jksü|j t jkrt|jƒ‚n|j t jko,|j|ks9‚q9nXtj||ƒnX|}tj|ƒWqŽt k rŠtjj|ƒs‹dSqŽXn|r¡t|dƒS|S(sFind the file for hashval. The "hashval" parameter contains the name of the file to be found. The "opener" parameter determines whether the function will return a path or an open file handle.trbN(t_FileManager__select_pathRRR&RtdirnametportabletrenametOSErrorterrnotENOENTtisdirtmakedirstEnvironmentErrortEACCEStEROFSRtfilenametEEXISTt removedirsR'topen( RR(topenerR)R+R,tp_sdirtetp_ddir((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyR%¢sJ            cCsë|jrt|ƒ‚n|j|tƒ\}}|r®||kr®ytj||ƒWnLtk r¤}|jtjksŒ|jtj kržt |j ƒ‚n‚nX|}nxštrJytj||ƒWnxtk rE}t j j|ƒ}|jtjkr›t j j|ƒ r›yt j|ƒWqBtk r—}|jtjks[|jtj krmt |j ƒ‚n|jtjko‹|j |ks˜‚q˜qBXqG|jtjkrèt j j|ƒ rèt j j|ƒrÖ|St||ƒ‚qG|jtjks |jtj krt |j ƒ‚qG|jtjkrGtj|ƒ‚qGq±XPq±W|rçyt jt j j|ƒƒWqçtk rã}|jtjksà|jtjkr§qä|jtjksË|jtj krÝt |j ƒ‚qä‚qçXn|S(s|Add the content at "src_path" to the files under the name "hashval". Returns the path to the inserted file.(RRR0R#R2R3R9R5R:R;RR<R&RR1R6R7R8R=R'RRt_convert_errorR>(RR(tsrc_pathR+R,RBtp_dir((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pytinsertïsj       cCsú|jrt|dƒ‚nxØ|jD]Í}|j|ƒ}tjj|j|ƒ}y*tj |ƒtj tjj |ƒƒWq%t k rñ}|j t jksî|j t jkrµqò|j t jksÙ|j t jkrët|jƒ‚qò‚q%Xq%WdS(sRThis function removes the file associated with the name "hashval".tremoveN(RRR R%R&RRRR2RHR>R1R9R5R6R=R:R;RR<(RR(R-R*R+RB((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRHIs"   ccsÒg}x°tj|jƒD]œ\}}}xŠ|D]‚}tjj||ƒ}|t|jƒjtjjƒ}xB|jD]*}|j ||ƒrv|j |ƒVPqvqvW|j |ƒq/WqW|rÎt |ƒ‚ndS(s+Generate all the hashes of all files known.N( R&twalkRRRtlentlstriptsepR tcontainst path_to_hashtappendR(Rt unrecognizedtdirpathtdirnamest filenamestfntfpR-((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRI`s" "N( R R R RRR$R0tFalseR#R%RGRHRI(((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyRos   M Z (R RR5R&tpkg.client.api_errorstclientt api_errorsRt pkg.portableR2tpkg.file_layout.layoutt file_layoutR!RRRtPermissionsExceptionRRtobjectR(((sB/usr/lib/python2.7/vendor-packages/pkg/file_layout/file_manager.pyt(s