|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import contextlib |
3 | 4 | import functools |
4 | 5 | import inspect |
5 | 6 | import logging |
@@ -156,9 +157,8 @@ def pin_meta(self, name, version: str = None) -> Meta: |
156 | 157 | meta_name = self.meta_factory.get_meta_name(*components) |
157 | 158 |
|
158 | 159 | path_meta = self.construct_path([*components, meta_name]) |
159 | | - f, local = self._open_pin_meta(path_meta) |
160 | | - |
161 | | - meta = self.meta_factory.read_pin_yaml(f, pin_name, selected_version, local=local) |
| 160 | + with self._open_pin_meta(path_meta) as (f, local): |
| 161 | + meta = self.meta_factory.read_pin_yaml(f, pin_name, selected_version, local=local) |
162 | 162 |
|
163 | 163 | return meta |
164 | 164 |
|
@@ -788,14 +788,18 @@ def _load_data(self, meta, pin_version_path): |
788 | 788 |
|
789 | 789 | # filesystem and cache methods -------------------------------------------- |
790 | 790 |
|
| 791 | + @contextlib.contextmanager |
791 | 792 | def _open_pin_meta(self, path): |
792 | 793 | f = self.fs.open(path) |
793 | | - self._touch_cache(path) |
| 794 | + try: |
| 795 | + self._touch_cache(path) |
794 | 796 |
|
795 | | - # optional additional data to put in Meta.local |
796 | | - local = {} |
| 797 | + # optional additional data to put in Meta.local |
| 798 | + local = {} |
797 | 799 |
|
798 | | - return f, local |
| 800 | + yield f, local |
| 801 | + finally: |
| 802 | + self.fs.close(f) |
799 | 803 |
|
800 | 804 | def _get_cache_path(self, pin_name, version=None, fname=None): |
801 | 805 | version_part = [version] if version is not None else [] |
@@ -934,8 +938,8 @@ def pin_meta(self, name, version=None): |
934 | 938 | # note that pins on this board should point to versions, so we use an |
935 | 939 | # empty string to mark version (it ultimately is ignored) |
936 | 940 | path_meta = self.construct_path([pin_name, "", meta_name]) |
937 | | - f, local = self._open_pin_meta(path_meta) |
938 | | - meta = self.meta_factory.read_pin_yaml(f, pin_name, VersionRaw(""), local=local) |
| 941 | + with self._open_pin_meta(path_meta) as (f, local): |
| 942 | + meta = self.meta_factory.read_pin_yaml(f, pin_name, VersionRaw(""), local=local) |
939 | 943 |
|
940 | 944 | # TODO(#59,#83): handle caching, and then re-enable pin_read. |
941 | 945 | # self._touch_cache(path_meta) |
@@ -1143,22 +1147,26 @@ def pin_versions_prune(self, *args, **kwargs): |
1143 | 1147 | ) |
1144 | 1148 | super().pin_versions_prune(*args, **kwargs) |
1145 | 1149 |
|
| 1150 | + @contextlib.contextmanager |
1146 | 1151 | def _open_pin_meta(self, path): |
1147 | 1152 | f = self.fs.open(path) |
1148 | | - self._touch_cache(path) |
1149 | | - |
1150 | | - # optional additional data to put in Meta.local |
1151 | | - user_name, content_name, bundle_id = str(path).split("/")[:3] |
1152 | | - user_guid = self.fs._user_name_cache[user_name] |
1153 | | - content_guid = self.fs._content_name_cache[(user_guid, content_name)] |
1154 | | - |
1155 | | - local = { |
1156 | | - "content_id": content_guid, |
1157 | | - "version": bundle_id, |
1158 | | - "url": f"{self.fs.api.server_url}/content/{content_guid}/", |
1159 | | - } |
1160 | | - |
1161 | | - return f, local |
| 1153 | + try: |
| 1154 | + self._touch_cache(path) |
| 1155 | + |
| 1156 | + # optional additional data to put in Meta.local |
| 1157 | + user_name, content_name, bundle_id = str(path).split("/")[:3] |
| 1158 | + user_guid = self.fs._user_name_cache[user_name] |
| 1159 | + content_guid = self.fs._content_name_cache[(user_guid, content_name)] |
| 1160 | + |
| 1161 | + local = { |
| 1162 | + "content_id": content_guid, |
| 1163 | + "version": bundle_id, |
| 1164 | + "url": f"{self.fs.api.server_url}/content/{content_guid}/", |
| 1165 | + } |
| 1166 | + |
| 1167 | + yield f, local |
| 1168 | + finally: |
| 1169 | + self.fs.close(f) |
1162 | 1170 |
|
1163 | 1171 | def validate_pin_name(self, name) -> None: |
1164 | 1172 | # this should be the default behavior, expecting a full pin name. |
|
0 commit comments