Compare commits

...

6 Commits

Author SHA1 Message Date
Bastian Venthur
618327adf5 reformatted 2023-11-12 17:24:48 +01:00
Bastian Venthur
f9f27953c6 Merge branch 'master' into delete_extra_files 2023-11-12 17:23:48 +01:00
Bastian Venthur
9168720ede Merge branch 'master' into delete_extra_files 2022-08-07 21:12:35 +02:00
Bastian Venthur
94ad898a86 added logger messanges on deletion 2022-08-07 21:10:51 +02:00
Bastian Venthur
ff3f8101ad added test 2022-08-06 22:29:48 +02:00
Bastian Venthur
8c0e69b2f4 Delete files that should not be in dest-dir 2022-08-06 21:55:09 +02:00
2 changed files with 42 additions and 0 deletions

View File

@@ -218,6 +218,7 @@ def build(args: argparse.Namespace) -> None:
""" """
os.makedirs(f"{args.output_dir}", exist_ok=True) os.makedirs(f"{args.output_dir}", exist_ok=True)
convertibles = [] convertibles = []
known_targets = []
for root, dirnames, filenames in os.walk(args.input_dir): for root, dirnames, filenames in os.walk(args.input_dir):
for filename in filenames: for filename in filenames:
rel_src = os.path.relpath( rel_src = os.path.relpath(
@@ -229,15 +230,22 @@ def build(args: argparse.Namespace) -> None:
rel_dst = rel_src rel_dst = rel_src
rel_dst = rel_dst[:-3] + ".html" rel_dst = rel_dst[:-3] + ".html"
convertibles.append((rel_src, rel_dst)) convertibles.append((rel_src, rel_dst))
known_targets.append(
os.path.abspath(f"{args.output_dir}/{rel_dst}")
)
else: else:
shutil.copy( shutil.copy(
f"{args.input_dir}/{rel_src}", f"{args.input_dir}/{rel_src}",
f"{args.output_dir}/{rel_src}", f"{args.output_dir}/{rel_src}",
) )
known_targets.append(
os.path.abspath(f"{args.output_dir}/{rel_src}")
)
for dirname in dirnames: for dirname in dirnames:
# all directories are copied into the output directory # all directories are copied into the output directory
path = os.path.relpath(f"{root}/{dirname}", start=args.input_dir) path = os.path.relpath(f"{root}/{dirname}", start=args.input_dir)
os.makedirs(f"{args.output_dir}/{path}", exist_ok=True) os.makedirs(f"{args.output_dir}/{path}", exist_ok=True)
known_targets.append(os.path.abspath(f"{args.output_dir}/{path}"))
# copy static files over # copy static files over
logger.info("Copying static files.") logger.info("Copying static files.")
@@ -273,6 +281,24 @@ def build(args: argparse.Namespace) -> None:
article_template, article_template,
) )
# clean up files that should not be there
for root, dirnames, filenames in os.walk(args.output_dir):
for filename in filenames:
dst = os.path.abspath(f"{root}/{filename}")
if dst not in known_targets:
logger.info(f"deleting {dst}")
os.remove(dst)
else:
known_targets.remove(dst)
for dirname in dirnames:
dst = os.path.abspath(f"{root}/{dirname}")
if dst not in known_targets:
logger.info(f"deleting {dst}")
shutil.rmtree(dst)
else:
known_targets.remove(dst)
logger.debug(known_targets)
generate_feed( generate_feed(
articles, articles,
args.output_dir, args.output_dir,

View File

@@ -312,6 +312,22 @@ foo bar
assert os.path.exists(f"{args.output_dir}/tags/bar.html") assert os.path.exists(f"{args.output_dir}/tags/bar.html")
def test_remove_extra_files(args):
"""Test that extra files are removed."""
# create a file and directory in output dir that have no corresponding
# source
file_path = f'{args.output_dir}/a'
dir_path = f'{args.output_dir}/b'
fh = open(file_path, 'w')
fh.close()
os.mkdir(dir_path)
blag.build(args)
assert not os.path.exists(file_path)
assert not os.path.exists(dir_path)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"template", "template",
[ [