workpeg 0.156.0__tar.gz → 0.158.0__tar.gz

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 (146) hide show
  1. {workpeg-0.156.0/src/workpeg.egg-info → workpeg-0.158.0}/PKG-INFO +1 -1
  2. {workpeg-0.156.0 → workpeg-0.158.0}/pyproject.toml +1 -1
  3. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/build.py +33 -1
  4. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/cli.py +7 -0
  5. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/create_new.py +27 -3
  6. workpeg-0.158.0/src/workpeg/function_base.py +88 -0
  7. workpeg-0.158.0/src/workpeg/function_check.py +241 -0
  8. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/project.py +13 -0
  9. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/submit.py +11 -0
  10. workpeg-0.158.0/src/workpeg/templates/functions/Dockerfile +16 -0
  11. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/functions/README.md +13 -4
  12. workpeg-0.158.0/src/workpeg/templates/functions/wheels/README.txt +30 -0
  13. workpeg-0.158.0/src/workpeg/templates/project/function/Dockerfile +16 -0
  14. workpeg-0.158.0/src/workpeg/templates/project/function/wheels/README.txt +30 -0
  15. {workpeg-0.156.0 → workpeg-0.158.0/src/workpeg.egg-info}/PKG-INFO +1 -1
  16. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg.egg-info/SOURCES.txt +6 -0
  17. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_build.py +5 -5
  18. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_cli.py +39 -2
  19. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_create_new.py +8 -2
  20. workpeg-0.158.0/tests/test_function_check.py +454 -0
  21. workpeg-0.158.0/tests/test_pegfn_base.py +258 -0
  22. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_submit.py +7 -7
  23. workpeg-0.156.0/src/workpeg/templates/functions/Dockerfile +0 -18
  24. workpeg-0.156.0/src/workpeg/templates/project/function/Dockerfile +0 -18
  25. {workpeg-0.156.0 → workpeg-0.158.0}/LICENSE +0 -0
  26. {workpeg-0.156.0 → workpeg-0.158.0}/MANIFEST.in +0 -0
  27. {workpeg-0.156.0 → workpeg-0.158.0}/README.md +0 -0
  28. {workpeg-0.156.0 → workpeg-0.158.0}/setup.cfg +0 -0
  29. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/__init__.py +0 -0
  30. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/auth.py +0 -0
  31. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/cdn.py +0 -0
  32. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/config.py +0 -0
  33. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/context.py +0 -0
  34. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/doctor.py +0 -0
  35. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/run.py +0 -0
  36. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/runtime.py +0 -0
  37. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/sandbox/__init__.py +0 -0
  38. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/sandbox/docker_driver.py +0 -0
  39. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/sandbox/driver.py +0 -0
  40. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/starter.py +0 -0
  41. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/__init__.py +0 -0
  42. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/agents/AGENTS.md +0 -0
  43. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/agents/CLAUDE.md +0 -0
  44. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/functions/LICENSE +0 -0
  45. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/functions/app/__init__.py +0 -0
  46. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/functions/app/main.py +0 -0
  47. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/functions/requirements.txt +0 -0
  48. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/project/README.md +0 -0
  49. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/project/function/app/__init__.py +0 -0
  50. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/project/function/app/main.py +0 -0
  51. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/project/function/requirements.txt +0 -0
  52. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/project/workpeg.json +0 -0
  53. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/app_shell.py +0 -0
  54. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/components.py +0 -0
  55. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/main.py +0 -0
  56. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/__init__.py +0 -0
  57. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/customer.py +0 -0
  58. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/customers.py +0 -0
  59. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/home.py +0 -0
  60. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/not_found.py +0 -0
  61. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/terminal.py +0 -0
  62. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/__init__.py +0 -0
  63. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/actions.py +0 -0
  64. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/actions_manifest.py +0 -0
  65. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/app.py +0 -0
  66. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/build.py +0 -0
  67. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/calendar.py +0 -0
  68. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/__init__.py +0 -0
  69. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/ast_parser.py +0 -0
  70. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/ir.py +0 -0
  71. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/js_generator.py +0 -0
  72. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/rtc_checks.py +0 -0
  73. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/rtc_tables.py +0 -0
  74. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/create.py +0 -0
  75. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/dates.py +0 -0
  76. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/dev_refused_names.json +0 -0
  77. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/devmode.py +0 -0
  78. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/devserve.py +0 -0
  79. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/devsite.py +0 -0
  80. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/fake_cloud.py +0 -0
  81. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/minify.py +0 -0
  82. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/render_nodes.py +0 -0
  83. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/renderers_manifest.py +0 -0
  84. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/rtc.py +0 -0
  85. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-concurrent-replies.md +0 -0
  86. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-prompt-queue.md +0 -0
  87. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-question-frame.md +0 -0
  88. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-subagent-frame.md +0 -0
  89. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai.js +0 -0
  90. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/editor.js +0 -0
  91. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/OFL.txt +0 -0
  92. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic-ext.af944491.woff2 +0 -0
  93. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic.88be6946.woff2 +0 -0
  94. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin-ext.370a8962.woff2 +0 -0
  95. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin.7611b95c.woff2 +0 -0
  96. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-vietnamese.8dfcf303.woff2 +0 -0
  97. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic-ext.b7a545bb.woff2 +0 -0
  98. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic.6129fc85.woff2 +0 -0
  99. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin-ext.58a6b173.woff2 +0 -0
  100. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin.9b6f5ff4.woff2 +0 -0
  101. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-vietnamese.f689f638.woff2 +0 -0
  102. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/icons.css.gz +0 -0
  103. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/overlay.js +0 -0
  104. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/pegment.css +0 -0
  105. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/rtc.js +0 -0
  106. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/scaffold.js +0 -0
  107. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/workpeg-runtime.js +0 -0
  108. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/scaffold.py +0 -0
  109. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/search_manifest.py +0 -0
  110. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/serve.py +0 -0
  111. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/skills_manifest.py +0 -0
  112. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/state.py +0 -0
  113. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/validators.py +0 -0
  114. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/watcher.py +0 -0
  115. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/__init__.py +0 -0
  116. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/base.py +0 -0
  117. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/canvas.py +0 -0
  118. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/display.py +0 -0
  119. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/fields.py +0 -0
  120. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/forms.py +0 -0
  121. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/futures.py +0 -0
  122. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/gestures.py +0 -0
  123. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/interactive.py +0 -0
  124. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/layout.py +0 -0
  125. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/pickers.py +0 -0
  126. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg/utils.py +0 -0
  127. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg.egg-info/dependency_links.txt +0 -0
  128. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg.egg-info/entry_points.txt +0 -0
  129. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg.egg-info/requires.txt +0 -0
  130. {workpeg-0.156.0 → workpeg-0.158.0}/src/workpeg.egg-info/top_level.txt +0 -0
  131. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_agents_guide.py +0 -0
  132. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_auth.py +0 -0
  133. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_cdn.py +0 -0
  134. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_cdn_publish.py +0 -0
  135. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_changelog.py +0 -0
  136. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_changelog_guard.py +0 -0
  137. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_config.py +0 -0
  138. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_context.py +0 -0
  139. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_doctor.py +0 -0
  140. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_project_create.py +0 -0
  141. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_release_script.py +0 -0
  142. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_run.py +0 -0
  143. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_runtime.py +0 -0
  144. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_utils.py +0 -0
  145. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_version.py +0 -0
  146. {workpeg-0.156.0 → workpeg-0.158.0}/tests/test_watch_pipeline.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: workpeg
