cleantmp

cleanup a directory according to 0x0.st rules
git clone git://shroom.party/cleantmp.git
Log | Files | Refs | README | LICENSE

commit d33d3515c459d86851d861ff7f2810dd72cb7e61
Author: tx <trqx@goat.si>
Date:   Mon,  4 Mar 2019 21:15:00 +0100

init commit

Diffstat:
ALICENSE | 21+++++++++++++++++++++
AREADME | 20++++++++++++++++++++
Acleantmp | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/LICENSE b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 tx <trqx@goat.si> + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README @@ -0,0 +1,20 @@ +====[ cleantmp ]========================================================== + +clean direcory according to 0x0.st rules + +------[ usage ]----------------------------------------------------------- + +usage: cleantmp [-h] [--min-age MIN_AGE] [--max-age MAX_AGE] + [--max-size MAX_SIZE] [-v] [-d] + [PATH] + +positional arguments: + PATH path to directory + +optional arguments: + -h, --help show this help message and exit + --min-age MIN_AGE minimum age retention (30 days) + --max-age MAX_AGE maximum age retention (366 days) + --max-size MAX_SIZE maximum file size (8MB) + -v, --verbose print info about all files + -d, --delete actually delete files diff --git a/cleantmp b/cleantmp @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 +"""cleanup a directory according to 0x0.st rules""" + +from datetime import datetime +import argparse +from os import path, remove, scandir + + +def cleandir(args): + """clean a directory""" + dirpath = path.abspath(args.PATH) + with scandir(dirpath) as entries: + for entry in entries: + age = (datetime.now() - datetime.fromtimestamp(entry.stat().st_mtime)).days + + # dir + if not entry.is_file(): + continue + + # too young to die + if age <= args.min_age: + continue + + entry_max_age = ( + args.min_age + + (-args.max_age + args.min_age) + * (entry.stat().st_size / args.max_size - 1) ** 3 + ) + + if args.verbose: + print( + f"path: {entry.path} " + f"size: {entry.stat().st_size} " + f"age: {age} " + f"max_age: {entry_max_age}" + ) + + if age >= entry_max_age: + print(f"{entry.path}") + if args.delete: + remove(entry.path) + + +if __name__ == "__main__": + # pylint: disable=invalid-name + parser = argparse.ArgumentParser() + parser.add_argument( + "--min-age", help="minimum age retention (30 days)", type=int, default=30 + ) + parser.add_argument( + "--max-age", help="maximum age retention (365 days)", type=int, default=365 + ) + parser.add_argument( + "--max-size", help="maximum file size (8MB)", type=int, default=8 * 1024 * 1024 + ) + parser.add_argument( + "-v", "--verbose", help="print info about all files", action="store_true" + ) + parser.add_argument( + "-d", "--delete", help="actually delete files", action="store_true" + ) + parser.add_argument( + "PATH", help="path to directory", type=str, nargs="?", default="." + ) + cleandir(parser.parse_args())