<s> """<STR_LIT>""" <EOL> import datetime <EOL> import errno <EOL> import logging <EOL> import grp <EOL> import os <EOL> import os . path <EOL> import pwd <EOL> import re <EOL> import shutil <EOL> import socket <EOL> import stat as stat_fn <EOL> import tempfile <EOL> import time <EOL> import boto . utils <EOL> log = logging . getLogger ( __name__ ) <EOL> def file_size ( file_path ) : <EOL> """<STR_LIT>""" <EOL> stat = os . stat ( file_path ) <EOL> assert stat_fn . S_ISDIR ( stat . st_mode ) == False <EOL> return stat . st_size <EOL> def file_md5 ( file_path ) : <EOL> """<STR_LIT>""" <EOL> log . debug ( "<STR_LIT>" , file_path ) <EOL> start_ms = time . time ( ) * <NUM_LIT:10> ** <NUM_LIT:3> <EOL> fp = open ( file_path , '<STR_LIT:rb>' ) <EOL> try : <EOL> md5 , _ , _ = boto . utils . compute_md5 ( fp ) <EOL> finally : <EOL> fp . close ( ) <EOL> duration_ms = ( time . time ( ) * <NUM_LIT:10> ** <NUM_LIT:3> ) - start_ms <EOL> log . debug ( "<STR_LIT>" , md5 , file_path , <EOL> duration_ms ) <EOL> return md5 <EOL> def ensure_dir ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . makedirs ( path ) <EOL> except ( EnvironmentError ) as e : <EOL> if not ( e . errno == errno . EEXIST and <EOL> e . filename == path ) : <EOL> raise <EOL> return <EOL> def maybe_remove_dirs ( path ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> os . removedirs ( path ) <EOL> except ( EnvironmentError ) as e : <EOL> if e . errno != errno . ENOTEMPTY : <EOL> raise <EOL> return <EOL> def human_disk_bytes ( bytes ) : <EOL> """<STR_LIT>""" <EOL> patterns = [ <EOL> ( <NUM_LIT> ** <NUM_LIT:3> , "<STR_LIT>" ) , <EOL> ( <NUM_LIT> ** <NUM_LIT:2> , "<STR_LIT:M>" ) , <EOL> ( <NUM_LIT> , "<STR_LIT>" ) <EOL> ] <EOL> for scale , label in patterns : <EOL> if bytes >= scale : <EOL> return "<STR_LIT>" . format ( i = ( bytes / scale ) , label = label ) <EOL> return "<STR_LIT>" % ( bytes , ) <EOL> class FileReferenceContext ( object ) : <EOL> log = logging . getLogger ( "<STR_LIT>" % ( __name__ , "<STR_LIT>" ) ) <EOL> def __init__ ( self , source_path ) : <EOL> """<STR_LIT>""" <EOL> self . _source_path = source_path <EOL> self . _stable_dir = None <EOL> self . stable_path = None <EOL> self . ignore_next_exit = False <EOL> def link ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __enter__ ( ) <EOL> return self . stable_path <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __exit__ ( None , None , None ) <EOL> return <EOL> def __enter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . stable_path : <EOL> return self . stable_path <EOL> _ , file_name = os . path . split ( self . _source_path ) <EOL> stable_dir = tempfile . mkdtemp ( prefix = "<STR_LIT>" % file_name ) <EOL> assert self . _source_path . startswith ( "<STR_LIT:/>" ) <EOL> stable_path = os . path . join ( stable_dir , self . _source_path [ <NUM_LIT:1> : ] ) <EOL> self . log . debug ( "<STR_LIT>" , stable_path , <EOL> self . _source_path ) <EOL> ensure_dir ( os . path . dirname ( stable_path ) ) <EOL> try : <EOL> os . link ( self . _source_path , stable_path ) <EOL> except ( EnvironmentError ) as e : <EOL> if e . errno == errno . ENOENT : <EOL> return None <EOL> raise <EOL> self . _stable_dir = stable_dir <EOL> self . stable_path = stable_path <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> if self . ignore_next_exit : <EOL> self . ignore_next_exit = False <EOL> return False <EOL> if self . _stable_dir : <EOL> self . log . debug ( "<STR_LIT>" , self . stable_path ) <EOL> shutil . rmtree ( self . _stable_dir ) <EOL> self . _stable_dir = None <EOL> self . stable_path = None <EOL> return False </s>
