Skip to content

Commit

Permalink
CleanOutput.py: support glob patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Apr 9, 2024
1 parent fab01a9 commit d51f2c8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
3 changes: 3 additions & 0 deletions support/Pipelines/Bbh/InitialData.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
# See LICENSE.txt for details.

Executable: SolveXcts
ExpectedOutput:
- BbhReductions.h5
- BbhVolume*.h5
{% if evolve %}
Next:
Run: spectre.Pipelines.Bbh.Inspiral:start_inspiral
Expand Down
7 changes: 1 addition & 6 deletions tests/InputFiles/Poisson/ProductOfSinusoids1D.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ Testing:
ExpectedOutput:
- PoissonProductOfSinusoids1DReductions.h5
- PoissonProductOfSinusoids1DVolume0.h5
- SubdomainMatrix_[B0,(L1I0)].txt
- SubdomainMatrix_[B0,(L1I1)].txt
- SubdomainMatrix_[B0,(L2I0)].txt
- SubdomainMatrix_[B0,(L2I1)].txt
- SubdomainMatrix_[B0,(L2I2)].txt
- SubdomainMatrix_[B0,(L2I3)].txt
- SubdomainMatrix*.txt
OutputFileChecks:
- Label: Discretization error
Subfile: ErrorNorms.dat
Expand Down
4 changes: 2 additions & 2 deletions tests/tools/Test_CleanOutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def setUp(self):
os.makedirs(self.test_dir, exist_ok=True)
with open(self.input_file_path, "w") as open_file:
yaml.safe_dump_all(
[{"ExpectedOutput": ["Reduction.h5", "Volume0.h5"]}, {}],
[{"ExpectedOutput": ["Reduction.h5", "Volume*.h5"]}, {}],
open_file,
)

Expand All @@ -48,7 +48,7 @@ def test_clean_output(self):
)
with open(self.reduction_file_path, "w"):
pass
with self.assertRaisesRegex(MissingExpectedOutputError, "Volume0.h5"):
with self.assertRaisesRegex(MissingExpectedOutputError, "Volume\*.h5"):
clean_output(
input_file=self.input_file_path,
output_dir=self.test_dir,
Expand Down
23 changes: 14 additions & 9 deletions tools/CleanOutput.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

import glob
import logging
import os
import shutil
Expand All @@ -24,13 +25,14 @@ def clean_output(input_file, output_dir, force):
Deletes output files specified in the `input_file` from the `output_dir`,
raising an error if the expected output files were not found.
The `input_file` must list its expected output files in the metadata:
The `input_file` must list its expected output files in the metadata.
They may contain glob patterns:
\b
```yaml
ExpectedOutput:
- Reduction.h5
- Volume0.h5
- Volume*.h5
```
"""
with open(input_file, "r") as open_input_file:
Expand All @@ -52,14 +54,17 @@ def clean_output(input_file, output_dir, force):

missing_files = []
for expected_output_file in expected_output:
expected_output_file = os.path.join(output_dir, expected_output_file)
found_output_files = glob.glob(
os.path.join(output_dir, expected_output_file)
)
logging.debug(f"Attempting to remove file {expected_output_file}...")
if os.path.exists(expected_output_file):
if os.path.isfile(expected_output_file):
os.remove(expected_output_file)
else:
shutil.rmtree(expected_output_file)
logging.info(f"Removed file {expected_output_file}.")
if len(found_output_files) > 0:
for expected_output_file in found_output_files:
if os.path.isfile(expected_output_file):
os.remove(expected_output_file)
else:
shutil.rmtree(expected_output_file)
logging.info(f"Removed file {expected_output_file}.")
elif not force:
missing_files.append(expected_output_file)
logging.error(
Expand Down

0 comments on commit d51f2c8

Please sign in to comment.