python-ship 0.2.0__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. python_ship/__init__.py +1 -1
  2. python_ship/adapters.py +28 -3
  3. python_ship/capabilities.py +26 -1
  4. python_ship/cli.py +311 -95
  5. python_ship/compose.py +40 -14
  6. python_ship/config.py +26 -11
  7. python_ship/contracts.py +13 -1
  8. python_ship/environment.py +252 -38
  9. python_ship/image_build.py +143 -0
  10. python_ship/init.py +298 -76
  11. python_ship/layout.py +50 -0
  12. python_ship/mutagen.py +1 -1
  13. python_ship/package_managers.py +1 -1
  14. python_ship/plugins.py +22 -2
  15. python_ship/portable.py +7 -0
  16. python_ship/ports.py +120 -0
  17. python_ship/production.py +2 -1
  18. python_ship/release.py +4 -2
  19. python_ship/runtime.py +9 -7
  20. python_ship/schema/service-environment.json +2 -0
  21. python_ship/schema/ship.schema.json +28 -0
  22. python_ship/security.py +2 -1
  23. python_ship/service_setup.py +81 -0
  24. python_ship/services.py +104 -5
  25. python_ship/storage.py +9 -3
  26. python_ship/stubs.py +23 -21
  27. python_ship/templates/Dockerfile +12 -2
  28. python_ship/templates/check-python-security-floor.py +1 -1
  29. python_ship/templates/launcher +49 -0
  30. python_ship/terminal.py +75 -0
  31. python_ship/wizard.py +52 -6
  32. {python_ship-0.2.0.dist-info → python_ship-0.4.0.dist-info}/METADATA +382 -181
  33. python_ship-0.4.0.dist-info/RECORD +47 -0
  34. python_ship-0.2.0.dist-info/RECORD +0 -41
  35. {python_ship-0.2.0.dist-info → python_ship-0.4.0.dist-info}/WHEEL +0 -0
  36. {python_ship-0.2.0.dist-info → python_ship-0.4.0.dist-info}/entry_points.txt +0 -0
  37. {python_ship-0.2.0.dist-info → python_ship-0.4.0.dist-info}/licenses/LICENSE.md +0 -0
  38. {python_ship-0.2.0.dist-info → python_ship-0.4.0.dist-info}/top_level.txt +0 -0
python_ship/__init__.py CHANGED
@@ -1,3 +1,3 @@
1
1
  """Docker development environments and portable releases for Python web apps."""
2
2
 
3
- __version__ = "0.2.0"
3
+ __version__ = "0.4.0"
python_ship/adapters.py CHANGED
@@ -7,7 +7,7 @@ from pathlib import Path
7
7
  import re
8
8
 
9
9
  from .config import ShipConfig
10
- from .contracts import Interface
10
+ from .contracts import Interface, SetupAdvice, ShipEnvironment
11
11
 
12
12
  TARGET = re.compile(r"[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*:[A-Za-z_][A-Za-z0-9_]*(?:\(\))?")
13
13
  # Directories never searched when detecting a project's app module.
@@ -104,6 +104,11 @@ def callable_interface(project_root: Path, target: str) -> Interface | None:
104
104
  return None
105
105
 
106
106
 
107
+ def _declares_celery(root: Path) -> bool:
108
+ return any(path.is_file() and re.search(r"\bcelery\b", path.read_text(encoding="utf-8"), re.I)
109
+ for path in (root / "pyproject.toml", root / "requirements.txt"))
110
+
111
+
107
112
  class PythonCapabilities:
108
113
  """Optional metadata and process suggestions, requiring actual project evidence."""
109
114
 
@@ -134,8 +139,7 @@ class PythonCapabilities:
134
139
  return {"health": None, "urlSetting": None, "requiredEnv": sorted(set(required))}
135
140
 
136
141
  def suggest_processes(self, config: ShipConfig, root: Path) -> dict:
137
- manifests = [root / "pyproject.toml", root / "requirements.txt"]
138
- if not any(path.is_file() and re.search(r"\bcelery\b", path.read_text(encoding="utf-8"), re.I) for path in manifests):
142
+ if not _declares_celery(root):
139
143
  return {}
140
144
  # The app supplies its Celery instance; the environment remains authoritative.
141
145
  suggestions = {"celery-beat": "celery -A \"$CELERY_APP\" beat --loglevel=info --schedule=/tmp/celerybeat-schedule"}
@@ -143,6 +147,11 @@ class PythonCapabilities:
143
147
  suggestions["celery-worker"] = "celery -A \"$CELERY_APP\" worker --loglevel=info"
