backupcleaner/tests/filegenerator.py

75 lines
1.9 KiB
Python
Raw Normal View History

2022-05-31 13:16:32 +03:00
#!/usr/bin/env python3
"""
Generate files for tests.
"""
import argparse
from datetime import date, timedelta
from pathlib import Path
import os
DEFAULT_NUMBER_OF_FILES = 200
DEFAULT_PREFIX = "testfile-"
DEFAULT_SUFFIX = ".bak"
2022-05-31 16:50:01 +03:00
DEFAULT_TIMESTAMP_FORMAT = "%Y%m%d"
2022-05-31 13:16:32 +03:00
# Argument parser
2022-05-31 17:47:43 +03:00
parser = argparse.ArgumentParser(
description="Cleanup old backups",
epilog = "For a complete timestamp format description, see the python strftime() " +
"documentation: https://docs.python.org/3/library/datetime.html" +
"#strftime-strptime-behavior"
)
2022-05-31 13:16:32 +03:00
# path argument
parser.add_argument(
"path",
metavar = "PATH",
type = str,
nargs = 1,
help = "directory path"
)
# number of files
parser.add_argument(
"-n", "--number-of-files",
type = int,
default = DEFAULT_NUMBER_OF_FILES,
metavar = "N",
help = f"generate N files, default: {DEFAULT_NUMBER_OF_FILES}"
)
# prefix argument
parser.add_argument(
"-p", "--prefix",
type = str,
default = DEFAULT_PREFIX,
metavar = "PREFIX",
help = f"use PREFIX as file name prefix, default: {DEFAULT_PREFIX}"
)
# suffix argument
parser.add_argument(
"-s", "--suffix",
type = str,
default = DEFAULT_SUFFIX,
metavar = "SUFFIX",
help = f"use SUFFIX as file name suffix, default: {DEFAULT_SUFFIX}"
)
2022-05-31 16:50:01 +03:00
# timestamp format
parser.add_argument(
"-t",
"--timestamp-format",
type = str,
default = DEFAULT_TIMESTAMP_FORMAT,
metavar = "FORMAT",
help = f"format of timestamp, default: {DEFAULT_TIMESTAMP_FORMAT}".replace(r"%",r"%%")
)
2022-05-31 13:16:32 +03:00
args = parser.parse_args()
path = args.path[0]
number_of_files = args.number_of_files
prefix = args.prefix
suffix = args.suffix
2022-05-31 16:50:01 +03:00
timestamp_format = args.timestamp_format
2022-05-31 13:16:32 +03:00
for i in range(number_of_files):
2022-05-31 16:50:01 +03:00
timestamp = (date.today() - timedelta(days=i)).strftime(timestamp_format)
2022-05-31 13:16:32 +03:00
filename = f"{prefix}{timestamp}{suffix}"
Path(os.path.join(path, filename)).touch()