Skip to content

Commit a52b8cd

Browse files
authored
Don't clear the output dir when generating markdown (#92)
* Don't clear the output dir when generating markdown * Don't use distutils.dir_utils * Fix a typo
1 parent f0acc15 commit a52b8cd

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

glean_parser/translate.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"""
1010

1111
from pathlib import Path
12+
import os
1213
import shutil
1314
import sys
1415
import tempfile
@@ -18,7 +19,15 @@
1819
from . import markdown
1920

2021

21-
OUTPUTTERS = {"kotlin": kotlin.output_kotlin, "markdown": markdown.output_markdown}
22+
# Each outputter in the table has the following keys:
23+
# - "output_func": the main function of the outputter, the one which
24+
# does the actual translation.
25+
# - "clear_output_dir": a flag to clear the target directory before moving there
26+
# the generated files.
27+
OUTPUTTERS = {
28+
"kotlin": {"output_func": kotlin.output_kotlin, "clear_output_dir": True},
29+
"markdown": {"output_func": markdown.output_markdown, "clear_output_dir": False},
30+
}
2231

2332

2433
def translate(input_filepaths, output_format, output_dir, options={}, parser_config={}):
@@ -49,13 +58,24 @@ def translate(input_filepaths, output_format, output_dir, options={}, parser_con
4958
# Write everything out to a temporary directory, and then move it to the
5059
# real directory, for transactional integrity.
5160
with tempfile.TemporaryDirectory() as tempdir:
52-
OUTPUTTERS[output_format](all_objects.value, Path(tempdir), options)
61+
tempdir_path = Path(tempdir)
62+
OUTPUTTERS[output_format]["output_func"](
63+
all_objects.value, tempdir_path, options
64+
)
5365

54-
if output_dir.is_file():
55-
output_dir.unlink()
56-
elif output_dir.is_dir():
57-
shutil.rmtree(output_dir)
66+
if OUTPUTTERS[output_format]["clear_output_dir"]:
67+
if output_dir.is_file():
68+
output_dir.unlink()
69+
elif output_dir.is_dir():
70+
shutil.rmtree(output_dir)
5871

59-
shutil.copytree(tempdir, output_dir)
72+
shutil.copytree(tempdir, output_dir)
73+
else:
74+
# We can't use shutil.copytree alone if the directory already exists.
75+
# However, if it doesn't exist, make sure to create one otherwise
76+
# shutil.copy will fail.
77+
os.makedirs(output_dir, exist_ok=True)
78+
for filename in tempdir_path.glob("*"):
79+
shutil.copy(filename, output_dir)
6080

6181
return 0

tests/test_translate.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77

88
import pytest
9+
import shutil
910

1011
from glean_parser import parser
1112
from glean_parser import translate
@@ -36,7 +37,7 @@ def test_translate_missing_directory(tmpdir):
3637
assert len(list(output.iterdir())) == 5
3738

3839

39-
def test_translate_remove_obsolete_files(tmpdir):
40+
def test_translate_remove_obsolete_kotlin_files(tmpdir):
4041
output = Path(tmpdir) / "foo"
4142

4243
translate.translate(
@@ -53,6 +54,27 @@ def test_translate_remove_obsolete_files(tmpdir):
5354
assert len(list(output.iterdir())) == 1
5455

5556

57+
def test_translate_retains_existing_markdown_files(tmpdir):
58+
output = Path(tmpdir) / "foo"
59+
60+
translate.translate(
61+
ROOT / "data" / "core.yaml",
62+
"markdown",
63+
output,
64+
parser_config={"allow_reserved": True},
65+
)
66+
67+
# Move the file to a different location, translate always writes to
68+
# metrics.md.
69+
shutil.move(output / "metrics.md", output / "old.md")
70+
71+
assert len(list(output.iterdir())) == 1
72+
73+
translate.translate(ROOT / "data" / "smaller.yaml", "markdown", output)
74+
75+
assert len(list(output.iterdir())) == 2
76+
77+
5678
def test_translate_expires():
5779
contents = [
5880
{

0 commit comments

Comments
 (0)