Skip to content

Commit 657b1dd

Browse files
ejguanfacebook-github-bot
authored andcommitted
Make portalocker optional dependency (#1007)
Summary: Fixes #940 #978 ## Changes: - Make portalocker optional - Add `workflow_dispatch` to `pull_release.yml` to enable manual triggering on nightly release testing. - Remove unused `test/requirements.yml` Pull Request resolved: #1007 Reviewed By: wenleix, NivekT Differential Revision: D43238384 Pulled By: ejguan fbshipit-source-id: b23bdd558bb2d97ba90c3297ac7d93ea8adafe5f
1 parent ee5b18a commit 657b1dd

File tree

9 files changed

+47
-19
lines changed

9 files changed

+47
-19
lines changed

.github/workflows/pull_release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
name: Test Release Pipelines
22

33
on:
4+
workflow_dispatch:
45
pull_request:
56
paths:
67
- .github/workflows/pull_release.yml

mypy.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ ignore_missing_imports = True
5757
[mypy-pandas.*]
5858
ignore_missing_imports = True
5959

60+
[mypy-portalocker.*]
61+
ignore_missing_imports = True
62+
6063
[mypy-psutil.*]
6164
ignore_missing_imports = True
6265

packaging/torchdata/meta.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ requirements:
2424
- python
2525
- urllib3>=1.25
2626
- requests
27-
- portalocker>=2.0.0
2827
{{ environ.get('CONDA_PYTORCH_CONSTRAINT') }}
2928

3029
build:

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
urllib3 >= 1.25
22
requests
3-
portalocker >= 2.0.0

test/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ s3fs
55
iopath == 0.1.9
66
numpy
77
rarfile
8+
portalocker >= 2.0.0
89
# Protobuf 4.0 is binary incompatible with what C++ TF uses.
910
# See: https://github.com/tensorflow/tensorflow/blob/8dcaf6b98a6a49c85eb470140ba8506e71a3b5af/tensorflow/tools/pip_package/setup.py#L88-L94
1011
# Protobuf 3.20.2 is also broken on MacOS Python 3.10

test/requirements.yml

Lines changed: 0 additions & 9 deletions
This file was deleted.

test/test_local_io.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@
7474
HAS_RAR_TOOLS = False
7575
skipIfNoRarTools = unittest.skipIf(not HAS_RAR_TOOLS, "no rar tools")
7676

77+
try:
78+
import portalocker
79+
80+
HAS_PORTALOCKER = True
81+
except ImportError:
82+
HAS_PORTALOCKER = False
83+
skipIfNoPortalocker = unittest.skipIf(not HAS_PORTALOCKER, "No portalocker installed")
84+
7785

7886
def filepath_fn(temp_dir_name, name: str) -> str:
7987
return os.path.join(temp_dir_name, os.path.basename(name))
@@ -645,6 +653,7 @@ def _slow_fn(tmpdirname, x):
645653
time.sleep(10)
646654
return (x, "str")
647655

656+
@skipIfNoPortalocker
648657
def test_disk_cache_locks(self):
649658
with tempfile.TemporaryDirectory() as tmpdirname:
650659
file_name = os.path.join(tmpdirname, "test.bin")

test/test_remote_io.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@
6060
skipIfAWS = unittest.skipIf(HAS_AWS, "AWSSDK Enabled")
6161
skipIfNoAWS = unittest.skipIf(not HAS_AWS, "No AWSSDK Enabled")
6262

63+
try:
64+
import portalocker
65+
66+
HAS_PORTALOCKER = True
67+
except ImportError:
68+
HAS_PORTALOCKER = False
69+
skipIfNoPortalocker = unittest.skipIf(not HAS_PORTALOCKER, "No portalocker installed")
70+
6371

6472
class TestDataPipeRemoteIO(expecttest.TestCase):
6573
def setUp(self):
@@ -125,6 +133,7 @@ def test_http_reader_iterdatapipe(self):
125133
allow_redirects=query_params["allow_redirects"],
126134
)
127135

136+
@skipIfNoPortalocker
128137
def test_on_disk_cache_holder_iterdatapipe(self):
129138
tar_file_url = "https://raw.githubusercontent.com/pytorch/data/main/test/_fakedata/csv.tar.gz"
130139
expected_file_name = os.path.join(self.temp_dir.name, "csv.tar.gz")

torchdata/datapipes/iter/util/cacheholder.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,8 @@
1818

1919
try:
2020
import portalocker
21-
except ImportError as e:
22-
if os.name == "nt" and str(e).startswith("DLL load failed while importing"):
23-
print(
24-
"Please take a look at FAQ in https://github.com/pytorch/data#frequently-asked-questions-faq"
25-
"for the solution of this Error."
26-
)
27-
raise
28-
21+
except ImportError:
22+
protalocker = None
2923

3024
from torch.utils.data.datapipes.utils.common import _check_unpickable_fn, DILL_AVAILABLE
3125

@@ -38,6 +32,26 @@
3832

3933
dill.extend(use_dill=False)
4034

35+
36+
def _assert_portalocker() -> None:
37+
try:
38+
import portalocker # noqa: F401
39+
except ImportError as e:
40+
if os.name == "nt" and str(e).startswith("DLL load failed while importing"):
41+
print(
42+
"Please take a look at FAQ in https://github.com/pytorch/data#frequently-asked-questions-faq"
43+
"for the solution of this Error."
44+
)
45+
raise
46+
else:
47+
raise ModuleNotFoundError(
48+
"Package `portalocker` is required to be installed to use this datapipe."
49+
"Please use `pip install 'portalocker>=2.0.0'` or"
50+
"`conda install -c conda-forge 'portalocker>=2/0.0'`"
51+
"to install the package"
52+
)
53+
54+
4155
T_co = TypeVar("T_co", covariant=True)
4256

4357
PROMISE_FILE_DELETE_TIMEOUT = 30
@@ -190,6 +204,8 @@ def __init__(
190204
hash_type: str = "sha256",
191205
extra_check_fn: Optional[Callable[[str], bool]] = None,
192206
):
207+
_assert_portalocker()
208+
193209
self.source_datapipe = source_datapipe
194210

195211
if filepath_fn is not None:

0 commit comments

Comments
 (0)