forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_utils.py
More file actions
90 lines (72 loc) · 2.87 KB
/
cli_utils.py
File metadata and controls
90 lines (72 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import random
import string
import subprocess
import sys
import tempfile
from contextlib import contextmanager
from pathlib import Path
from textwrap import dedent
from typing import List, Tuple
from feast import cli
from feast.feature_store import FeatureStore
def get_example_repo(example_repo_py) -> str:
parent = Path(__file__).parent
traversal_limit = 5
while traversal_limit > 0 and parent.parts[-1] != "tests":
traversal_limit -= 1
parent = parent.parent
if parent.parts[-1] != "tests":
raise ValueError(f"Unable to find where repo {example_repo_py} is located")
return (parent / "example_repos" / example_repo_py).read_text()
class CliRunner:
"""
NB. We can't use test runner helper from click here, since it doesn't start a new Python
interpreter. And we need a new interpreter for each test since we dynamically import
modules from the feature repo, and it is hard to clean up that state otherwise.
"""
def run(self, args: List[str], cwd: Path) -> subprocess.CompletedProcess:
return subprocess.run([sys.executable, cli.__file__] + args, cwd=cwd)
def run_with_output(self, args: List[str], cwd: Path) -> Tuple[int, bytes]:
try:
return (
0,
subprocess.check_output(
[sys.executable, cli.__file__] + args,
cwd=cwd,
stderr=subprocess.STDOUT,
),
)
except subprocess.CalledProcessError as e:
return e.returncode, e.output
@contextmanager
def local_repo(self, example_repo_py: str, offline_store: str):
"""
Convenience method to set up all the boilerplate for a local feature repo.
"""
project_id = "test" + "".join(
random.choice(string.ascii_lowercase + string.digits) for _ in range(10)
)
with tempfile.TemporaryDirectory() as repo_dir_name, tempfile.TemporaryDirectory() as data_dir_name:
repo_path = Path(repo_dir_name)
data_path = Path(data_dir_name)
repo_config = repo_path / "feature_store.yaml"
repo_config.write_text(
dedent(
f"""
project: {project_id}
registry: {data_path / "registry.db"}
provider: local
online_store:
path: {data_path / "online_store.db"}
offline_store:
type: {offline_store}
"""
)
)
repo_example = repo_path / "example.py"
repo_example.write_text(example_repo_py)
result = self.run(["apply"], cwd=repo_path)
assert result.returncode == 0
yield FeatureStore(repo_path=str(repo_path), config=None)
result = self.run(["teardown"], cwd=repo_path)
assert result.returncode == 0