<s> """<STR_LIT>""" <EOL> from datetime import datetime <EOL> from datetime import timedelta <EOL> import logging <EOL> from optparse import OptionParser <EOL> from mrjob . emr import EMRJobRunner <EOL> from mrjob . emr import iso8601_to_datetime <EOL> from mrjob . job import MRJob <EOL> from mrjob . options import _add_basic_opts <EOL> from mrjob . options import _alphabetize_options <EOL> from mrjob . parse import parse_s3_uri <EOL> from mrjob . util import scrape_options_into_new_groups <EOL> log = logging . getLogger ( __name__ ) <EOL> def main ( cl_args = None ) : <EOL> option_parser = _make_option_parser ( ) <EOL> options , args = option_parser . parse_args ( cl_args ) <EOL> MRJob . set_up_logging ( quiet = options . quiet , verbose = options . verbose ) <EOL> if not args or len ( args ) < <NUM_LIT:2> : <EOL> option_parser . error ( '<STR_LIT>' ) <EOL> time_old = _process_time ( args [ <NUM_LIT:0> ] ) <EOL> for path in args [ <NUM_LIT:1> : ] : <EOL> _s3_cleanup ( path , time_old , <EOL> dry_run = options . test , <EOL> ** _runner_kwargs ( options ) ) <EOL> def _s3_cleanup ( glob_path , time_old , dry_run = False , ** runner_kwargs ) : <EOL> """<STR_LIT>""" <EOL> runner = EMRJobRunner ( ** runner_kwargs ) <EOL> log . info ( '<STR_LIT>' % <EOL> ( glob_path , time_old ) ) <EOL> for path in runner . fs . ls ( glob_path ) : <EOL> bucket_name , key_name = parse_s3_uri ( path ) <EOL> bucket = runner . fs . get_bucket ( bucket_name ) <EOL> for key in bucket . list ( key_name ) : <EOL> last_modified = iso8601_to_datetime ( key . last_modified ) <EOL> age = datetime . utcnow ( ) - last_modified <EOL> if age > time_old : <EOL> log . info ( '<STR_LIT>' % ( key . name , age ) ) <EOL> if not dry_run : <EOL> key . delete ( ) <EOL> def _runner_kwargs ( options ) : <EOL> """<STR_LIT>""" <EOL> kwargs = options . __dict__ . copy ( ) <EOL> for unused_arg in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:test>' ) : <EOL> del kwargs [ unused_arg ] <EOL> return kwargs <EOL> def _process_time ( time ) : <EOL> if time [ - <NUM_LIT:1> ] == '<STR_LIT:m>' : <EOL> return timedelta ( minutes = int ( time [ : - <NUM_LIT:1> ] ) ) <EOL> elif time [ - <NUM_LIT:1> ] == '<STR_LIT:h>' : <EOL> return timedelta ( hours = int ( time [ : - <NUM_LIT:1> ] ) ) <EOL> elif time [ - <NUM_LIT:1> ] == '<STR_LIT:d>' : <EOL> return timedelta ( days = int ( time [ : - <NUM_LIT:1> ] ) ) <EOL> else : <EOL> return timedelta ( hours = int ( time ) ) <EOL> def _make_option_parser ( ) : <EOL> usage = '<STR_LIT>' <EOL> description = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> option_parser = OptionParser ( usage = usage , description = description ) <EOL> option_parser . add_option ( <EOL> '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:test>' , default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = "<STR_LIT>" ) <EOL> _add_basic_opts ( option_parser ) <EOL> scrape_options_into_new_groups ( MRJob ( ) . all_option_groups ( ) , { <EOL> option_parser : ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } ) <EOL> _alphabetize_options ( option_parser ) <EOL> return option_parser <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
