Skip to content

Commit 0d1ca13

Browse files
authored
Merge pull request #818 from nipy/enh-one-README
Do not create README if there is other allowed README.md or alike
2 parents 9e1e601 + 5855da4 commit 0d1ca13

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

heudiconv/bids.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -176,12 +176,12 @@ def populate_bids_templates(
176176
if op.exists(op.dirname(sourcedata_README)):
177177
create_file_if_missing(
178178
sourcedata_README,
179-
(
180-
"TODO: Provide description about source data, e.g. \n"
181-
"Directory below contains DICOMS compressed into tarballs per "
182-
"each sequence, replicating directory hierarchy of the BIDS dataset"
183-
" itself."
184-
),
179+
"TODO: Provide description about source data, e.g. \n"
180+
"Directory below contains DICOMS compressed into tarballs per "
181+
"each sequence, replicating directory hierarchy of the BIDS dataset"
182+
" itself.",
183+
# TODO: get from schema
184+
glob_suffixes=[".md", ".txt", ".rst", ""],
185185
)
186186
create_file_if_missing(
187187
op.join(path, "CHANGES"),
@@ -196,6 +196,8 @@ def populate_bids_templates(
196196
op.join(path, "README"),
197197
"TODO: Provide description for the dataset -- basic details about the "
198198
"study, possibly pointing to pre-registration (if public or embargoed)",
199+
# TODO: get from schema
200+
glob_suffixes=[".md", ".txt", ".rst", ""],
199201
)
200202
create_file_if_missing(
201203
op.join(path, "scans.json"), json_dumps(SCANS_FILE_FIELDS, sort_keys=False)

heudiconv/tests/test_main.py

+24-3
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,35 @@ def test_main_version(std: StringIO) -> None:
4444

4545

4646
def test_create_file_if_missing(tmp_path: Path) -> None:
47-
tf = tmp_path / "README.txt"
47+
tf_base = tmp_path / "README"
48+
tf = tf_base.with_suffix(".txt")
4849
assert not tf.exists()
49-
create_file_if_missing(str(tf), "content")
50+
assert create_file_if_missing(str(tf), "content")
5051
assert tf.exists()
5152
assert tf.read_text() == "content"
52-
create_file_if_missing(str(tf), "content2")
53+
assert not create_file_if_missing(str(tf), "content2")
5354
# nothing gets changed
5455
assert tf.read_text() == "content"
56+
# matching glob - no change
57+
for g in [".md", ".txt", ".json"], [".*"], [".*", ".txt"]:
58+
assert not create_file_if_missing(str(tf_base), "content2", glob_suffixes=g)
59+
assert tf.exists()
60+
assert tf.read_text() == "content"
61+
assert not tf_base.exists() # was not created, since there is .md
62+
63+
# non-matching glob - change
64+
for g in [".md", ".json"], [".d*", ".*d"]:
65+
assert create_file_if_missing(str(tf_base), "content3", glob_suffixes=g)
66+
assert tf_base.read_text() == "content3"
67+
# now that we have suffix less README, we do not match, so we keep creating it
68+
assert create_file_if_missing(str(tf_base), "content4", glob_suffixes=g)
69+
assert tf_base.read_text() == "content4"
70+
# unless we list it explicitly
71+
assert not create_file_if_missing(
72+
str(tf_base), "content5", glob_suffixes=g + [""]
73+
)
74+
assert tf_base.read_text() == "content4"
75+
tf_base.unlink()
5576

5677

5778
def test_populate_bids_templates(tmp_path: Path) -> None:

heudiconv/utils.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,20 @@ def anonymize_sid(sid: AnyStr, anon_sid_cmd: str) -> AnyStr:
164164
return anon_sid
165165

166166

167-
def create_file_if_missing(filename: str, content: str) -> bool:
167+
def create_file_if_missing(
168+
filename: str, content: str, glob_suffixes: list[str] | None = None
169+
) -> bool:
168170
"""Create file if missing, so we do not
169-
override any possibly introduced changes"""
170-
if op.lexists(filename):
171+
override any possibly introduced changes.
172+
173+
Note: if glob_suffixes list is used, and it is desired
174+
also to allow for original filename to 'match', add an empty
175+
string within glob_suffixes.
176+
"""
177+
if glob_suffixes:
178+
if any(glob(filename + s) for s in glob_suffixes):
179+
return False
180+
elif op.lexists(filename):
171181
return False
172182
dirname = op.dirname(filename)
173183
if not op.exists(dirname):

0 commit comments

Comments
 (0)