|
| 1 | +import contextlib |
1 | 2 | import logging |
2 | 3 | import tempfile |
3 | 4 | import shutil |
@@ -160,13 +161,10 @@ def pin_meta(self, name, version: str = None) -> Meta: |
160 | 161 | meta_name = self.meta_factory.get_meta_name(*components) |
161 | 162 |
|
162 | 163 | path_meta = self.construct_path([*components, meta_name]) |
163 | | - f, local = self._open_pin_meta(path_meta) |
164 | | - |
165 | | - meta = self.meta_factory.read_pin_yaml( |
166 | | - f, pin_name, selected_version, local=local |
167 | | - ) |
168 | | - |
169 | | - return meta |
| 164 | + with self._open_pin_meta(path_meta) as (f, local): |
| 165 | + return self.meta_factory.read_pin_yaml( |
| 166 | + f, pin_name, selected_version, local=local |
| 167 | + ) |
170 | 168 |
|
171 | 169 | def pin_list(self): |
172 | 170 | """List names of all pins in a board. |
@@ -610,14 +608,18 @@ def _load_data(self, meta, pin_version_path): |
610 | 608 |
|
611 | 609 | # filesystem and cache methods -------------------------------------------- |
612 | 610 |
|
| 611 | + @contextlib.contextmanager |
613 | 612 | def _open_pin_meta(self, path): |
614 | 613 | f = self.fs.open(path) |
615 | | - self._touch_cache(path) |
| 614 | + try: |
| 615 | + self._touch_cache(path) |
616 | 616 |
|
617 | | - # optional additional data to put in Meta.local |
618 | | - local = {} |
| 617 | + # optional additional data to put in Meta.local |
| 618 | + local = {} |
619 | 619 |
|
620 | | - return f, local |
| 620 | + yield f, local |
| 621 | + finally: |
| 622 | + self.fs.close(f) |
621 | 623 |
|
622 | 624 | def _get_cache_path(self, pin_name, version=None, fname=None): |
623 | 625 | version_part = [version] if version is not None else [] |
@@ -703,8 +705,8 @@ def pin_meta(self, name, version=None): |
703 | 705 | # note that pins on this board should point to versions, so we use an |
704 | 706 | # empty string to mark version (it ultimately is ignored) |
705 | 707 | path_meta = self.construct_path([pin_name, "", meta_name]) |
706 | | - f, local = self._open_pin_meta(path_meta) |
707 | | - meta = self.meta_factory.read_pin_yaml(f, pin_name, VersionRaw(""), local=local) |
| 708 | + with self._open_pin_meta(path_meta) as (f, local): |
| 709 | + meta = self.meta_factory.read_pin_yaml(f, pin_name, VersionRaw(""), local=local) |
708 | 710 |
|
709 | 711 | # TODO(#59,#83): handle caching, and then re-enable pin_read. |
710 | 712 | # self._touch_cache(path_meta) |
|
0 commit comments