workflow.core 1.12.3__tar.gz → 1.12.6__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 (61) hide show
  1. {workflow_core-1.12.3 → workflow_core-1.12.6}/PKG-INFO +1 -1
  2. {workflow_core-1.12.3 → workflow_core-1.12.6}/pyproject.toml +1 -1
  3. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/run.py +14 -7
  4. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/configure.py +9 -2
  5. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/execute.py +55 -13
  6. workflow_core-1.12.6/workflow/utils/write.py +60 -0
  7. workflow_core-1.12.3/workflow/utils/write.py +0 -44
  8. {workflow_core-1.12.3 → workflow_core-1.12.6}/README.md +0 -0
  9. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/__init__.py +0 -0
  10. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/__init__.py +0 -0
  11. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/buckets.py +0 -0
  12. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/configs.py +0 -0
  13. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/main.py +0 -0
  14. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/pipelines.py +0 -0
  15. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/results.py +0 -0
  16. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/schedules.py +0 -0
  17. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/cli/workspace.py +0 -0
  18. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/daemons/__init__.py +0 -0
  19. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/daemons/audit.py +0 -0
  20. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/daemons/transfer.py +0 -0
  21. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/definitions/__init__.py +0 -0
  22. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/definitions/config.py +0 -0
  23. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/definitions/jsonschema.md +0 -0
  24. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/definitions/notify.py +0 -0
  25. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/definitions/work.py +0 -0
  26. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/definitions/workspace.py +0 -0
  27. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/examples/autocomplete.py +0 -0
  28. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/examples/function.py +0 -0
  29. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/examples/sample.svg +0 -0
  30. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/examples/sample.workflow.yml +0 -0
  31. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/exceptions.py +0 -0
  32. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/helpers/__init__.py +0 -0
  33. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/helpers/date.py +0 -0
  34. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/__init__.py +0 -0
  35. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/buckets.py +0 -0
  36. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/client.py +0 -0
  37. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/configs.py +0 -0
  38. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/context.py +0 -0
  39. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/pipelines.py +0 -0
  40. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/results.py +0 -0
  41. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/http/schedules.py +0 -0
  42. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/__init__.py +0 -0
  43. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/archive/__init__.py +0 -0
  44. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/archive/http.py +0 -0
  45. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/archive/posix.py +0 -0
  46. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/archive/s3.py +0 -0
  47. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/lifecycle/attempt.py +0 -0
  48. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/__init__.py +0 -0
  49. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/decorators.py +0 -0
  50. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/format.py +0 -0
  51. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/logger.py +0 -0
  52. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/prompt.py +0 -0
  53. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/read.py +0 -0
  54. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/renderers.py +0 -0
  55. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/tmate.sh +0 -0
  56. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/validate.py +0 -0
  57. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/utils/variables.py +0 -0
  58. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/workspaces/__init__.py +0 -0
  59. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/workspaces/chimefrb.yml +0 -0
  60. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/workspaces/development.yml +0 -0
  61. {workflow_core-1.12.3 → workflow_core-1.12.6}/workflow/workspaces/site-dev.yml +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: workflow.core
3
- Version: 1.12.3
3
+ Version: 1.12.6
4
4
  Summary: Workflow Core
5
5
  License: MIT
6
6
  Author: Shiny Brar
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "workflow.core"
3
- version = "1.12.3"
3
+ version = "1.12.6"
4
4
  description = "Workflow Core"
5
5
  authors = ["Shiny Brar <charanjotbrar@gmail.com>"]
6
6
  license = "MIT"
@@ -2,6 +2,7 @@
2
2
 
3
3
  import platform
4
4
  import signal
5
+ from contextlib import nullcontext
5
6
  from pathlib import Path
6
7
  from sys import stderr, stdout
7
8
  from threading import Event
@@ -228,13 +229,19 @@ def run(
228
229
  "[bold]Starting Workflow Lifecycle[/bold]",
229
230
  extra=dict(markup=True, color="green"),
230
231
  )
231
- console = Console(force_terminal=True, tab_size=4)
232
- with console.status(
233
- status="",
234
- spinner="aesthetic" if tty else "dots",
235
- spinner_style="bold green",
236
- refresh_per_second=10 if tty else 1,
237
- ):
232
+ # Without a terminal (e.g. a container's logs) every spinner frame would be
233
+ # written to the logs, so it's only shown in a terminal
234
+ spinner = (
235
+ Console(force_terminal=True, tab_size=4).status(
236
+ status="",
237
+ spinner="aesthetic",
238
+ spinner_style="bold green",
239
+ refresh_per_second=10,
240
+ )
241
+ if tty
242
+ else nullcontext()
243
+ )
244
+ with spinner:
238
245
  lifecycle(
239
246
  buckets=buckets,
240
247
  function=function,
@@ -119,7 +119,9 @@ def arguments(func: Callable[..., Any], work: Work) -> List[str]:
119
119
  paraneter_name_in_function, None
120
120
  )
