Skip to content

Commit c843602

Browse files
authored
Merge pull request #50 from aclark4life/main
Misc updates
2 parents 4298881 + e8ddcb3 commit c843602

File tree

5 files changed

+140
-14
lines changed

5 files changed

+140
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ mongocryptd.pid
77
.python-version
88
build/
99
uv.lock
10+
secrets-export.sh

django_mongodb_cli/repo.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import typer
22
import os
3+
import shlex
34

45
from .utils import Package, Repo, Test
56

@@ -106,9 +107,7 @@ def cd(
106107
ctx: typer.Context,
107108
repo_name: str = typer.Argument(None),
108109
):
109-
"""
110-
Change directory to the specified repository.
111-
"""
110+
"""Change directory to the specified repository."""
112111
repo = Repo()
113112
repo.ctx = ctx
114113

@@ -122,6 +121,52 @@ def cd(
122121
)
123122

124123

124+
@repo.command()
125+
def run(
126+
ctx: typer.Context,
127+
repo_name: str = typer.Argument(..., help="Repository name"),
128+
command: list[str] = typer.Argument(
129+
..., metavar="CMD...", help="Command (and args) to run in the repo directory"
130+
),
131+
):
132+
"""Run an arbitrary command inside the repository directory.
133+
134+
Examples:
135+
dm repo run mongo-python-driver just setup tests encryption
136+
dm repo run mongo-python-driver "just setup tests encryption"
137+
138+
Environment variables can be configured per-repo under
139+
``[tool.django-mongodb-cli.run.<repo_name>.env_vars]`` in ``pyproject.toml``.
140+
"""
141+
repo = Repo()
142+
repo.ctx = ctx
143+
144+
# Allow a single shell-style string (e.g. "just setup tests encryption").
145+
if len(command) == 1:
146+
command = shlex.split(command[0])
147+
148+
path, _ = repo.ensure_repo(repo_name)
149+
if not path:
150+
return
151+
152+
# Build environment from current process plus any configured env_vars.
153+
env = os.environ.copy()
154+
run_cfg = repo.run_cfg(repo_name)
155+
env_vars_list = run_cfg.get("env_vars")
156+
if env_vars_list:
157+
repo.info("Setting environment variables from pyproject.toml:")
158+
for item in env_vars_list:
159+
name = item.get("name")
160+
value = str(item.get("value"))
161+
if name is None:
162+
continue
163+
env[name] = value
164+
typer.echo(f" {name}={value}")
165+
166+
repo.info(f"Running in {path}: {' '.join(command)}")
167+
repo.run(command, cwd=path, env=env)
168+
169+
125170
@repo.command()
126171
def checkout(
127172
ctx: typer.Context,
@@ -448,6 +493,18 @@ def reset_repo(name):
448493
)
449494

450495

