Skip to content

Commit 6aed888

Browse files
authored
Merge pull request #18 from kurtmckee/include-metadata
Support calls to `importlib.metadata.version()`
2 parents 2d36136 + 4ac63ee commit 6aed888

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Added
2+
-----
3+
4+
* Support calls to ``importlib.metadata.version()``.
5+
6+
Changed
7+
-------
8+
9+
* Bundle ``.dist-info/`` metadata directories into databases.

src/sqliteimport/accessor.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,15 @@ def add_file(self, directory: pathlib.Path, file: pathlib.Path) -> None:
6363
(directory / file).read_text(),
6464
),
6565
)
66+
67+
def get_file(self, path_like: str) -> str:
68+
source: str = self.connection.execute(
69+
"""
70+
SELECT
71+
source
72+
FROM code
73+
WHERE path LIKE ?;
74+
""",
75+
(path_like,),
76+
).fetchone()[0]
77+
return source

src/sqliteimport/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def bundle(directory: pathlib.Path, database: pathlib.Path) -> None:
4747
files = []
4848
for path in paths:
4949
rel_path = path.relative_to(directory)
50-
if rel_path.suffix in (".dist-info", ".so"):
50+
if rel_path.suffix in {".so"}:
5151
continue
5252
if rel_path.name == "__pycache__":
5353
continue

src/sqliteimport/importer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66

77
import importlib.abc
88
import importlib.machinery
9+
import importlib.metadata
910
import os.path
1011
import pathlib
1112
import sqlite3
1213
import sys
1314
import types
1415
import typing
1516

17+
from sqliteimport.accessor import Accessor
18+
1619

1720
class SqliteFinder(importlib.abc.MetaPathFinder):
1821
def __init__(self, database: pathlib.Path | sqlite3.Connection) -> None:
@@ -23,6 +26,7 @@ def __init__(self, database: pathlib.Path | sqlite3.Connection) -> None:
2326
_, _, path = database.execute("PRAGMA database_list;").fetchone()
2427
self.database = pathlib.Path(path)
2528
self.connection = database
29+
self.accessor = Accessor(self.connection)
2630

2731
def find_spec(
2832
self,
@@ -44,6 +48,15 @@ def find_spec(
4448
)
4549
return spec
4650

51+
def find_distributions(
52+
self,
53+
context: importlib.metadata.DistributionFinder.Context | None = None,
54+
) -> typing.Generator[SqliteDistribution]:
55+
if context is None:
56+
context = importlib.metadata.DistributionFinder.Context()
57+
if context.name is not None:
58+
yield SqliteDistribution(context.name, self.connection)
59+
4760

4861
class SqliteLoader(importlib.abc.Loader):
4962
def __init__(self, source: str) -> None:
@@ -62,3 +75,16 @@ def load(database: pathlib.Path | str | sqlite3.Connection) -> None:
6275
raise FileNotFoundError(f"{database} must exist.")
6376
database = pathlib.Path(database)
6477
sys.meta_path.append(SqliteFinder(database))
78+
79+
80+
class SqliteDistribution(importlib.metadata.Distribution):
81+
def __init__(self, name: str, connection: sqlite3.Connection):
82+
self.__name = name
83+
self.__connection = connection
84+
self.__accessor = Accessor(connection)
85+
86+
def locate_file(self, path: typing.Any) -> pathlib.Path:
87+
raise NotImplementedError()
88+
89+
def read_text(self, filename: str) -> str:
90+
return self.__accessor.get_file(f"{self.__name}-%/{filename}")

0 commit comments

Comments
 (0)