import sys
import os
import re
import boto
from datetime import datetime, timedelta
from s3put import multipart_upload

backups = []
daily_backups = 7
weekly_backups = 4
monthly_backups = 3

S3_BUCKET = 'bucket_name'
KEY_PREFIX = 'key_prefix/'

def rotate(key_prefix, key_ext, bucket_name):
    s3con = boto.connect_s3(None, None)
    bucket = s3con.get_bucket(bucket_name)
    keys = bucket.list(prefix=key_prefix)

    regex = '{0}-(?P<year>[\d]+?)-(?P<month>[\d]+?)-(?P<day>[\d]+?){1}'.format(key_prefix, key_ext)
    backups = []

    for key in keys:
        match = re.match(regex, str(key.key))
        if not match:
            continue
        year = int(match.group('year'))
        month = int(match.group('month'))
        day = int(match.group('day'))
        key_date = datetime(year, month, day)
        backups[:0] = [key_date]
    backups = sorted(backups, reverse=True)

    if len(backups) > daily_backups+1 and backups[daily_backups] - backups[daily_backups+1] < timedelta(days=7):
        key = bucket.get_key("{0}{1}{2}".format(key_prefix,backups[daily_backups].strftime("-%Y-%m-%d"), key_ext))
        print("deleting {0}".format(key))
        key.delete()
        del backups[daily_backups]

    month_offset = daily_backups + weekly_backups
    if len(backups) > month_offset+1 and backups[month_offset] - backups[month_offset+1] < timedelta(days=30):
        key = bucket.get_key("{0}{1}{2}".format(key_prefix,backups[month_offset].strftime("-%Y-%m-%d"), key_ext))
        print("deleting {0}".format(key))
        key.delete()
        del backups[month_offset]


def splitext( filename ):
    index = filename.find('.')
    if index == 0:
        index = 1+filename[1:].find('.')
    if index == -1:
        return filename, ''
    return filename[:index], filename[index:]
    return os.path.splitext(filename)


if __name__ == '__main__':
    print('Usage: python backup_rotate.py filename')

    file_path = sys.argv[1]
    basename = os.path.basename(file_path)
    key_base, key_ext = list(splitext(basename))
    key_prefix = "".join([KEY_PREFIX, key_base])
    key_datestamp = datetime.utcnow().date().strftime("-%Y-%m-%d")
    key = "".join([key_prefix, key_datestamp, key_ext])
    print("Uploading {0} to {1}".format(file_path, key))

    multipart_upload(S3_BUCKET, None, None, file_path, key, False, 0, None, 0)

    print('Rotating file on S3 with key prefix {0}'.format(key_prefix))
    rotate(key_prefix, key_ext, S3_BUCKET)


@@ -1,12 +1,28 @@
+import argparse%0A
 import sys%0Ai
@@ -300,16 +300,66 @@
 _name):%0A
+    %22%22%22 Check if we need to remove any files? %22%22%22%0A
     s3co
@@ -1675,16 +1675,179 @@
 name ):%0A
+    %22%22%22 Return the filename and extension according to the first dot in the filename.%0A        This helps date stamping .tar.bz2 or .ext.gz files properly.%0A    %22%22%22%0A
     inde
@@ -2099,54 +2099,395 @@
    p
-rint('Usage: python backup_rotate.py filename'
+arser = argparse.ArgumentParser(description='Upload a file to Amazon S3 and rotate old backups.')%0A    parser.add_argument('bucket', help=%22Name of the Amazon S3 bucket to save the backup file to.%22)%0A    parser.add_argument('prefix', help=%22The prefix to add before the filename for the key.%22)%0A    parser.add_argument('file', help=%22Path to the file to upload.%22)%0A    args = parser.parse_args(
 )%0A%0A 
@@ -2505,19 +2505,17 @@
 h = 
-sys.argv%5B1%5D
+args.file
 %0A   
@@ -2629,26 +2629,27 @@
 %22.join(%5B
-KEY_PREFIX
+args.prefix
 , key_ba
@@ -2776,16 +2776,17 @@
 y_ext%5D)%0A
+%0A
     prin
@@ -2834,17 +2834,16 @@
 , key))%0A
-%0A
     mult
@@ -2855,25 +2855,27 @@
 _upload(
-S3_BUCKET
+args.bucket
 , None, 
@@ -2966,16 +2966,34 @@
 efix %7B0%7D
+ and extension %7B1%7D
 '.format
@@ -3003,16 +3003,25 @@
 y_prefix
+, key_ext
 ))%0A    r
@@ -3047,20 +3047,22 @@
 ey_ext, 
-S3_BUCKET
+args.bucket
 )%0A%0A

Use argparse for command line arguments