121
121
 
122
- if parameter_value_in_work:
122
+ # Only missing parameters are left to their defaults: False, 0 and ""
123
+ # are values too
124
+ if parameter_value_in_work is not None:
123
125
  if isinstance(parameter, click.Argument):
124
126
  # If argument, then the parameter is purely positional without a key
125
127
  if isinstance(parameter_value_in_work, list):
@@ -131,7 +133,12 @@ def arguments(func: Callable[..., Any], work: Work) -> List[str]:
131
133
  if hasattr(parameter, "is_flag") and parameter.is_flag:
132
134
  # If is_flag=True,
133
135
  # then the parameter is a flag and doesn't have a value
134
- args.append(f"{parameter_name_in_cli}")
136
+ if parameter_value_in_work:
137
+ args.append(f"{parameter_name_in_cli}")
138
+ elif parameter.secondary_opts:
139
+ # An on/off flag (e.g. --trace/--no-trace) set to False
140
+ args.append(f"{parameter.secondary_opts[-1]}")
141
+ # A plain flag set to False is left off
135
142
  else:
136
143
  space_or_equals = ""
137
144
 
@@ -13,6 +13,10 @@ from workflow.utils.logger import get_logger
13
13
 
14
14
  logger = get_logger("workflow.lifecycle.execute")
15
15
 
16
+ # The lines of a command's stdout and stderr kept in its results (the last ones),
17
+ # so a verbose command doesn't fill the database
18
+ OUTPUT_LINES = 1024
19
+
16
20
  Outcome = Union[Dict[str, Any], Tuple[Dict[str, Any], List[str], List[str]], Any, None]
17
21
 
18
22
 
@@ -75,6 +79,46 @@ def function(work: Work) -> Work:
75
79
  return work
76
80
 
77
81
 
82
+ def last_lines(stream: Optional[bytes]) -> List[str]:
83
+ """The last OUTPUT_LINES lines of a command's output.
84
+
85
+ Args:
86
+ stream (Optional[bytes]): Its stdout or stderr
87
+
88
+ Returns:
89
+ List[str]: Its last lines
90
+ """
91
+ return (
92
+ (stream or b"").decode("utf-8", errors="replace").splitlines()[-OUTPUT_LINES:]
93
+ )
94
+
95
+
96
+ def output(
97
+ work: Work,
98
+ args: Any,
99
+ stdout: Optional[bytes],
100
+ stderr: Optional[bytes],
101
+ returncode: Optional[int],
102
+ ) -> None:
103
+ """Keep a command's output in the work's results, unless it has results.
104
+
105
+ Args:
106
+ work (Work): Work object
107
+ args (Any): The command run
108
+ stdout (Optional[bytes]): Its standard output
109
+ stderr (Optional[bytes]): Its standard error
110
+ returncode (Optional[int]): Its exit code, None if it was stopped
111
+ """
112
+ if work.results or work.products or work.plots:
113
+ return
114
+ work.results = {
115
+ "args": args,
116
+ "stdout": last_lines(stdout),
117
+ "stderr": last_lines(stderr),
118
+ "returncode": returncode,
119
+ }
120
+
121
+
78
122
  def command(work: Work) -> Work:
