workpeg 0.157.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.157.0/src/workpeg.egg-info → workpeg-0.158.0}/PKG-INFO +1 -1
  2. {workpeg-0.157.0 → workpeg-0.158.0}/pyproject.toml +1 -1
  3. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/build.py +33 -1
  4. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/cli.py +7 -0
  5. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/create_new.py +8 -1
  6. workpeg-0.158.0/src/workpeg/function_check.py +241 -0
  7. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/project.py +5 -4
  8. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/submit.py +11 -0
  9. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/functions/Dockerfile +3 -3
  10. workpeg-0.158.0/src/workpeg/templates/functions/wheels/README.txt +30 -0
  11. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/project/function/Dockerfile +3 -3
  12. workpeg-0.158.0/src/workpeg/templates/project/function/wheels/README.txt +30 -0
  13. {workpeg-0.157.0 → workpeg-0.158.0/src/workpeg.egg-info}/PKG-INFO +1 -1
  14. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg.egg-info/SOURCES.txt +2 -0
  15. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_build.py +5 -5
  16. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_cli.py +39 -2
  17. workpeg-0.158.0/tests/test_function_check.py +454 -0
  18. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_pegfn_base.py +25 -2
  19. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_submit.py +7 -7
  20. workpeg-0.157.0/src/workpeg/templates/functions/wheels/README.txt +0 -11
  21. workpeg-0.157.0/src/workpeg/templates/project/function/wheels/README.txt +0 -11
  22. {workpeg-0.157.0 → workpeg-0.158.0}/LICENSE +0 -0
  23. {workpeg-0.157.0 → workpeg-0.158.0}/MANIFEST.in +0 -0
  24. {workpeg-0.157.0 → workpeg-0.158.0}/README.md +0 -0
  25. {workpeg-0.157.0 → workpeg-0.158.0}/setup.cfg +0 -0
  26. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/__init__.py +0 -0
  27. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/auth.py +0 -0
  28. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/cdn.py +0 -0
  29. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/config.py +0 -0
  30. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/context.py +0 -0
  31. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/doctor.py +0 -0
  32. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/function_base.py +0 -0
  33. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/run.py +0 -0
  34. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/runtime.py +0 -0
  35. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/sandbox/__init__.py +0 -0
  36. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/sandbox/docker_driver.py +0 -0
  37. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/sandbox/driver.py +0 -0
  38. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/starter.py +0 -0
  39. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/__init__.py +0 -0
  40. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/agents/AGENTS.md +0 -0
  41. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/agents/CLAUDE.md +0 -0
  42. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/functions/LICENSE +0 -0
  43. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/functions/README.md +0 -0
  44. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/functions/app/__init__.py +0 -0
  45. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/functions/app/main.py +0 -0
  46. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/functions/requirements.txt +0 -0
  47. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/project/README.md +0 -0
  48. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/project/function/app/__init__.py +0 -0
  49. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/project/function/app/main.py +0 -0
  50. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/project/function/requirements.txt +0 -0
  51. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/project/workpeg.json +0 -0
  52. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/app_shell.py +0 -0
  53. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/components.py +0 -0
  54. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/main.py +0 -0
  55. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/__init__.py +0 -0
  56. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/customer.py +0 -0
  57. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/customers.py +0 -0
  58. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/home.py +0 -0
  59. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/templates/starter/pages/not_found.py +0 -0
  60. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/terminal.py +0 -0
  61. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/__init__.py +0 -0
  62. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/actions.py +0 -0
  63. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/actions_manifest.py +0 -0
  64. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/app.py +0 -0
  65. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/build.py +0 -0
  66. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/calendar.py +0 -0
  67. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/__init__.py +0 -0
  68. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/ast_parser.py +0 -0
  69. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/ir.py +0 -0
  70. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/js_generator.py +0 -0
  71. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/rtc_checks.py +0 -0
  72. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/compiler/rtc_tables.py +0 -0
  73. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/create.py +0 -0
  74. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/dates.py +0 -0
  75. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/dev_refused_names.json +0 -0
  76. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/devmode.py +0 -0
  77. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/devserve.py +0 -0
  78. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/devsite.py +0 -0
  79. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/fake_cloud.py +0 -0
  80. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/minify.py +0 -0
  81. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/render_nodes.py +0 -0
  82. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/renderers_manifest.py +0 -0
  83. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/rtc.py +0 -0
  84. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-concurrent-replies.md +0 -0
  85. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-prompt-queue.md +0 -0
  86. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-question-frame.md +0 -0
  87. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai-subagent-frame.md +0 -0
  88. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/ai.js +0 -0
  89. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/editor.js +0 -0
  90. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/OFL.txt +0 -0
  91. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic-ext.af944491.woff2 +0 -0
  92. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-cyrillic.88be6946.woff2 +0 -0
  93. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin-ext.370a8962.woff2 +0 -0
  94. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-latin.7611b95c.woff2 +0 -0
  95. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-italic-vietnamese.8dfcf303.woff2 +0 -0
  96. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic-ext.b7a545bb.woff2 +0 -0
  97. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-cyrillic.6129fc85.woff2 +0 -0
  98. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin-ext.58a6b173.woff2 +0 -0
  99. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-latin.9b6f5ff4.woff2 +0 -0
  100. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/fonts/geist-normal-vietnamese.f689f638.woff2 +0 -0
  101. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/icons.css.gz +0 -0
  102. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/overlay.js +0 -0
  103. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/pegment.css +0 -0
  104. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/rtc.js +0 -0
  105. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/scaffold.js +0 -0
  106. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/runtime/workpeg-runtime.js +0 -0
  107. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/scaffold.py +0 -0
  108. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/search_manifest.py +0 -0
  109. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/serve.py +0 -0
  110. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/skills_manifest.py +0 -0
  111. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/state.py +0 -0
  112. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/validators.py +0 -0
  113. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/watcher.py +0 -0
  114. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/__init__.py +0 -0
  115. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/base.py +0 -0
  116. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/canvas.py +0 -0
  117. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/display.py +0 -0
  118. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/fields.py +0 -0
  119. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/forms.py +0 -0
  120. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/futures.py +0 -0
  121. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/gestures.py +0 -0
  122. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/interactive.py +0 -0
  123. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/layout.py +0 -0
  124. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/ui/widgets/pickers.py +0 -0
  125. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg/utils.py +0 -0
  126. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg.egg-info/dependency_links.txt +0 -0
  127. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg.egg-info/entry_points.txt +0 -0
  128. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg.egg-info/requires.txt +0 -0
  129. {workpeg-0.157.0 → workpeg-0.158.0}/src/workpeg.egg-info/top_level.txt +0 -0
  130. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_agents_guide.py +0 -0
  131. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_auth.py +0 -0
  132. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_cdn.py +0 -0
  133. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_cdn_publish.py +0 -0
  134. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_changelog.py +0 -0
  135. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_changelog_guard.py +0 -0
  136. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_config.py +0 -0
  137. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_context.py +0 -0
  138. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_create_new.py +0 -0
  139. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_doctor.py +0 -0
  140. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_project_create.py +0 -0
  141. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_release_script.py +0 -0
  142. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_run.py +0 -0
  143. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_runtime.py +0 -0
  144. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_utils.py +0 -0
  145. {workpeg-0.157.0 → workpeg-0.158.0}/tests/test_version.py +0 -0
  146. {workpeg-0.157.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.157.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.157.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:
@@ -12,6 +12,11 @@ class TemplateError(Exception):
12
12
  pass
13
13
 
14
14
 
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
+
15
20
  def _copy_tree(src_root: Path, dst_root: Path, *, force: bool,
16
21
  base_version: str) -> None:
17
22
  """
@@ -35,7 +40,9 @@ def _copy_tree(src_root: Path, dst_root: Path, *, force: bool,
35
40
  raise TemplateError(f"Target file exists: {dst} (use --force)")
36
41
 
37
42
  dst.parent.mkdir(parents=True, exist_ok=True)
38
- if item.name == "Dockerfile":
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:
39
46
  body = item.read_text(encoding="utf-8")
40
47
  dst.write_text(function_base.fill(body, base_version),
41
48
  encoding="utf-8")
@@ -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
@@ -109,10 +109,11 @@ def create_project(target: Path, namespace: str, *, force: bool = False):
109
109
  source = resources.files("workpeg") / "templates" / "project"
110
110
  with resources.as_file(source) as root:
111
111
  _copy(Path(root), target, namespace, force=force)
112
- dockerfile = target / "function" / "Dockerfile"
113
- dockerfile.write_text(
114
- function_base.fill(dockerfile.read_text(encoding="utf-8"),
115
- base_version), encoding="utf-8")
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")
116
117
  try:
117
118
  write_starter(target / "ui", namespace, force=force)
118
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__",
@@ -4,9 +4,9 @@
4
4
  FROM registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base:REPLACE_PEGFN_BASE_VERSION
5
5
 
6
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 with
8
- # pip download (wheels/README.txt has the command, for Linux x86_64 and
9
- # Python 3.12, which is what Workpeg runs). Never list workpeg itself.
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
10
  COPY wheels /wheels
11
11
  COPY requirements.txt /app/requirements.txt
12
12
  USER root
@@ -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.
@@ -4,9 +4,9 @@
4
4
  FROM registry.gitlab.com/workpeg/workpeg-sdk/pegfn-base:REPLACE_PEGFN_BASE_VERSION
5
5
 
6
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 with
8
- # pip download (wheels/README.txt has the command, for Linux x86_64 and
9
- # Python 3.12, which is what Workpeg runs). Never list workpeg itself.
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
10
  COPY wheels /wheels
11
11
  COPY requirements.txt /app/requirements.txt
12
12
  USER root
@@ -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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: workpeg
3
- Version: 0.157.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
@@ -12,6 +12,7 @@ src/workpeg/context.py
12
12
  src/workpeg/create_new.py
13
13
  src/workpeg/doctor.py
14
14
  src/workpeg/function_base.py
15
+ src/workpeg/function_check.py
15
16
  src/workpeg/project.py
16
17
  src/workpeg/run.py
17
18
  src/workpeg/runtime.py
@@ -129,6 +130,7 @@ tests/test_config.py
129
130
  tests/test_context.py
130
131
  tests/test_create_new.py
131
132
  tests/test_doctor.py
133
+ tests/test_function_check.py
132
134
  tests/test_pegfn_base.py
133
135
  tests/test_project_create.py
134
136
  tests/test_release_script.py
@@ -7,7 +7,7 @@ def _scaffold(tmp_path):
7
7
  project = tmp_path / "demo"
8
8
  project.mkdir()
9
9
  (project / "workpeg.json").write_text("{}")
10
- (project / "Dockerfile").write_text("FROM scratch")
10
+ (project / "Dockerfile").write_text("FROM python:3.12-slim\n")
11
11
  (project / "app").mkdir()
12
12
  (project / "app" / "main.py").write_text("")
13
13
  return project
@@ -42,7 +42,7 @@ def test_validate_raises_without_a_dockerfile(tmp_path):
42
42
  def test_validate_raises_without_app_main(tmp_path):
43
43
  p = tmp_path / "p"
44
44
  p.mkdir()
45
- (p / "Dockerfile").write_text("FROM scratch")
45
+ (p / "Dockerfile").write_text("FROM python:3.12-slim\n")
46
46
  with pytest.raises(FileNotFoundError, match="No app/main.py"):
47
47
  build.validate_function_project(p)
48
48
 
@@ -82,7 +82,7 @@ def test_build_image_uses_default_name(monkeypatch, tmp_path):
82
82
  project = tmp_path / "demo"
83
83
  project.mkdir()
84
84
  (project / "workpeg.json").write_text("{}")
85
- (project / "Dockerfile").write_text("FROM scratch")
85
+ (project / "Dockerfile").write_text("FROM python:3.12-slim\n")
86
86
  (project / "app").mkdir()
87
87
  (project / "app" / "main.py").write_text("")
88
88
 
@@ -106,7 +106,7 @@ def test_build_image_uses_explicit_tag(monkeypatch, tmp_path):
106
106
  project = tmp_path / "demo"
107
107
  project.mkdir()
108
108
  (project / "workpeg.json").write_text("{}")
109
- (project / "Dockerfile").write_text("FROM scratch")
109
+ (project / "Dockerfile").write_text("FROM python:3.12-slim\n")
110
110
  (project / "app").mkdir()
111
111
  (project / "app" / "main.py").write_text("")
112
112
 
@@ -131,7 +131,7 @@ def test_build_image_uses_config_image(monkeypatch, tmp_path):
131
131
  (project / "workpeg.json").write_text(
132
132
  '{"build": {"image": "configured-image"}}'
133
133
  )
134
- (project / "Dockerfile").write_text("FROM scratch")
134
+ (project / "Dockerfile").write_text("FROM python:3.12-slim\n")
135
135
  (project / "app").mkdir()
136
136
  (project / "app" / "main.py").write_text("")
137
137
 
@@ -1,3 +1,5 @@
1
+ from unittest.mock import patch
2
+
1
3
  import pytest
2
4
 
3
5
  import workpeg.ui.build as _ui_build
@@ -166,7 +168,7 @@ def test_cli_function_build(monkeypatch, capsys):
166
168
  monkeypatch.setattr(
167
169
  cli.build,
168
170
  "build_image",
169
- lambda path=".", tag=None: "built-image",
171
+ lambda path=".", tag=None, like_workpeg=False: "built-image",
170
172
  )
171
173
 
172
174
  cli.main(["function", "build"])
@@ -179,9 +181,10 @@ def test_cli_function_build(monkeypatch, capsys):
179
181
  def test_cli_function_build_with_args(monkeypatch, capsys):
180
182
  called = {}
181
183
 
182
- def fake_build_image(path=".", tag=None):
184
+ def fake_build_image(path=".", tag=None, like_workpeg=False):
183
185
  called["path"] = path
184
186
  called["tag"] = tag
187
+ called["like_workpeg"] = like_workpeg
185
188
  return "custom-image"
186
189
 
187
190
  monkeypatch.setattr(
@@ -202,6 +205,7 @@ def test_cli_function_build_with_args(monkeypatch, capsys):
202
205
  assert called == {
203
206
  "path": "/tmp/demo",
204
207
  "tag": "custom-image",
208
+ "like_workpeg": False,
205
209
  }
206
210
 
207
211
  out = capsys.readouterr().out.strip()
@@ -209,6 +213,39 @@ def test_cli_function_build_with_args(monkeypatch, capsys):
209
213
  assert out == "custom-image"
210
214
 
211
215
 
216
+ def test_cli_function_build_like_workpeg_reaches_the_build(monkeypatch):
217
+ """--like-workpeg is passed through to build_image, so the flag on the
218
+ command line is the build Docker gets (test_build pins the argv)."""
219
+ called = {}
220
+
221
+ def fake_build_image(path=".", tag=None, like_workpeg=False):
222
+ called["like_workpeg"] = like_workpeg
223
+ return "img"
224
+
225
+ monkeypatch.setattr(cli.build, "build_image", fake_build_image)
226
+
227
+ cli.main(["function", "build", "--like-workpeg"])
228
+
229
+ assert called == {"like_workpeg": True}
230
+
231
+
232
+ def test_cli_function_build_prints_the_installs_refusal(tmp_path, capsys):
233
+ """A Dockerfile Workpeg refuses fails `workpeg function build` with the
234
+ install's message and exit 2, before Docker is called."""
235
+ (tmp_path / "Dockerfile").write_text("FROM workpeg-api:local\n")
236
+ (tmp_path / "app").mkdir()
237
+ (tmp_path / "app" / "main.py").write_text("")
238
+
239
+ with patch("subprocess.run") as docker:
240
+ with pytest.raises(SystemExit) as e:
241
+ cli.main(["function", "build", "--path", str(tmp_path)])
242
+
243
+ assert e.value.code == 2
244
+ assert "Line 1 of the function bundle's Dockerfile must be FROM python" \
245
+ in capsys.readouterr().err
246
+ docker.assert_not_called()
247
+
248
+
212
249
  def test_cli_function_run(monkeypatch):
213
250
  called = {}
214
251