<s> from __future__ import absolute_import <EOL> import calendar <EOL> import errno <EOL> import logging <EOL> import posixpath <EOL> import re <EOL> import sys <EOL> import time <EOL> from functools import wraps <EOL> from boto . exception import S3ResponseError <EOL> from hadoop . fs import normpath <EOL> ERRNO_MAP = { <EOL> <NUM_LIT> : errno . EACCES , <EOL> <NUM_LIT> : errno . ENOENT <EOL> } <EOL> DEFAULT_ERRNO = errno . EINVAL <EOL> S3_PATH_RE = re . compile ( '<STR_LIT>' ) <EOL> S3_ROOT = '<STR_LIT>' <EOL> def lookup_s3error ( error ) : <EOL> err_no = ERRNO_MAP . get ( error . status , DEFAULT_ERRNO ) <EOL> return IOError ( err_no , error . reason ) <EOL> def translate_s3_error ( fn ) : <EOL> @ wraps ( fn ) <EOL> def wrapped ( * args , ** kwargs ) : <EOL> try : <EOL> return fn ( * args , ** kwargs ) <EOL> except S3ResponseError : <EOL> _ , exc , tb = sys . exc_info ( ) <EOL> logging . error ( '<STR_LIT>' % exc ) <EOL> lookup = lookup_s3error ( exc ) <EOL> raise lookup . __class__ , lookup , tb <EOL> return wrapped <EOL> def parse_uri ( uri ) : <EOL> """<STR_LIT>""" <EOL> match = S3_PATH_RE . match ( uri ) <EOL> if not match : <EOL> raise ValueError ( "<STR_LIT>" % uri ) <EOL> key = match . group ( <NUM_LIT:3> ) or '<STR_LIT>' <EOL> basename = match . group ( <NUM_LIT:4> ) or '<STR_LIT>' <EOL> return match . group ( <NUM_LIT:1> ) , key , basename <EOL> def is_root ( uri ) : <EOL> """<STR_LIT>""" <EOL> return uri . lower ( ) == S3_ROOT <EOL> def abspath ( cd , uri ) : <EOL> """<STR_LIT>""" <EOL> if not uri . lower ( ) . startswith ( S3_ROOT ) : <EOL> uri = normpath ( join ( cd , '<STR_LIT:..>' , uri ) ) <EOL> return uri <EOL> def join ( * comp_list ) : <EOL> def _prep ( uri ) : <EOL> try : <EOL> return '<STR_LIT>' % parse_uri ( uri ) [ : <NUM_LIT:2> ] <EOL> except ValueError : <EOL> return '<STR_LIT:/>' if is_root ( uri ) else uri <EOL> joined = posixpath . join ( * map ( _prep , comp_list ) ) <EOL> if joined and joined [ <NUM_LIT:0> ] == '<STR_LIT:/>' : <EOL> joined = '<STR_LIT>' % joined <EOL> return joined <EOL> def s3datetime_to_timestamp ( datetime ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> stripped = time . strptime ( datetime [ : - <NUM_LIT:4> ] , '<STR_LIT>' ) <EOL> assert datetime [ - <NUM_LIT:4> : ] == '<STR_LIT>' , '<STR_LIT>' % datetime <EOL> except ValueError : <EOL> stripped = time . strptime ( datetime [ : - <NUM_LIT:5> ] , '<STR_LIT>' ) <EOL> assert datetime [ - <NUM_LIT:5> : ] == '<STR_LIT>' , '<STR_LIT>' % datetime <EOL> return int ( calendar . timegm ( stripped ) ) </s>
