Skip to content

Commit

Permalink
Purge the remove_if_temporary_notebook function
Browse files Browse the repository at this point in the history
Not needed if rewrite_notebook always produces temporary files.
Just use os.remove to clean up after rewrite_notebook.
  • Loading branch information
pavoljuhas committed Mar 14, 2023
1 parent 92477a7 commit 0d5895f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 44 deletions.
7 changes: 1 addition & 6 deletions dev_tools/notebooks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from dev_tools.notebooks.utils import (
filter_notebooks,
list_all_notebooks,
remove_if_temporary_notebook,
rewrite_notebook,
)
from dev_tools.notebooks.utils import filter_notebooks, list_all_notebooks, rewrite_notebook
9 changes: 2 additions & 7 deletions dev_tools/notebooks/isolated_notebook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@
import pytest

from dev_tools import shell_tools
from dev_tools.notebooks import (
list_all_notebooks,
filter_notebooks,
rewrite_notebook,
remove_if_temporary_notebook,
)
from dev_tools.notebooks import list_all_notebooks, filter_notebooks, rewrite_notebook

# these notebooks rely on features that are not released yet
# after every release we should raise a PR and empty out this list
Expand Down Expand Up @@ -213,7 +208,7 @@ def test_notebooks_against_released_cirq(partition, notebook_path, cloned_env):
f"instead of `pip install cirq` to this notebook, and exclude it from "
f"dev_tools/notebooks/isolated_notebook_test.py."
)
remove_if_temporary_notebook(rewritten_notebook_path)
os.remove(rewritten_notebook_path)


@pytest.mark.parametrize("notebook_path", NOTEBOOKS_DEPENDING_ON_UNRELEASED_FEATURES)
Expand Down
9 changes: 2 additions & 7 deletions dev_tools/notebooks/notebook_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@
import pytest

from dev_tools import shell_tools
from dev_tools.notebooks import (
filter_notebooks,
list_all_notebooks,
rewrite_notebook,
remove_if_temporary_notebook,
)
from dev_tools.notebooks import filter_notebooks, list_all_notebooks, rewrite_notebook

SKIP_NOTEBOOKS = [
# skipping vendor notebooks as we don't have auth sorted out
Expand Down Expand Up @@ -88,4 +83,4 @@ def test_notebooks_against_released_cirq(notebook_path):
f"notebook (in Github Actions, you can download it from the workflow artifact"
f" 'notebook-outputs')"
)
remove_if_temporary_notebook(rewritten_notebook_path)
os.remove(rewritten_notebook_path)
16 changes: 0 additions & 16 deletions dev_tools/notebooks/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,3 @@ def rewrite_notebook(notebook_path):
new_file.writelines(lines)

return new_file.name


def remove_if_temporary_notebook(notebook_path: str):
"""Delete temporary notebook if written by `rewrite_notebook`.
Do nothing if the specified notebook is not in the temporary directory
or if its filename does not end in '-rewrite.ipynb'
Use this to safely clean up notebooks created by `rewrite_notebook`.
"""
if (
notebook_path.endswith('-rewrite.ipynb')
and os.path.isfile(notebook_path)
and os.path.dirname(notebook_path) == tempfile.gettempdir()
):
os.remove(notebook_path)
12 changes: 4 additions & 8 deletions dev_tools/notebooks/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ def test_rewrite_notebook():
rewritten = f.read()
assert rewritten == 'd = 3\nd = 4'

dt.remove_if_temporary_notebook(path)
assert not os.path.exists(path)
os.remove(path)
shutil.rmtree(directory)


Expand All @@ -59,8 +58,7 @@ def test_rewrite_notebook_multiple():
rewritten = f.read()
assert rewritten == 'd = 3\nd = 1'

dt.remove_if_temporary_notebook(path)
assert not os.path.exists(path)
os.remove(path)
shutil.rmtree(directory)


Expand All @@ -73,8 +71,7 @@ def test_rewrite_notebook_ignore_non_seperator_lines():
rewritten = f.read()
assert rewritten == 'd = 3\nd = 4'

dt.remove_if_temporary_notebook(path)
assert not os.path.exists(path)
os.remove(path)
shutil.rmtree(directory)


Expand All @@ -88,8 +85,7 @@ def test_rewrite_notebook_no_tst_file():
assert path != ipynb_path
assert filecmp.cmp(path, ipynb_path)

dt.remove_if_temporary_notebook(path)

os.remove(path)
shutil.rmtree(directory)


Expand Down

0 comments on commit 0d5895f

Please sign in to comment.