Skip to content

Commit

Permalink
Merge pull request #16 from univ-of-utah-marriott-library-apple/dev
Browse files Browse the repository at this point in the history
Overflow ability
  • Loading branch information
pdarragh committed May 19, 2015
2 parents 48606bd + f72646c commit fe81140
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cleanup_management/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import analysis
import cleanup

__version__ = '1.2.0'
__version__ = '1.3.0'
__all__ = ['analysis', 'cleanup']

if __name__ == "__main__":
Expand Down
20 changes: 10 additions & 10 deletions cleanup_management/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def get_date_based_deletable_inventory(keep_after, logger, target=None, folders=
return delete_folders, delete_files, delete_links


def get_size_based_deletable_inventory(target_space, logger, target=None, oldest_first=True, folders=None, files=None, links=None):
def get_size_based_deletable_inventory(target_space, logger, target=None, oldest_first=True, overflow=False, folders=None, files=None, links=None):
"""
Finds all of the items within an inventory that can be deleted based on a
given target amount of space to attempt to free up.
Expand Down Expand Up @@ -113,7 +113,7 @@ def get_size_based_deletable_inventory(target_space, logger, target=None, oldest
accumulated_size = 0

# Build up the deletion lists.
while accumulated_size <= target_space:
while accumulated_size < target_space:
logger.verbose(" target_space = {}".format(target_space))
logger.verbose(" accumulated_size = {}".format(accumulated_size))
# If we have folders left but no files, just look at the maximum value
Expand All @@ -127,10 +127,10 @@ def get_size_based_deletable_inventory(target_space, logger, target=None, oldest
logger.verbose(" age: {}".format(folder[2]))
# If that folder's size won't put us over the 'total_size' alotment,
# add it to the list of folders to be deleted.
if folder[2] <= target_space - accumulated_size:
if overflow or folder[2] <= target_space - accumulated_size:
delete_folders.append(folder[0])
accumulated_size += folder[2]
logger.verobse(" deleting")
logger.verbose(" deleting")
# In any case, remove the folder from the list of folders.
# This way we can keep iterating over the list and not get stuck on
# one value.
Expand All @@ -146,10 +146,10 @@ def get_size_based_deletable_inventory(target_space, logger, target=None, oldest
logger.verbose(" age: {}".format(file[2]))
# If the file's size won't put us over the 'total_size' alotment,
# add it do the list of files to be deleted.
if file[2] <= target_space - accumulated_size:
if overflow or file[2] <= target_space - accumulated_size:
delete_files.append(file[0])
accumulated_size += file[2]
logger.verobse(" deleting")
logger.verbose(" deleting")
# Even if the file is too big to be deleted, remove it from the list
# of files so we don't have to see it again.
files.remove(file)
Expand All @@ -171,20 +171,20 @@ def get_size_based_deletable_inventory(target_space, logger, target=None, oldest
if (oldest_first and folder[1] <= file[1]) or (not oldest_first and folder[2] >= file[2]):
logger.verbose(" folder preferred")
# Add the folder to the list of folders to be deleted.
if folder[2] <= target_space - accumulated_size:
if overflow or folder[2] <= target_space - accumulated_size:
delete_folders.append(folder[0])
accumulated_size += folder[2]
logger.verobse(" deleting folder")
logger.verbose(" deleting folder")
# Remove the folder from the list of folders.
folders.remove(folder)
# But if the file is older/larger...
else:
logger.verbose(" file preferred")
# Add the file to the list of files to be deleted.
if file[2] <= target_space - accumulated_size:
if overflow or file[2] <= target_space - accumulated_size:
delete_files.append(file[0])
accumulated_size += file[2]
logger.verobse(" deleting file")
logger.verbose(" deleting file")
# Remove the file from the list of files.
files.remove(file)

Expand Down
14 changes: 11 additions & 3 deletions cleanup_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
raise e


def main(target, keep_after, free_space, oldest_first, skip_prompt, logger):
def main(target, keep_after, free_space, oldest_first, skip_prompt, overflow, logger):
# Get an absolute reference to the target path.
target = os.path.abspath(os.path.expanduser(target))

Expand All @@ -33,7 +33,7 @@ def main(target, keep_after, free_space, oldest_first, skip_prompt, logger):
if keep_after is not None:
delete_folders, delete_files, delete_links = cleanup_management.analysis.get_date_based_deletable_inventory(keep_after=keep_after, logger=logger, folders=folders, files=files, links=links)
elif free_space is not None and oldest_first is not None:
delete_folders, delete_files, delete_links, deleted_space = cleanup_management.analysis.get_size_based_deletable_inventory(target_space=free_space, logger=logger, oldest_first=oldest_first, folders=folders, files=files, links=links)
delete_folders, delete_files, delete_links, deleted_space = cleanup_management.analysis.get_size_based_deletable_inventory(target_space=free_space, logger=logger, oldest_first=oldest_first, overflow=overflow, folders=folders, files=files, links=links)
else:
raise RuntimeError("Did not specify either --keep-after or --freeup.")

Expand Down Expand Up @@ -171,6 +171,12 @@ def usage():
--delete-largest-first
When deleting by size, larger items are deleted first to free up the
designated `--freeup` space.
--overflow
When deleting by size, this flag will ensure that at the very least the
amount designated will be deleted. (The default action is to delete up
to - but not more than - the amount.) This is useful when your top-level
directory only contains items that are greater in size than the target
free space amount.
target
The top-level directory to delete from within.
Expand Down Expand Up @@ -441,6 +447,7 @@ def volume_size_target(size, target, logger=None):
parser.add_argument('-f', '--freeup', default=None)
parser.add_argument('--delete-oldest-first', action='store_true', default=True)
parser.add_argument('--delete-largest-first', action='store_false', dest='delete_oldest_first')
parser.add_argument('--overflow', action='store_true')
parser.add_argument('target', nargs='?', default=os.getcwd())

# Parse the arguments.
Expand Down Expand Up @@ -499,9 +506,10 @@ def volume_size_target(size, target, logger=None):
free_space = free_space,
oldest_first = args.delete_oldest_first,
skip_prompt = args.skip_prompt,
overflow = args.overflow,
logger = logger,
)
except:
# Output the exception with the error name and its message. Suppresses the stack trace.
logger.error("{errname}: {error}".format(errname=sys.exc_info()[0].__name__, error=' '.join(sys.exc_info()[1])))
sys.exit(3)
raise
Binary file not shown.

0 comments on commit fe81140

Please sign in to comment.