<s> from yas3fs . YAS3FSPlugin import YAS3FSPlugin <EOL> import json <EOL> import os <EOL> import re <EOL> import errno <EOL> from stat import * <EOL> import datetime <EOL> import time <EOL> '''<STR_LIT>''' <EOL> class RecoverYas3fsPlugin ( YAS3FSPlugin ) : <EOL> def epochseconds_to_iso8601 ( self , s = None ) : <EOL> t = None <EOL> if s == None : <EOL> dt = datetime . datetime . now ( ) <EOL> else : <EOL> dt = datetime . datetime . utcfromtimestamp ( s ) <EOL> dt = dt . replace ( microsecond = <NUM_LIT:0> ) <EOL> rt = dt . isoformat ( ) <EOL> return rt <EOL> def stat_to_dict ( self , stat ) : <EOL> fn_map = { <EOL> '<STR_LIT>' : ( ST_MODE , str ) , <EOL> '<STR_LIT>' : ( ST_INO , str ) , <EOL> '<STR_LIT>' : ( ST_DEV , str ) , <EOL> '<STR_LIT>' : ( ST_NLINK , str ) , <EOL> '<STR_LIT>' : ( ST_UID , str ) , <EOL> '<STR_LIT>' : ( ST_GID , str ) , <EOL> '<STR_LIT>' : ( ST_SIZE , str ) , <EOL> '<STR_LIT>' : ( ST_ATIME , self . epochseconds_to_iso8601 ) , <EOL> '<STR_LIT>' : ( ST_MTIME , self . epochseconds_to_iso8601 ) , <EOL> '<STR_LIT>' : ( ST_CTIME , self . epochseconds_to_iso8601 ) <EOL> } <EOL> d = { } <EOL> for k in fn_map : <EOL> d [ k ] = fn_map [ k ] [ <NUM_LIT:1> ] ( stat [ fn_map [ k ] [ <NUM_LIT:0> ] ] ) <EOL> return d <EOL> def s3key_json_filter ( self , x ) : <EOL> if x [ <NUM_LIT:0> ] in ( '<STR_LIT>' ) : <EOL> return False <EOL> return True <EOL> def __init__ ( self , yas3fs , logger = None ) : <EOL> super ( RecoverYas3fsPlugin , self ) . __init__ ( yas3fs , logger ) <EOL> self . recovery_path = yas3fs . cache . cache_path + "<STR_LIT>" <EOL> self . cache = yas3fs . cache <EOL> self . logger . info ( "<STR_LIT>" % self . recovery_path ) <EOL> try : <EOL> os . makedirs ( self . recovery_path ) <EOL> self . logger . debug ( "<STR_LIT>" % self . recovery_path ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . EEXIST and os . path . isdir ( self . recovery_path ) : <EOL> self . logger . debug ( "<STR_LIT>" % self . recovery_path ) <EOL> pass <EOL> else : <EOL> raise <EOL> def make_recovery_copy ( self , cache_file ) : <EOL> path = re . sub ( self . cache . cache_path , '<STR_LIT>' , cache_file ) <EOL> path = re . sub ( '<STR_LIT>' , '<STR_LIT>' , path ) <EOL> recovery_file = self . recovery_path + path <EOL> self . logger . info ( "<STR_LIT>" % ( cache_file , recovery_file ) ) <EOL> recovery_path = os . path . dirname ( recovery_file ) <EOL> try : <EOL> os . makedirs ( recovery_path ) <EOL> self . logger . debug ( "<STR_LIT>" % recovery_path ) <EOL> except OSError as exc : <EOL> if exc . errno == errno . EEXIST and os . path . isdir ( recovery_path ) : <EOL> self . logger . debug ( "<STR_LIT>" % recovery_path ) <EOL> pass <EOL> else : <EOL> raise <EOL> import shutil <EOL> shutil . copyfile ( cache_file , recovery_file ) <EOL> self . logger . info ( "<STR_LIT>" % ( cache_file , recovery_file ) ) <EOL> return True <EOL> def do_cmd_on_s3_now_w_retries ( self , fn ) : <EOL> def wrapper ( * args , ** kargs ) : <EOL> try : <EOL> return fn ( * args , ** kargs ) <EOL> except Exception as e : <EOL> self . logger . error ( "<STR_LIT>" ) <EOL> selfless_args = None <EOL> if args [ <NUM_LIT:1> ] : <EOL> selfless_args = args [ <NUM_LIT:1> : ] <EOL> self . logger . error ( "<STR_LIT>" + "<STR_LIT:U+0020>" + str ( selfless_args ) ) <EOL> s = args [ <NUM_LIT:0> ] <EOL> key = args [ <NUM_LIT:1> ] <EOL> pub = args [ <NUM_LIT:2> ] <EOL> action = args [ <NUM_LIT:3> ] <EOL> arg = args [ <NUM_LIT:4> ] <EOL> kargs = args [ <NUM_LIT:5> ] <EOL> if pub [ <NUM_LIT:0> ] == '<STR_LIT>' : <EOL> try : <EOL> path = pub [ <NUM_LIT:1> ] <EOL> cache_file = s . cache . get_cache_filename ( path ) <EOL> cache_stat = os . stat ( cache_file ) <EOL> etag = None <EOL> etag_filename = s . cache . get_cache_etags_filename ( path ) <EOL> if os . path . isfile ( etag_filename ) : <EOL> with open ( etag_filename , mode = '<STR_LIT:r>' ) as etag_file : <EOL> etag = etag_file . read ( ) <EOL> json_recover = { <EOL> "<STR_LIT:action>" : action , <EOL> "<STR_LIT>" : self . epochseconds_to_iso8601 ( ) , <EOL> "<STR_LIT>" : pub [ <NUM_LIT:0> ] , <EOL> "<STR_LIT:file>" : path , <EOL> "<STR_LIT>" : cache_file , <EOL> "<STR_LIT>" : self . stat_to_dict ( cache_stat ) , <EOL> "<STR_LIT>" : etag_filename , <EOL> "<STR_LIT>" : etag , <EOL> "<STR_LIT>" : str ( e ) , <EOL> "<STR_LIT>" : dict ( filter ( self . s3key_json_filter , key . __dict__ . iteritems ( ) ) ) <EOL> } <EOL> self . logger . error ( "<STR_LIT>" + json . dumps ( json_recover ) ) <EOL> self . make_recovery_copy ( cache_file ) <EOL> except Exception , e : <EOL> self . logger . exception ( e ) <EOL> return args [ <NUM_LIT:2> ] <EOL> return wrapper </s>
