Skip to content

Commit b7414a8

Browse files
committed
Read the cache data file using Rust
1 parent 55f8631 commit b7414a8

2 files changed

Lines changed: 9 additions & 58 deletions

File tree

.importlinter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ ignore_imports =
1717
; Grimp doesn't understand extension modules: it thinks grimp._rustgrimp
1818
; is an object within grimp, rather than a module in its own right,
1919
; so we ignore these imports here.
20+
grimp.adaptors.caching -> grimp
2021
grimp.adaptors.graph -> grimp
2122
grimp.adaptors.filesystem -> grimp
2223
grimp.application.scanning -> grimp

src/grimp/adaptors/caching.py

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import hashlib
2+
23
import json
34
import logging
45
from typing import Dict, List, Optional, Set, Tuple, Type
@@ -9,6 +10,7 @@
910

1011
from ..application.ports.caching import Cache as AbstractCache
1112
from ..application.ports.caching import CacheMiss
13+
from grimp import _rustgrimp as rust # type: ignore[attr-defined]
1214

1315
logger = logging.getLogger(__name__)
1416
PrimitiveFormat = Dict[str, List[Tuple[str, Optional[int], str]]]
@@ -199,74 +201,22 @@ def _read_data_map_file(self) -> Dict[Module, Set[DirectImport]]:
199201
),
200202
)
201203
try:
202-
serialized = self.file_system.read(data_cache_filename)
204+
imports_by_module = rust.read_cache_data_map_file(
205+
data_cache_filename, self.file_system.convert_to_basic()
206+
)
203207
except FileNotFoundError:
204208
logger.info(f"No cache file: {data_cache_filename}.")
205209
return {}
206-
207-
# Deserialize to primitives.
208-
try:
209-
deserialized_json = json.loads(serialized)
210-
logger.info(f"Used cache data file {data_cache_filename}.")
211-
except json.JSONDecodeError:
210+
except rust.CorruptCache:
212211
logger.warning(f"Could not use corrupt cache file {data_cache_filename}.")
213212
return {}
214213

215-
primitives_map: PrimitiveFormat = self._to_primitives_data_map(deserialized_json)
216-
217-
return {
218-
Module(name=name): {
219-
DirectImport(
220-
importer=Module(name),
221-
imported=Module(import_data[0]),
222-
line_number=int(import_data[1]), # type: ignore
223-
line_contents=import_data[2],
224-
)
225-
for import_data in imports_data
226-
}
227-
for name, imports_data in primitives_map.items()
228-
}
214+
logger.info(f"Used cache data file {data_cache_filename}.")
215+
return imports_by_module
229216

230217
def _build_data_cache_filename(self, found_package: FoundPackage) -> str:
231218
return self.file_system.join(self.cache_dir, f"{found_package.name}.data.json")
232219

233-
def _to_primitives_data_map(self, deserialized_json: object) -> PrimitiveFormat:
234-
"""
235-
Convert the deserialized json from a data file to a narrower schema.
236-
237-
Anything that doesn't fit the schema will be removed.
238-
"""
239-
if not isinstance(deserialized_json, dict):
240-
return {}
241-
242-
primitives_map: PrimitiveFormat = {}
243-
244-
for key, value in deserialized_json.items():
245-
if not isinstance(key, str):
246-
continue
247-
if not isinstance(value, list):
248-
continue
249-
primitive_imports = []
250-
for deserialized_import in value:
251-
try:
252-
[imported, line_number, line_contents] = deserialized_import
253-
except ValueError:
254-
continue
255-
try:
256-
primitive_imports.append(
257-
(
258-
str(imported),
259-
int(line_number) if line_number else None,
260-
str(line_contents),
261-
)
262-
)
263-
except TypeError:
264-
continue
265-
266-
primitives_map[key] = primitive_imports
267-
268-
return primitives_map
269-
270220
def _write_marker_files_if_not_already_there(self) -> None:
271221
marker_files_info = (
272222
(".gitignore", "# Automatically created by Grimp.\n*"),

0 commit comments

Comments
 (0)