diff --git a/python/suricata/ctl/filestore.py b/python/suricata/ctl/filestore.py index 76ca5cb8b3..076e4ef310 100644 --- a/python/suricata/ctl/filestore.py +++ b/python/suricata/ctl/filestore.py @@ -31,10 +31,14 @@ class InvalidAgeFormatError(Exception): def register_args(parser): - parsers = parser.add_subparsers() - prune_parser = parsers.add_parser("prune") - prune_parser.add_argument("-d", "--directory", help="filestore directory") - prune_parser.add_argument("--age", help="prune files older than age") + subparser = parser.add_subparsers(help="sub-command help") + prune_parser = subparser.add_parser("prune", + help="Remove files in specified directory older than specified age") + required_args = prune_parser.add_argument_group("required arguments") + required_args.add_argument("-d", "--directory", + help="filestore directory", required=True) + required_args.add_argument("--age", + help="prune files older than age, units: s, m, h, d") prune_parser.add_argument( "-n", "--dry-run", action="store_true", default=False, help="only print what would happen") @@ -57,15 +61,13 @@ def parse_age(age): raise InvalidAgeFormatError(age) val = int(matched_age.group(1)) unit = matched_age.group(2) - if unit == "s": - return val - if unit == "m": - return val * 60 - if unit == "h": - return val * 60 * 60 - if unit == "d": - return val * 60 * 60 * 24 - raise InvalidAgeFormatError("bad unit: %s" % (unit)) + ts_units = ["s", "m", "h", "d"] + try: + idx = ts_units.index(unit) + except ValueError: + raise InvalidAgeFormatError("bad unit: %s" % (unit)) + multiplier = 60 ** idx if idx != 3 else 24 * 60 ** 2 + return val * multiplier def get_filesize(path): @@ -81,7 +83,6 @@ def remove_file(path, dry_run): def prune(args): - if args.verbose: logger.setLevel(logging.DEBUG) if args.quiet: @@ -106,7 +107,6 @@ def prune(args): # Do not go into the tmp directory. if "tmp" in dirnames: dirnames.remove("tmp") - for filename in filenames: path = os.path.join(dirpath, filename) mtime = os.path.getmtime(path) @@ -115,6 +115,5 @@ def prune(args): logger.debug("Deleting %s; age=%ds", path, this_age) size += remove_file(path, args.dry_run) count += 1 - logger.info("Removed %d files; %d bytes.", count, size) return 0 diff --git a/python/suricata/ctl/main.py b/python/suricata/ctl/main.py index a8125f96c1..d51a5be61e 100644 --- a/python/suricata/ctl/main.py +++ b/python/suricata/ctl/main.py @@ -34,11 +34,10 @@ def init_logger(): def main(): init_logger() - parser = argparse.ArgumentParser(description="Suricata Control Tool") - subparsers = parser.add_subparsers( - title="subcommands", - description="Commands") - filestore.register_args(subparsers.add_parser("filestore")) + parser = argparse.ArgumentParser() + subparsers = parser.add_subparsers(help='sub-command help') + fs_parser = subparsers.add_parser("filestore", help="Filestore related commands") + filestore.register_args(parser=fs_parser) args = parser.parse_args() try: func = args.func