79
123
  """Execute a command.
80
124
 
@@ -103,22 +147,20 @@ def command(work: Work) -> Work:
103
147
  timeout=work.timeout,
104
148
  shell=is_shell, # nosec
105
149
  )
106
- # Check return code
107
- process.check_returncode()
108
- # Convert stdout and stderr to strings
109
- stdout = process.stdout.decode("utf-8").splitlines()
110
- stderr = process.stderr.decode("utf-8").splitlines()
111
- # Convert last line of stdout to a Tuple
112
- if not (work.results or work.products or work.plots):
113
- work.results = {
114
- "args": process.args,
115
- "stdout": stdout,
116
- "stderr": stderr,
117
- "returncode": process.returncode,
118
- }
150
+ # Keep the output before checking the return code: a failed command's
151
+ # output is the most useful
152
+ output(work, process.args, process.stdout, process.stderr, process.returncode)
119
153
  # * Check if results are less than 4MB
120
154
  validate.size(work)
155
+ # Check return code
156
+ process.check_returncode()
121
157
  work.status = "success"
158
+ except subprocess.TimeoutExpired as error:
159
+ # Keep what it printed before it was stopped
160
+ output(work, error.cmd, error.stdout, error.stderr, None)
161
+ validate.size(work)
162
+ work.status = "failure"
163
+ logger.exception(error)
122
164
  except Exception as error:
123
165
  work.status = "failure"
124
166
  logger.exception(error)
@@ -0,0 +1,60 @@
1
+ """Writing utilities for workflow."""
2
+
3
+ import os
4
+ import shutil
5
+ import tempfile
6
+ from datetime import datetime
7
+ from pathlib import Path
8
+ from typing import Any, Dict, Union
9
+
10
+ import yaml
11
+
12
+ from workflow import DEFAULT_WORKSPACE_PATH
13
+
14
+
15
+ def workspace(
16
+ source: Union[Path, Dict[str, Any]], destination: Path = DEFAULT_WORKSPACE_PATH
17
+ ) -> None:
18
+ """Write a workspace to a destination.
19
+
20
+ The destination is replaced atomically, so a reader never finds it missing or
21
+ partly written; when it changes, the previous one is kept as a dated `.bak`.
22
+ A source file is copied, not moved.
23
+
24
+ Args:
25
+ source (Union[Path, Dict[str, Any]]): Workspace to write.
26
+ destination (Path): Destination to write the workspace.
27
+
28
+ Raises:
29
+ ValueError: If source is not a dictionary or a file path.
30
+
31
+ Returns:
32
+ None: None
33
+ """
34
+ if isinstance(source, Path):
35
+ assert source.exists(), f"workspace {source} does not exist."
36
+ content: str = source.read_text()
37
+ elif isinstance(source, dict): # type: ignore
38
+ content = yaml.dump(source)
39
+ else:
40
+ raise ValueError(f"source {source} is not a dictionary or a file path.")
41
+ destination.parent.mkdir(parents=True, exist_ok=True)
42
+ # Runners can share one home (e.g. CANFAR sessions on /arc), so another one may be
43
+ # reading the workspace right now: it is never moved away or left half-written.
44
+ if destination.exists():
45
+ if destination.read_text() == content:
46
+ return
47
+ # Back up the one being replaced, as a copy so it stays in place meanwhile.
48
+ date: str = datetime.now().strftime("%Y%m%d%H%M%S")
49
+ shutil.copy2(destination, destination.with_suffix(f".{date}.bak"))
50
+ # Write beside it, then rename into place: readers see the old or the new file.
51
+ descriptor, temporary = tempfile.mkstemp(
52
+ dir=destination.parent, prefix=f".{destination.name}.", suffix=".tmp"
53
+ )
54
+ try:
55
+ with os.fdopen(descriptor, "w") as filename:
56
+ filename.write(content)
57
+ os.replace(temporary, destination)
58
+ except BaseException:
59
+ Path(temporary).unlink(missing_ok=True)
60
+ raise
@@ -1,44 +0,0 @@
1
- """Writing utilities for workflow."""
2
-
3
- from datetime import datetime
4
- from pathlib import Path
5
- from typing import Any, Dict, Union
6
-
7
- import yaml
8
-
9
- from workflow import DEFAULT_WORKSPACE_PATH
10
-
11
-
12
- def workspace(
13
- source: Union[Path, Dict[str, Any]], destination: Path = DEFAULT_WORKSPACE_PATH
14
- ) -> None:
15
- """Write a workspace to a destination.
16
-
17
- Args:
18
- source (Union[Path, Dict[str, Any]]): Workspace to write.
19
- destination (Path): Destination to write the workspace.
20
-
21
- Raises:
22
- ValueError: If source is not a dictionary or a file path.
23
-
24
- Returns:
25
- None: None
26
- """
27
- if not destination.parent.exists():
28
- destination.parent.mkdir(parents=True, exist_ok=True)
29
- # If destination file exists, move it to a backup location file.
30
- date: str = datetime.now().strftime("%Y%m%d%H%M%S")
31
- if destination.exists():
32
- destination.replace(destination.with_suffix(f".{date}.bak"))
33
- # Make sure the source path exists.
34
- if isinstance(source, Path):
35
- assert source.exists(), f"workspace {source} does not exist."
36
- source.replace(destination)
37
- return
38
- # Write dictionary to destination.
39
- elif isinstance(source, dict): # type: ignore
40
- with open(destination, "w") as filename:
41
- yaml.dump(source, filename)
42
- return
43
- else:
44
- raise ValueError(f"source {source} is not a dictionary or a file path.")
File without changes