<s> import argparse <EOL> import datetime <EOL> import logging <EOL> import dateutil . parser <EOL> from dateutil import tz <EOL> from pytimeparse import timeparse <EOL> from paasta_tools import marathon_tools <EOL> log = logging . getLogger ( '<STR_LIT:__main__>' ) <EOL> logging . basicConfig ( ) <EOL> def parse_args ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , type = timedelta_type , default = '<STR_LIT>' , <EOL> help = "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> options = parser . parse_args ( ) <EOL> return options <EOL> def timedelta_type ( value ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return None <EOL> return datetime_seconds_ago ( timeparse . timeparse ( value ) ) <EOL> def datetime_seconds_ago ( seconds ) : <EOL> return now ( ) - datetime . timedelta ( seconds = seconds ) <EOL> def now ( ) : <EOL> return datetime . datetime . now ( tz . tzutc ( ) ) <EOL> def delete_deployment_if_too_old ( client , deployment , max_date , dry_run ) : <EOL> started_at = dateutil . parser . parse ( deployment . version ) <EOL> age = now ( ) - started_at <EOL> if started_at < max_date : <EOL> if dry_run is True : <EOL> log . warning ( "<STR_LIT>" % ( deployment . id , deployment . affected_apps [ <NUM_LIT:0> ] , age ) ) <EOL> else : <EOL> log . warning ( "<STR_LIT>" % ( deployment . id , deployment . affected_apps [ <NUM_LIT:0> ] , age ) ) <EOL> client . delete_deployment ( deployment_id = deployment . id , force = True ) <EOL> else : <EOL> if dry_run is True : <EOL> log . warning ( "<STR_LIT>" % ( deployment . id , deployment . affected_apps [ <NUM_LIT:0> ] , age ) ) <EOL> def main ( ) : <EOL> args = parse_args ( ) <EOL> config = marathon_tools . load_marathon_config ( ) <EOL> client = marathon_tools . get_marathon_client ( config . get_url ( ) , config . get_username ( ) , config . get_password ( ) ) <EOL> for deployment in client . list_deployments ( ) : <EOL> delete_deployment_if_too_old ( <EOL> client = client , <EOL> deployment = deployment , <EOL> max_date = args . age , <EOL> dry_run = args . dry_run , <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