496+
@repo.command()
497+
def show(
498+
ctx: typer.Context,
499+
repo_name: str = typer.Argument(..., help="Repository name"),
500+
commit_hash: str = typer.Argument(..., help="Commit hash to show"),
501+
):
502+
"""Show the git diff for a specific commit hash in the given repository."""
503+
repo = Repo()
504+
repo.ctx = ctx
505+
repo.show_commit(repo_name, commit_hash)
506+
507+
451508
@repo.command()
452509
def set_default(
453510
repo_name: str = typer.Argument(None),

django_mongodb_cli/utils.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,16 @@ def run(
5555
args,
5656
cwd: Path | str | None = None,
5757
check: bool = True,
58-
env: str | None = None,
58+
env: dict[str, str] | None = None,
5959
) -> bool:
60+
"""Run a subprocess with optional working directory and environment.
61+
62+
Args:
63+
args: Command and arguments to run.
64+
cwd: Optional working directory.
65+
check: Whether to raise on non-zero exit code.
66+
env: Optional environment mapping to pass to the subprocess.
67+
"""
6068
try:
6169
subprocess.run(args, cwd=str(cwd) if cwd else None, check=check, env=env)
6270
return True
@@ -82,6 +90,14 @@ def tool_cfg(self) -> dict:
8290
def test_cfg(self, repo_name: str) -> dict:
8391
return self.tool_cfg.get("test", {}).get(repo_name, {}) or {}
8492

93+
def run_cfg(self, repo_name: str) -> dict:
94+
"""Return configuration for arbitrary repo commands.
95+
96+
The config is read from [tool.django-mongodb-cli.run.<repo_name>] in
97+
pyproject.toml and can contain keys like ``env_vars``.
98+
"""
99+
return self.tool_cfg.get("run", {}).get(repo_name, {}) or {}
100+
85101
def evergreen_cfg(self, repo_name: str) -> dict:
86102
return self.tool_cfg.get("evergreen", {}).get(repo_name, {}) or {}
87103

@@ -466,6 +482,19 @@ def get_repo_diff(self, repo_name: str) -> None:
466482
except GitCommandError as e:
467483
self.err(f"❌ Failed to diff working tree: {e}")
468484

485+
def show_commit(self, repo_name: str, commit_hash: str) -> None:
486+
"""Show the diff for a specific commit hash in the given repository."""
487+
self.info(f"Showing diff for {repo_name}@{commit_hash}")
488+
path, repo = self.ensure_repo(repo_name)
489+
if not repo or not path:
490+
return
491+
492+
try:
493+
output = repo.git.show(commit_hash)
494+
typer.echo(output)
495+
except GitCommandError as e:
496+
self.err(f"❌ Failed to show commit {commit_hash}: {e}")
497+
469498
def _list_repos(self) -> tuple[set, set]:
470499
map_repos = set(self.map.keys())
471500

pyproject.toml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ value = "/Users/alex.clark/Developer/django-mongodb-cli/src/drivers-evergreen-to
122122

123123
[[tool.django-mongodb-cli.test.mongo-python-driver.env_vars]]
124124
name = "AWS_PROFILE"
125-
value = "drivers-test-secrets-role-857654397073"
125+
value = "my-profile"
126126

127127
[tool.django-mongodb-cli.test.django]
128128
test_command = "./runtests.py"
@@ -142,6 +142,22 @@ test_dirs = [ "src/django/tests", "src/django-mongodb-backend/tests" ]
142142
name = "PYMONGOCRYPT_LIB"
143143
value = "/opt/homebrew/lib/libmongocrypt.dylib"
144144

145+
[[tool.django-mongodb-cli.test.django.env_vars]]
146+
name = "MONGODB_URI"
147+
value = "mongodb://localhost:64437/?directConnection=true"
148+
149+
[[tool.django-mongodb-cli.run.mongo-python-driver.env_vars]]
150+
name = "DRIVERS_TOOLS"
151+
value = "/Users/alex.clark/Developer/django-mongodb-cli/src/drivers-evergreen-tools"
152+
153+
[[tool.django-mongodb-cli.run.mongo-python-driver.env_vars]]
154+
name = "MONGODB_URI"
155+
value = "mongodb://localhost:64437/?directConnection=true"
156+
157+
[[tool.django-mongodb-cli.run.mongo-python-driver.env_vars]]
158+
name = "AWS_PROFILE"
159+
value = "my-profile"
160+
145161
[tool.django-mongodb-cli.test.django.migrations_dir]
146162
source = "mongo_migrations"
147163
target = "src/django/tests/mongo_migrations"

test/settings/qe.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@
66

77
MONGODB_URI = os.environ.get("MONGODB_URI", "mongodb://localhost:27017")
88

9+
# Configure KMS providers.
10+
#
11+
# We prefer AWS when FLE_AWS_KEY/FLE_AWS_SECRET are set, mirroring
12+
# src/mongo-python-driver/test/helpers_shared.py. Otherwise we fall back
13+
# to a local KMS for convenience in local development.
14+
AWS_CREDS = {
15+
"accessKeyId": os.environ.get("FLE_AWS_KEY", ""),
16+
"secretAccessKey": os.environ.get("FLE_AWS_SECRET", ""),
17+
}
18+
_USE_AWS_KMS = any(AWS_CREDS.values())
19+
20+
if _USE_AWS_KMS:
21+
# Use the same demo key ARN and region as the PyMongo QE tests.
22+
_AWS_REGION = os.environ.get("FLE_AWS_KMS_REGION", "us-east-1")
23+
_AWS_KEY_ARN = os.environ.get(
24+
"FLE_AWS_KMS_KEY_ARN",
25+
"arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0",
26+
)
27+
KMS_PROVIDERS = {"aws": AWS_CREDS}
28+
KMS_CREDENTIALS = {"aws": {"key": _AWS_KEY_ARN, "region": _AWS_REGION}}
29+
DEFAULT_KMS_PROVIDER = "aws"
30+
else:
31+
pass
32+
# Local-only fallback: matches the original configuration.
33+
# KMS_PROVIDERS = {"local": {"key": os.urandom(96)}}
34+
# KMS_CREDENTIALS = {"aws": {}}
35+
# DEFAULT_KMS_PROVIDER = "local"
36+
37+
938
DATABASES = {
1039
"default": {
1140
"ENGINE": "django_mongodb_backend",
@@ -24,16 +53,10 @@
2453
"OPTIONS": {
2554
"auto_encryption_opts": AutoEncryptionOpts(
2655
key_vault_namespace="djangotests_encrypted.__keyVault",
27-
kms_providers={
28-
"local": {
29-
"key": os.urandom(96),
30-
},
31-
},
56+
kms_providers=KMS_PROVIDERS,
3257
),
3358
},
34-
"KMS_CREDENTIALS": {
35-
"aws": {},
36-
},
59+
"KMS_CREDENTIALS": KMS_CREDENTIALS,
3760
},
3861
}
3962

@@ -57,7 +80,7 @@ def allow_migrate(self, db, app_label, model_name=None, **hints):
5780
return None
5881

5982
def kms_provider(self, model):
60-
return "local"
83+
return DEFAULT_KMS_PROVIDER
6184

6285

6386
DATABASE_ROUTERS = ["django_mongodb_backend.routers.MongoRouter", EncryptedRouter()]

0 commit comments

Comments
 (0)