3
- Version: 0.156.0
3
+ Version: 0.158.0
4
4
  Summary: Workpeg function runtime and SDK
5
5
  Author-email: Workpeg <support@workpeg.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "workpeg"
7
- version = "0.156.0"
7
+ version = "0.158.0"
8
8
  description = "Workpeg function runtime and SDK"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -1,8 +1,17 @@
1
1
  # src/workpeg/build.py
2
2
  import subprocess
3
+ import sys
3
4
  from pathlib import Path
4
5
 
5
6
  from workpeg.config import load_config
7
+ from workpeg.function_check import BundleRefused, check_config, check_files
8
+
9
+ #: What Workpeg's install adds to every function build (the api's
10
+ #: BUILD_ARGV_HEAD): pull the base from the registry and give no RUN step a
11
+ #: network. The platform is what Workpeg runs, so an Apple silicon Mac
12
+ #: builds the image Workpeg would, with the x86_64 wheels the docs vendor,
13
+ #: instead of an arm64 one. ``--like-workpeg`` builds with these.
14
+ WORKPEG_BUILD_FLAGS = ("--pull", "--network=none", "--platform=linux/amd64")
6
15
 
7
16
 
8
17
  def slugify(value: str) -> str:
@@ -39,18 +48,41 @@ def build_image(
39
48
  path: str = ".",
40
49
  tag: str | None = None,
41
50
  timeout: "float | None" = None,
51
+ like_workpeg: bool = False,
42
52
  ) -> str:
53
+ """Build the function at *path* and return the image's tag.
54
+
55
+ The Dockerfile is checked first, by the rules Workpeg's install applies
56
+ (workpeg.function_check), and a Dockerfile Workpeg would refuse is
57
+ refused here with the same message, before Docker runs: a build that
58
+ succeeds locally on a Dockerfile Workpeg refuses tells the developer
59
+ nothing true. A ``build.image`` in workpeg.json is still used as the
60
+ local tag, with a warning, because only the upload is refused for it.
61
+
62
+ *like_workpeg* adds WORKPEG_BUILD_FLAGS, so the build fails here when a
63
+ step downloads, or when the wheels are not for linux/amd64."""
43
64
  project_path = Path(path).resolve()