144
148
  return suggestions
145
149
 
150
+ def setup_advice(self, config: ShipConfig, root: Path) -> list[SetupAdvice]:
151
+ if config.services.get("tasks") == "celery" and not _declares_celery(root):
152
+ return [SetupAdvice("Declare Celery in your project dependencies and set CELERY_APP before starting the worker.", before_up=True)]
153
+ return []
154
+
146
155
 
147
156
  class GenericAdapter(PythonCapabilities):
148
157
  """Serve any ASGI or WSGI callable; no web framework required."""
@@ -187,6 +196,22 @@ class DjangoAdapter(PythonCapabilities):
187
196
  def detect(self, project_root: Path) -> bool:
188
197
  return (project_root / "manage.py").is_file()
189
198
 
199
+ def environment_defaults(self, config: ShipConfig, root: Path, environment: ShipEnvironment) -> dict[str, str]:
200
+ for path in (module_file(root, config.app), root / "manage.py"):
201
+ if not path.is_file():
202
+ continue
203
+ found = re.search(r"""DJANGO_SETTINGS_MODULE['"]\s*,\s*['"]([\w.]+)['"]""",
204
+ path.read_text(encoding="utf-8", errors="replace"))
205
+ if found:
206
+ return {"DJANGO_SETTINGS_MODULE": found[1]}
207
+ return {}
208
+
209
+ def setup_advice(self, config: ShipConfig, root: Path) -> list[SetupAdvice]:
210
+ advice = super().setup_advice(config, root)
211
+ if config.services.get("database"):
212
+ advice.append(SetupAdvice("Configure Django DATABASES from the DB_* settings, then run `ship manage migrate` after `ship up`."))
213
+ return advice
214
+
190
215
  def default_app(self, project_root: Path) -> str | None:
191
216
  manage = project_root / "manage.py"
192
217
  package = None
@@ -1,8 +1,10 @@
1
1
  """Optional adapter capabilities, shared by initialization, validation and releases."""
2
2
  from pathlib import Path
3
+ import re
3
4
  from typing import Any
4
5
 
5
6
  from .config import ConfigError, ShipConfig, http_problem, process_problem
7
+ from .contracts import SetupAdvice, ShipEnvironment
6
8
  from .production import DEVELOPMENT_DEFAULTS, read_env
7
9
 
8
10
 
@@ -16,13 +18,36 @@ def release_description(adapter: Any, config: ShipConfig, root: Path) -> dict[st
16
18
  problem = http_problem({"port": 8000, "role": "site", "health": result.get("health"), "urlSetting": result.get("urlSetting")})
17
19
  if problem:
18
20
  raise ConfigError(problem)
19
- import re
20
21
  if not isinstance(result.get("requiredEnv", []), list) or any(
21
22
  not isinstance(key, str) or not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", key) for key in result.get("requiredEnv", [])):
22
23
  raise ConfigError("Adapter requiredEnv must list setting names.")
23
24
  return result
24
25
 
25
26
 
27
+ def environment_defaults(adapter: Any, config: ShipConfig, root: Path, environment: ShipEnvironment) -> dict[str, str]:
28
+ hook = getattr(adapter, "environment_defaults", None)
29
+ if hook is None:
30
+ return {}
31
+ result = hook(config, root, environment)
32
+ if not isinstance(result, dict) or any(not isinstance(key, str) or not re.fullmatch(r"[A-Za-z_][A-Za-z0-9_]*", key)
33
+ or not isinstance(value, str) for key, value in result.items()):
34
+ raise ConfigError(f"Adapter {adapter.key!r} environment defaults must map variable names to strings.")
35
+ return result
36
+
37
+
38
+ def setup_advice(config: ShipConfig, root: Path) -> list[SetupAdvice]:
39
+ from .plugins import load_extensions
40
+
41
+ adapter = load_extensions(config).adapter(config)
42
+ hook = getattr(adapter, "setup_advice", None)
43
+ if hook is None:
44
+ return []
45
+ result = hook(config, root)
46
+ if not isinstance(result, list) or any(not isinstance(item, SetupAdvice) or not item.message.strip() for item in result):
47
+ raise ConfigError(f"Adapter {adapter.key!r} setup advice must be a list of nonempty SetupAdvice entries.")
48
+ return result
49
+
50
+
26
51
  def required_setting_problems(description: dict[str, Any], root: Path) -> list[str]:
27
52
  values = read_env(root / ".env.production")
28
53
  return [f"{key} is required in .env.production with a real value."