forked from github.com/blag
Compare commits
6 Commits
4cc23cea5f
...
delete_ext
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
618327adf5 | ||
|
|
f9f27953c6 | ||
|
|
9168720ede | ||
|
|
94ad898a86 | ||
|
|
ff3f8101ad | ||
|
|
8c0e69b2f4 |
26
blag/blag.py
26
blag/blag.py
@@ -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,
|
||||||
|
|||||||
@@ -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",
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in New Issue
Block a user