44
65
 
45
66
  validate_function_project(project_path)
67
+ check_files(project_path)
68
+ try:
69
+ check_config(project_path)
70
+ except BundleRefused as refused:
71
+ print(f"WARNING: {refused} `workpeg function submit` will refuse "
72
+ "this project.", file=sys.stderr)
46
73
 
47
74
  cfg = load_config(project_path / "workpeg.json")
48
75
 
49
76
  image = tag or cfg.get("build", {}).get("image") or default_image_name(project_path)
50
77
 
78
+ argv = ["docker", "build"]
79
+ if like_workpeg:
80
+ argv += WORKPEG_BUILD_FLAGS
81
+ argv += ["-t", image, str(project_path)]
82
+
51
83
  try:
52
84
  subprocess.run(
53
- ["docker", "build", "-t", image, str(project_path)],
85
+ argv,
54
86
  check=True,
55
87
  timeout=timeout,
56
88
  )
@@ -94,6 +94,12 @@ def build_parser() -> argparse.ArgumentParser:
94
94
  )
95
95
  build_p.add_argument("--path", default=".")
96
96
  build_p.add_argument("--tag", default=None)
97
+ build_p.add_argument(
98
+ "--like-workpeg",
99
+ action="store_true",
100
+ help="build as Workpeg's install does: --pull, no network for any "
101
+ "RUN step, and linux/amd64",
102
+ )
97
103
  build_p.set_defaults(_handler="function.build")
98
104
 
