Skip to content

Commit

Permalink
Version 0.7: Added list_items for retrieving non-archive, non-log ite…
Browse files Browse the repository at this point in the history
…ms (images, other files). Added new criteria: endswith, except_endswith
  • Loading branch information
robballou committed Dec 30, 2013
1 parent 3d24237 commit 76cbd68
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
32 changes: 19 additions & 13 deletions README.md
@@ -1,6 +1,6 @@
# Rotatelib

Version: 0.6
Version: 0.7

Module for assisting in querying the file system, databases, or Amazon Web Services (AWS) for backups/archives to rotate.

Expand All @@ -27,6 +27,9 @@ Sample Python script using rotatelib:
# remove those backups we just found
rotatelib.remove_items(directory=backups, items=items)

# look for some images that have dates
items = rotatelib.list_items(directory=backups, before=datetime.timedelta(5), endswith=".png")

## Database example

You may also now give it database connections to work with:
Expand All @@ -45,25 +48,25 @@ You may also now give it database connections to work with:

## S3 example

If you have the [boto python library][1] installed, you can even access items in an S3
If you have the [boto python library][1] installed, you can even access items in an S3
bucket:

import datetime
import rotatelib

"""
When you call list_archives or remove_items with an s3bucket argument, the library
will look in your environment variables for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
If you do not want to use environment variables, you can pass those in as keyword args
(aws_access_key_id and aws_secret_access_key).
"""

# list all archive items
items = rotatelib.list_archives(s3bucket='mybucket')

# list all archive items that are older than 5 days
items = rotatelib.list_archives(s3bucket='mybucket', before=datetime.timedelta(5))

rotatelib.remove_items(items=items, s3bucket='mybucket')

## EC2 example
Expand All @@ -72,20 +75,20 @@ If you have the [boto python library][1] installed, you can even rotate ec2 snap

import datetime
import rotatelib

"""
When you call list_archives or remove_items with an ec2snapshots argument, the library
will look in your environment variables for AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
If you do not want to use environment variables, you can pass those in as keyword args
(aws_access_key_id and aws_secret_access_key).
"""

# list all archive items
items = rotatelib.list_archives(ec2snapshots=True)

# list all archive items that are older than 5 days
items = rotatelib.list_archives(ec2snapshots=True, before=datetime.timedelta(5))

rotatelib.remove_items(items=items, ec2snapshots=True)

By default, `list_archives` will use the snapshots description to find a date. If not date is found,
Expand All @@ -95,7 +98,7 @@ to use the `start_time`, you can use the `snapshot_use_start_time` option.
# list all archive items that are older than 5 days, using start_time
items = rotatelib.list_archives(ec2snapshots=True, before=datetime.timedelta(5), snapshot_use_start_time=True)

Note that the EC2 option will only look at snapshots owned by the account for the credentials that are
Note that the EC2 option will only look at snapshots owned by the account for the credentials that are
used.

## Criteria
Expand All @@ -105,20 +108,23 @@ To help query for the items you want, there are a number of criteria tests:
- after (datetime or timedelta)
- before (datetime or timedelta)
- day (int or list of ints)
- endswith (string or list of strings)
- except_day (int or list of ints)
- except_endswith (string or list of strings)
- except_hour (int or list of ints)
- except_startswith (string or list of strings)
- has_date (datetime)
- hour (int or list of ints)
- startswith (string or list of strings)
- pattern (regex)

**New in version 0.6:** `startswith` and `except_startswith` were added.
**New in version 0.7:** `startswith` and `except_startswith` were added. `list_items` and `has_date` were added.
**New in version 0.6:** `startswith` and `except_startswith` were added.
**New in version 0.2:** `day` and `except_day` were added. `day`, `hour`, `except_day`, and `except_hour` all accept lists as well.

## License

Copyright (c) 2011 Rob Ballou
Copyright (c) 2013 Rob Ballou

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
48 changes: 47 additions & 1 deletion rotatelib.py
Expand Up @@ -92,6 +92,13 @@ def connect_to_s3(aws_access_key_id, aws_secret_access_key):
return S3Connection(aws_access_key_id, aws_secret_access_key)


def has_date(fn):
parsed_name = parse_name(fn)
if parsed_name['date']:
return True
return False


def is_archive(fn):
"""
Determines if the requested filename is an archive or not. See parse_name()
Expand Down Expand Up @@ -213,6 +220,25 @@ def list_backup_tables(db, db_type=None, **kwargs):
return backup_tables


def list_items(directory='./', items=None, s3bucket=None, aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
if not items:
if not s3bucket:
# regular file system request
items = os.listdir(directory)
else:
# s3 request
try:
s3 = connect_to_s3(aws_access_key_id, aws_secret_access_key)
bucket = s3.get_bucket(s3bucket)
if directory == './':
directory = ''
items = [item for item in bucket.list(directory)]
except NameError, e:
raise Exception('To use the S3 library, you must have the boto python library: %s', e)
items = [archive for archive in items if has_date(archive) and meets_criteria(directory, archive, **kwargs)]
return items


def list_logs(directory='./', items=None, s3bucket=None, aws_access_key_id=None, aws_secret_access_key=None, **kwargs):
"""
List all of the log files in the directory that meet the criteria.
Expand Down Expand Up @@ -258,7 +284,7 @@ def meets_criteria(directory, filename, **kwargs):
after
before
day
exceot_day
except_day
except_hour
has_date
hour
Expand Down Expand Up @@ -307,6 +333,26 @@ def meets_criteria(directory, filename, **kwargs):
break
if passes:
return False
# name must end with the string
if 'endswith' in kwargs:
endswith = _make_list(kwargs['endswith'])
passes = False
for s in endswith:
if filename.endswith(s):
passes = True
break
if not passes:
return False
# name must not end with the string
if 'except_endswith' in kwargs:
endswith = _make_list(kwargs['endswith'])
passes = False
for s in endswith:
if filename.endswith(s):
passes = True
break
if passes:
return False
if name['date']:
if 'before' in kwargs:
# check if this is a timedelta object
Expand Down

0 comments on commit 76cbd68

Please sign in to comment.