From 06da113bcea86e356a323baf1994d43e4899565e Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Tue, 26 Jul 2022 17:27:31 +0200 Subject: [PATCH 1/4] MNT make init atomic --- skops/hub_utils/_hf_hub.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/skops/hub_utils/_hf_hub.py b/skops/hub_utils/_hf_hub.py index 1e214f36..a226df95 100644 --- a/skops/hub_utils/_hf_hub.py +++ b/skops/hub_utils/_hf_hub.py @@ -116,10 +116,14 @@ def init(*, model: Union[str, Path], requirements: List[str], dst: Union[str, Pa raise OSError("None-empty dst path already exists!") dst.mkdir(parents=True, exist_ok=True) - shutil.copy2(src=model, dst=dst) - - model_name = Path(model).name - _create_config(model_path=model_name, requirements=requirements, dst=dst) + try: + shutil.copy2(src=model, dst=dst) + + model_name = Path(model).name + _create_config(model_path=model_name, requirements=requirements, dst=dst) + except Exception: + shutil.rmtree(dst) + raise def update_env(*, path: Union[str, Path], requirements: List[str] = None): From 6e6c7e04c6b352c9162c6a8beba55e6fdddd2577 Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Fri, 12 Aug 2022 14:52:15 +0200 Subject: [PATCH 2/4] add test --- skops/hub_utils/tests/test_hf_hub.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/skops/hub_utils/tests/test_hf_hub.py b/skops/hub_utils/tests/test_hf_hub.py index d7736752..c4fa99bc 100644 --- a/skops/hub_utils/tests/test_hf_hub.py +++ b/skops/hub_utils/tests/test_hf_hub.py @@ -183,6 +183,28 @@ def test_create_config_invalid_text_data(temp_path): ) +def test_atomic_init(classifier_pickle, temp_path): + with pytest.raises(ValueError): + # this fails since we're passing an invalid task. + init( + model=classifier_pickle, + requirements=["scikit-learn"], + dst=temp_path, + task="invalid", + data=iris.data, + ) + + # this passes even though the above init has failed once, on the same + # destination path. + init( + model=classifier_pickle, + requirements=["scikit-learn"], + dst=temp_path, + task="tabular-classification", + data=iris.data, + ) + + def test_init_invalid_task(classifier_pickle, temp_path): with pytest.raises( ValueError, match="Task invalid not supported. Supported tasks are" From 593f605e3d364a77867b799f46b816e488b4e45d Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Fri, 12 Aug 2022 14:58:51 +0200 Subject: [PATCH 3/4] add changelog --- docs/_authors.rst | 10 ++++++++++ docs/changes.rst | 7 ++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 docs/_authors.rst diff --git a/docs/_authors.rst b/docs/_authors.rst new file mode 100644 index 00000000..28768c7c --- /dev/null +++ b/docs/_authors.rst @@ -0,0 +1,10 @@ + +.. role:: raw-html(raw) + :format: html + + +.. _Adrin Jalali: https://github.com/adrinjalali + +.. _Benjamin Bossan: https://github.com/BenjaminBossan + +.. _Merve Noyan: https://github.com/merveenoyan diff --git a/docs/changes.rst b/docs/changes.rst index 105d52b9..51b17e55 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -1,3 +1,5 @@ +.. include:: _authors.rst + .. _changelog: skops Changelog @@ -11,7 +13,10 @@ skops Changelog v0.2 ---- - Tables, e.g. cross-validation results, can now be added to model cards using - the :meth:`.Card.add_table` method. :pr:`90` by :user:`Benjamin Bossan ` + the :meth:`.Card.add_table` method. :pr:`90` by `Benjamin Bossan`_. + +- Make :meth:`skops.hub_utils.init` atomic. Now it doesn't leave a trace on the + filesystem if it fails for some reason. :pr:`60` by `Adrin Jalali`_` v0.1 ---- From 2b1da536fb82e2f91124f257d78d47c798184210 Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Tue, 16 Aug 2022 10:11:24 +0200 Subject: [PATCH 4/4] fix test --- skops/hub_utils/tests/test_hf_hub.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/skops/hub_utils/tests/test_hf_hub.py b/skops/hub_utils/tests/test_hf_hub.py index c4fa99bc..cf4138f8 100644 --- a/skops/hub_utils/tests/test_hf_hub.py +++ b/skops/hub_utils/tests/test_hf_hub.py @@ -190,8 +190,8 @@ def test_atomic_init(classifier_pickle, temp_path): model=classifier_pickle, requirements=["scikit-learn"], dst=temp_path, - task="invalid", - data=iris.data, + task="tabular-classification", + data="invalid", ) # this passes even though the above init has failed once, on the same