99
105
  run_p = function_sub.add_parser(
@@ -745,6 +751,7 @@ def main(argv=None) -> None:
745
751
  image = build.build_image(
746
752
  path=args.path,
747
753
  tag=args.tag,
754
+ like_workpeg=args.like_workpeg,
748
755
  )
749
756
  print(image)
750
757
  except (FileNotFoundError, NotADirectoryError, RuntimeError) as e:
@@ -5,14 +5,23 @@ import shutil
5
5
  from pathlib import Path
6
6
  import importlib.resources as pkg_resources
7
7
 
8
+ from workpeg import function_base
9
+
8
10
 
9
11
  class TemplateError(Exception):
10
12
  pass
11
13
 
12
14
 
13
- def _copy_tree(src_root: Path, dst_root: Path, *, force: bool) -> None:
15
+ #: Template files that name the function base, filled with this SDK's
16
+ #: version as they are copied.
17
+ _FILLED = frozenset((Path("Dockerfile"), Path("wheels") / "README.txt"))
18
+
19
+
20
+ def _copy_tree(src_root: Path, dst_root: Path, *, force: bool,
21
+ base_version: str) -> None:
14
22
  """
15
- Recursively copy src_root directory contents into dst_root.
23
+ Recursively copy src_root directory contents into dst_root, writing
24
+ *base_version* into the Dockerfile's FROM line.
16
25
  """
17
26
  if not src_root.exists():
18
27
  raise TemplateError(f"Template root not found: {src_root}")
@@ -31,6 +40,13 @@ def _copy_tree(src_root: Path, dst_root: Path, *, force: bool) -> None:
31
40
  raise TemplateError(f"Target file exists: {dst} (use --force)")
32
41
 
33
42
  dst.parent.mkdir(parents=True, exist_ok=True)
43
+ # The Dockerfile's FROM line and the wheels/README.txt command
44
+ # that fills wheels/ both name the base at this SDK's version.
45
+ if rel in _FILLED:
46
+ body = item.read_text(encoding="utf-8")
47
+ dst.write_text(function_base.fill(body, base_version),
48
+ encoding="utf-8")
49
+ continue
34
50
  shutil.copy2(item, dst)
35
51
 
36
52
 
@@ -41,6 +57,13 @@ def create_new_project(target_dir: str, *, force: bool = False) -> Path:
41
57
  """
42
58
  target = Path(target_dir).expanduser().resolve()
43
59
 
60
+ # Named before anything is written, so an SDK that cannot name a base
61
+ # image leaves no half-made project behind.
62
+ try:
63
+ base_version = function_base.base_version()
64
+ except function_base.BaseVersionError as exc:
65
+ raise TemplateError(str(exc)) from None
66
+
44
67
  # Get a Traversable pointing at .../workpeg/templates/functions
45
68
  traversable = (
46
69
  pkg_resources.files("workpeg") / "templates" / "functions"
@@ -51,7 +74,8 @@ def create_new_project(target_dir: str, *, force: bool = False) -> Path:
51
74
  # "not available as a filesystem path" issue.
52
75
  with pkg_resources.as_file(traversable) as src_path:
53
76
  src_root = Path(src_path)
54
- _copy_tree(src_root, target, force=force)
77
+ _copy_tree(src_root, target, force=force,
78
+ base_version=base_version)
55
79
 
56
80
  return target
57
81
 
@@ -0,0 +1,88 @@
1
+ """Which Workpeg function base image a new function project builds FROM.
2
+
3
+ Every function template starts ``FROM`` the base this SDK's release pipeline
4
+ pushes, at one exact version::
5
+
6
+ FROM registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base:<X.Y.Z>
7
+
8
+ The base carries the SDK, the non-root ``appuser`` and the runtime's
9
+ ENTRYPOINT, so a bundle adds only its own code and the wheels it vendors.
10
+ Workpeg builds a function with no network, so the base is the only way the
11
+ SDK gets into the image at all; the api refuses any other tag than an exact
12
+ ``X.Y.Z`` (``_allowed_base`` in the api's ``function_container.py``).
13
+
14
+ The version written is the SDK the developer has installed, read when
15
+ ``workpeg function new`` or ``workpeg project create`` runs, so the image a
16
+ project builds on is the runtime it was tested against locally. A dev tree
17
+ installs as version ``0``, which names no image, so it writes the newest
18
+ release its own ``CHANGELOG.md`` records instead, and refuses when it cannot
19
+ find one rather than write a ``FROM`` that cannot be pulled.
20
+ """
21
+ from __future__ import annotations
22
+
23
+ import re
24
+ from pathlib import Path
25
+
26
+ #: The image the release pipeline pushes (``pegfn_base`` in .gitlab-ci.yml),
27
+ #: and the one name the api accepts besides the official ``python`` images.
28
+ IMAGE = "registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base"
29
+
30
+ #: Substituted into the function templates' Dockerfiles.
31
+ PLACEHOLDER = "REPLACE_PEGFN_BASE_VERSION"
32
+
33
+ #: A release version as the api reads one: three dot-separated numbers.
34
+ # The api's own rule for the base's tag (installed_pegs/function_container.py
35
+ # _RELEASE): at most four digits in each part, no leading zero. A version
36
+ # this accepts and the api refuses would name a FROM no install can build.
37
+ RELEASE = re.compile(
38
+ r"(0|[1-9][0-9]{0,3})\.(0|[1-9][0-9]{0,3})\.(0|[1-9][0-9]{0,3})")
39
+
40
+ _HEADING = re.compile(r"^## \[((?:0|[1-9][0-9]*)(?:\.(?:0|[1-9][0-9]*)){2})\]",
41
+ re.MULTILINE)
42
+
43
+
44
+ class BaseVersionError(Exception):
45
+ """No released base image can be named for this SDK."""
46
+
47
+
48
+ def _installed_version() -> str:
49
+ from workpeg import __version__
50
+ return __version__
51
+
52
+
53
+ def _source_changelog() -> Path:
54
+ # src/workpeg/function_base.py -> the repository root, in a dev tree.
55
+ return Path(__file__).resolve().parents[2] / "CHANGELOG.md"
56
+
57
+
58
+ def base_version(version: str | None = None,
59
+ changelog: Path | None = None) -> str:
60
+ """The exact ``X.Y.Z`` of the base image a new project should use."""
61
+ version = _installed_version() if version is None else version
62
+ if RELEASE.fullmatch(version):
63
+ return version
64
+ path = _source_changelog() if changelog is None else changelog
65
+ try:
66
+ found = _HEADING.search(path.read_text(encoding="utf-8"))
67
+ except OSError:
68
+ found = None
69
+ if found:
70
+ return found.group(1)
71
+ raise BaseVersionError(
72
+ f"This workpeg is version '{version}', not a release, and no "
73
+ f"released version was found in {path}, so there is no "
74
+ f"{IMAGE} tag to build on. Install a released SDK "
75
+ f"(pip install workpeg) and run the command again.")
76
+
77
+
78
+ def base_image(version: str | None = None,
79
+ changelog: Path | None = None) -> str:
80
+ """``IMAGE:<X.Y.Z>`` for the base a new project should use."""
81
+ return f"{IMAGE}:{base_version(version, changelog)}"
82
+
83
+
84
+ def fill(text: str, version: str | None = None) -> str:
85
+ """*text* with the template's base-version placeholder filled in."""
86
+ if PLACEHOLDER not in text:
87
+ return text
88
+ return text.replace(PLACEHOLDER, base_version(version))
@@ -0,0 +1,241 @@
1
+ """What Workpeg's install refuses in a function bundle, checked before it
2
+ leaves the developer's machine.
3
+
4
+ Workpeg reads a function bundle's ``Dockerfile`` and ``workpeg.json`` before
5
+ it builds anything (``check_bundle`` and ``check_dockerfile`` in the api's
6
+ ``installed_pegs/function_container.py``), and refuses one that could reach
7
+ past its own build context. Until this module the SDK read neither, so a
8
+ refused bundle uploaded cleanly and the refusal was seen only by an operator
9
+ at install, never by the developer who could fix it.
10
+
11
+ This is a port of those rules, not a call to them: the api is the authority
12
+ and keeps its own copy, so nothing a developer changes in their SDK can
13
+ loosen the install's check. The two must agree, which is why the messages
14
+ are the api's own, word for word, and why ``tests/test_function_check.py``
15
+ carries the api's cases. When the api's rules change, change this file and
16
+ those tests in the same release.
17
+
18
+ Fail closed, as the api does: anything this cannot read as Docker does is
19
+ refused, not guessed at.
20
+ """
21
+ from __future__ import annotations
22
+
23
+ import itertools
24
+ import json
25
+ import re
26
+ import stat
27
+ from pathlib import Path
28
+
29
+ from workpeg.function_base import IMAGE as PLATFORM_BASE
30
+ from workpeg.function_base import RELEASE as _RELEASE
31
+
32
+ #: The official images a function's FROM may name, written ``<name>`` or
33
+ #: ``<name>:<tag>``: python only, and never an ``-onbuild`` tag.
34
+ ALLOWED_BASES = ("python",)
35
+
36
+ #: Docker's rule for a tag (distribution/reference).
37
+ _TAG = re.compile(r"[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}")
38
+
39
+ #: What Workpeg builds: the Dockerfile, and the function itself.
40
+ _REQUIRED = ("Dockerfile", "app/main.py")
41
+
42
+ #: Printable ASCII, tabs and line ends: the only text the check reads
43
+ #: exactly as Docker does. A carriage return is a line end only before a
44
+ #: newline.
45
+ _UNREADABLE = re.compile(r"[^\t\n\r\x20-\x7e]|\r(?!\n)")
46
+
47
+ #: A parser directive: ``# name=value`` before the first instruction.
48
+ _DIRECTIVE = re.compile(r"#[ \t]*[A-Za-z][A-Za-z0-9]*[ \t]*=")
49
+
50
+ #: A backslash, then spaces or tabs only, at the end of a line: it
51
+ #: continues on the next.
52
+ _CONTINUES = re.compile(r"\\[ \t]*\Z")
53
+
54
+ #: Two at the end, which one Docker parser reads as continued and another
55
+ #: as ended.
56
+ _ESCAPED_END = re.compile(r"\\\\[ \t]*\Z")
57
+
58
+ #: Docker's rule for a stage's AS name, which it folds to lower case.
59
+ _STAGE = re.compile(r"[a-z][a-z0-9_.-]*")
60
+
61
+ #: Every instruction but ONBUILD, VOLUME and LABEL.
62
+ _INSTRUCTIONS = frozenset((
63
+ "add", "arg", "cmd", "copy", "entrypoint", "env", "expose", "from",
64
+ "healthcheck", "maintainer", "run", "shell", "stopsignal", "user",
65
+ "workdir"))
66
+
67
+
68
+ class BundleRefused(RuntimeError):
69
+ """Workpeg's install would refuse this function bundle. The message is
70
+ the one the install gives."""
71
+
72
+
73
+ def check_bundle(directory) -> Path:
74
+ """The project as a resolved Path, once Workpeg would build it: a
75
+ regular ``Dockerfile`` and ``app/main.py``, a Dockerfile
76
+ :func:`check_dockerfile` accepts, and a ``workpeg.json`` that names no
77
+ image. Otherwise :class:`BundleRefused`."""
78
+ project = check_files(directory)
79
+ check_config(project)
80
+ return project
81
+
82
+
83
+ def check_files(directory) -> Path:
84
+ """The Dockerfile and ``app/main.py`` half of :func:`check_bundle`."""
85
+ project = Path(directory).resolve()
86
+ for required in _REQUIRED:
87
+ if not _is_regular(project / required):
88
+ raise BundleRefused(
89
+ f"The function bundle has no regular file {required}.")
90
+ # Bytes, not text mode: text mode would turn a lone carriage return
91
+ # into a line end, which Docker does not.
92
+ try:
93
+ dockerfile = (project / "Dockerfile").read_bytes().decode("utf-8")
94
+ except ValueError:
95
+ raise BundleRefused(
96
+ "The function bundle's Dockerfile cannot be read as UTF-8 text.")
97
+ check_dockerfile(dockerfile)
98
+ return project
99
+
100
+
101
+ def check_config(project) -> None:
102
+ """Refuse a ``workpeg.json`` that is not a JSON object, or that names
103
+ the image (``build.image``): Workpeg names every function image."""
104
+ try:
105
+ config = json.loads(
106
+ (Path(project) / "workpeg.json").read_text(encoding="utf-8"))
107
+ except FileNotFoundError:
108
+ return
109
+ except (OSError, ValueError, RecursionError):
110
+ raise BundleRefused(
111
+ "The function bundle's workpeg.json cannot be read as JSON.")
112
+ if not isinstance(config, dict):
113
+ raise BundleRefused(
114
+ "The function bundle's workpeg.json is not a JSON object.")
115
+ build = config.get("build")
116
+ if build is not None and (not isinstance(build, dict)
117
+ or build.get("image") is not None):
118
+ raise BundleRefused(
119
+ "The function bundle names its own image (build.image in "
120
+ "workpeg.json). The platform names every function image; "
121
+ "upload a new version without build.image.")
122
+
123
+
124
+ def check_dockerfile(text: str) -> None:
125
+ """Refuse, with :class:`BundleRefused` naming the line, a function
126
+ Dockerfile Workpeg's install would refuse (docs/function-base.md, "The
127
+ rules for every function build")."""
128
+ bad = _UNREADABLE.search(text)
129
+ if bad:
130
+ _refuse(text.count("\n", 0, bad.start()) + 1,
131
+ "is not plain ASCII text with tabs and line ends only, "
132
+ "which is all this check can read exactly as Docker does")
133
+ stages: list[str] = [] # each stage's AS name so far, '' when none
134
+ for number, line in _instructions(text):
135
+ if "<<" in line:
136
+ _refuse(number, "has a heredoc (<<), which Docker reads by "
137
+ "rules of its own; write the file and COPY it")
138
+ keyword, *words = line.split()
139
+ keyword = keyword.lower()
140
+ flags = list(itertools.takewhile(lambda w: w.startswith("--"), words))
141
+ args = words[len(flags):]
142
+ if keyword not in _INSTRUCTIONS:
143
+ _refuse(number, "is not an instruction a function Dockerfile "
144
+ "may use (ONBUILD, VOLUME and LABEL are not)")
145
+ if any(c in flag for flag in flags for c in "\"'\\"):
146
+ _refuse(number, "has a flag with a quote or a backslash in it")
147
+ if keyword == "from":
148
+ stages.append(_from(number, args, stages))
149
+ if keyword == "run" and flags:
150
+ _refuse(number, "is a RUN step with a -- flag (--network, "
151
+ "--security, --device, --mount): a function RUN step "
152
+ "takes none")
153
+ for flag in flags if keyword in ("copy", "add") else ():
154
+ name, _, value = flag[2:].partition("=")
155
+ # Docker folds a stage name to lower case where AS defines it
156
+ # and where --from looks it up, so --from=Build is the stage
157
+ # 'build'. A stage name is only ever _STAGE once folded, so no
158
+ # image reference, with its ':' or '/', can match one.
159
+ if name.lower() == "from" and value.lower() not in stages[:-1]:
160
+ _refuse(number, "has a --from that is not an earlier stage "
161
+ "of this Dockerfile, named with AS")
162
+ if keyword == "add" and ("://" in line
163
+ or any(c in line for c in "@$\"'\\")):
164
+ _refuse(number, "is an ADD with a URL, a git source or a $, "
165
+ "quote or backslash: ADD takes plain local paths only, "
166
+ "and COPY does the rest")
167
+
168
+
169
+ def _from(number, args, stages):
170
+ """The AS name of the stage a FROM starts ('' when it has none), once
171
+ its base is an earlier stage or an allowed image. A stage is looked up
172
+ folded to lower case, as Docker folds it where AS defines it and where
173
+ FROM names it, so `FROM Build` after `AS Build` is that stage. An image
174
+ is matched as written: Docker refuses an upper-case reference."""
175
+ named = len(args) == 3 and args[1].lower() == "as"
176
+ name = args[2].lower() if named else ""
177
+ if not (len(args) == 1 or (named and _STAGE.fullmatch(name))) or not (
178
+ args[0].lower() in stages or _allowed_base(args[0])):
179
+ _refuse(number, "must be FROM python or python:<tag>, FROM "
180
+ f"{PLATFORM_BASE}:<X.Y.Z>, or FROM an earlier stage of "
181
+ "this Dockerfile, with an optional AS <name>")
182
+ return name
183
+
184
+
185
+ def _allowed_base(image):
186
+ name, colon, tag = image.partition(":")
187
+ if name == PLATFORM_BASE:
188
+ return bool(colon) and bool(_RELEASE.fullmatch(tag))
189
+ return (name in ALLOWED_BASES and (not colon or bool(_TAG.fullmatch(tag)))
190
+ and "onbuild" not in tag)
191
+
192
+
193
+ def _instructions(text):
194
+ """(line number, instruction) for each instruction of *text*, joined as
195
+ moby/buildkit's parser joins them: a line ending in a backslash and
196
+ spaces or tabs continues on the next; inside a continuation, comment
197
+ and blank lines are dropped and do not end it. Refuses a parser
198
+ directive before the first instruction."""
199
+ lines = [line.removesuffix("\r") for line in text.split("\n")]
200
+ index, first = 0, True
201
+ while index < len(lines):
202
+ number, line = index + 1, lines[index].lstrip(" \t")
203
+ index += 1
204
+ if line.startswith("#"):
205
+ if first and _DIRECTIVE.match(line):
206
+ _refuse(number, "is a parser directive (# name=value "
207
+ "before the first instruction): # syntax= chooses "
208
+ "the image that builds a Dockerfile and # escape= "
209
+ "how it is read, so a function Dockerfile has none")
210
+ continue
211
+ line, more = _continued(number, line)
212
+ while more and index < len(lines):
213
+ part = lines[index]
214
+ index += 1
215
+ if not part.strip(" \t") or part.lstrip(" \t").startswith("#"):
216
+ continue
217
+ part, more = _continued(index, part)
218
+ line += part
219
+ if line.strip(" \t"):
220
+ first = False
221
+ yield number, line
222
+
223
+
224
+ def _continued(number, line):
225
+ """*line* without its continuation backslash, and 1 if it had one."""
226
+ if _ESCAPED_END.search(line):
227
+ _refuse(number, "ends in two backslashes, which one version of "
228
+ "Docker's parser reads as continued and another as ended")
229
+ return _CONTINUES.subn("", line)
230
+
231
+
232
+ def _refuse(number, why):
233
+ raise BundleRefused(
234
+ f"Line {number} of the function bundle's Dockerfile {why}.")
235
+
236
+
237
+ def _is_regular(path: Path) -> bool:
238
+ try:
239
+ return stat.S_ISREG(path.lstat().st_mode)
240
+ except OSError:
241
+ return False
@@ -31,6 +31,7 @@ import re
31
31
  from importlib import resources
32
32
  from pathlib import Path
33
33
 
34
+ from workpeg import function_base
34
35
  from workpeg.starter import (
35
36
  PLACEHOLDER, TemplateExists, copy_template, write_agent_guide,
36
37
  write_starter,
@@ -98,9 +99,21 @@ def create_project(target: Path, namespace: str, *, force: bool = False):
98
99
  f"{target} already exists and is not empty. Use --force to write "
99
100
  f"into it anyway.")
100
101
 
102
+ # Named before anything is written, so an SDK that cannot name the
103
+ # function's base image leaves no half-made peg behind.
104
+ try:
105
+ base_version = function_base.base_version()
106
+ except function_base.BaseVersionError as exc:
107
+ raise ProjectError(str(exc)) from None
108
+
101
109
  source = resources.files("workpeg") / "templates" / "project"
102
110
  with resources.as_file(source) as root:
103
111
  _copy(Path(root), target, namespace, force=force)
112
+ for filled in ("Dockerfile", "wheels/README.txt"):
113
+ path = target / "function" / filled
114
+ path.write_text(
115
+ function_base.fill(path.read_text(encoding="utf-8"),
116
+ base_version), encoding="utf-8")
104
117
  try:
105
118
  write_starter(target / "ui", namespace, force=force)
106
119
  except TemplateExists as exc:
@@ -96,11 +96,22 @@ def build_tar_gz_bytes(source_dir: Path) -> bytes:
96
96
 
97
97
  from workpeg.build import validate_function_project
98
98
 
99
+ from workpeg.function_check import BundleRefused, check_bundle
100
+
99
101
  try:
100
102
  validate_function_project(source_dir)
101
103
  except (FileNotFoundError, NotADirectoryError) as e:
102
104
  _fail(str(e))
103
105
 
106
+ # What Workpeg's install would refuse is refused here, before anything
107
+ # is uploaded, with the install's own message. Before this check a refused
108
+ # bundle uploaded cleanly and only an operator saw why it never ran.
109
+ try:
110
+ check_bundle(source_dir)
111
+ except BundleRefused as e:
112
+ _fail(f"{e} Workpeg would refuse to build this function, so it "
113
+ "was not uploaded.")
114
+
104
115
  exclude_names = {
105
116
  ".git",
106
117
  "__pycache__",
@@ -0,0 +1,16 @@
1
+ # Built on the Workpeg function base: Python 3.12, this SDK at one exact
2
+ # version, a non-root user (appuser, 10001), WORKDIR /app and the runtime's
3
+ # ENTRYPOINT. `workpeg function new` wrote the version of the SDK it ran as.
4
+ FROM registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base:REPLACE_PEGFN_BASE_VERSION
5
+
6
+ # Workpeg builds this image with no network, so nothing here may download.
7
+ # Extra packages come from wheels/, filled before you build or submit by
8
+ # running pip inside this FROM image (wheels/README.txt has the command).
9
+ # Never list workpeg itself: the base already has it.
10
+ COPY wheels /wheels
11
+ COPY requirements.txt /app/requirements.txt
12
+ USER root
13
+ RUN pip install --no-cache-dir --no-index --find-links=/wheels -r /app/requirements.txt
14
+ USER appuser
15
+
16
+ COPY app /app/app
@@ -12,8 +12,9 @@ It contains everything you need to build and run a single **Workpeg Function**,
12
12
  app/
13
13
  __init__.py
14
14
  main.py # Your function: def main(context, payload) -> dict
15
- requirements.txt # Python dependencies (must include workpeg)
16
- Dockerfile # Example container runtime using workpeg runtime
15
+ requirements.txt # Extra Python dependencies (not workpeg: the base has it)
16
+ wheels/ # The wheels those come from; builds have no network
17
+ Dockerfile # FROM the Workpeg function base at this SDK's version
17
18
  ```
18
19
 
19
20
  ---
@@ -42,16 +43,18 @@ export FUNCTION_ENTRYPOINT="module.path:function_name"
42
43
 
43
44
  ## Running Locally
44
45
 
45
- Make sure `workpeg` is installed (or use the generated `requirements.txt`):
46
+ Make sure `workpeg` is installed, plus anything your function lists in
47
+ `requirements.txt` (which never lists `workpeg` itself):
46
48
 
47
49
  ```bash
50
+ pip install workpeg
48
51
  pip install -r requirements.txt
49
52
  ```
50
53
 
51
54
  Then run the function:
52
55
 
53
56
  ```bash
54
- echo '{"context": {}, "payload": {"hello": "world"}}' | workpeg runtime
57
+ echo '{"context": {}, "payload": {"hello": "world"}}' | workpeg function runtime
55
58
  ```
56
59
 
57
60
  Expected output:
@@ -64,6 +67,12 @@ Expected output:
64
67
 
65
68
  ## Docker (Optional)
66
69
 
70
+ The `Dockerfile` starts `FROM registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base`
71
+ at the SDK version that created this project. The base already has the SDK,
72
+ the non-root `appuser` and the runtime's entrypoint. Workpeg builds the image
73
+ with no network, so add a package by listing it in `requirements.txt` and
74
+ downloading its wheel into `wheels/` (see `wheels/README.txt`).
75
+
67
76
  You can build and run the container using the included `Dockerfile`:
68
77
 
69
78
  ```bash
@@ -0,0 +1,30 @@
1
+ Wheels for the packages in requirements.txt go here.
2
+
3
+ Workpeg builds a function image with no network, so the Dockerfile installs
4
+ from this folder only (pip install --no-index --find-links=/wheels). Fill it
5
+ before you build or submit, by running pip inside the image your Dockerfile
6
+ starts FROM. Run it from the folder that holds the Dockerfile and
7
+ requirements.txt (the one above this README). If you change the FROM line,
8
+ change the image below to match:
9
+
10
+ docker run --rm --platform linux/amd64 \
11
+ --user "$(id -u):$(id -g)" -e HOME=/tmp \
12
+ -v "$PWD:/w" -w /w --entrypoint pip \
13
+ registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base:REPLACE_PEGFN_BASE_VERSION \
14
+ wheel --no-cache-dir --wheel-dir wheels -r requirements.txt
15
+
16
+ Inside that image pip picks the wheels it can install, with every package
17
+ they depend on. Avoid `pip download --platform ...` on your own computer: it
18
+ matches one tag exactly and misses wheels tagged with older Linux names.
19
+
20
+ Run it again whenever requirements.txt changes. When requirements.txt is
21
+ empty there is nothing to fill, but keep this folder and this file: the
22
+ Dockerfile copies wheels/, and the build fails without it. The SDK itself is
23
+ already in the base image; do not list workpeg in requirements.txt.
24
+
25
+ To build the way Workpeg does before you submit, from the same folder:
26
+
27
+ workpeg function build --like-workpeg
28
+
29
+ It checks the Dockerfile by the rules Workpeg's install applies, then builds
30
+ with --pull --network=none --platform=linux/amd64, as Workpeg does.
@@ -0,0 +1,16 @@
1
+ # Built on the Workpeg function base: Python 3.12, this SDK at one exact
2
+ # version, a non-root user (appuser, 10001), WORKDIR /app and the runtime's
3
+ # ENTRYPOINT. `workpeg project create` wrote the version of the SDK it ran as.
4
+ FROM registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base:REPLACE_PEGFN_BASE_VERSION
5
+
6
+ # Workpeg builds this image with no network, so nothing here may download.
7
+ # Extra packages come from wheels/, filled before you build or submit by
8
+ # running pip inside this FROM image (wheels/README.txt has the command).
9
+ # Never list workpeg itself: the base already has it.
10
+ COPY wheels /wheels
11
+ COPY requirements.txt /app/requirements.txt
12
+ USER root
13
+ RUN pip install --no-cache-dir --no-index --find-links=/wheels -r /app/requirements.txt
14
+ USER appuser
15
+
16
+ COPY app /app/app