scout-rig 0.2.6 → 0.2.7
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.
- checksums.yaml +4 -4
- data/.vimproject +31 -13
- data/README.md +27 -4
- data/Rakefile +1 -0
- data/VERSION +1 -1
- data/doc/Improvements.md +396 -0
- data/doc/StartHere.md +95 -0
- data/doc/developer/Architecture.md +232 -0
- data/doc/developer/DataConversionInternals.md +407 -0
- data/doc/developer/DesignPrinciples.md +254 -0
- data/doc/developer/PythonTaskRunnerInternals.md +371 -0
- data/doc/developer/PythonWorkflowInternals.md +539 -0
- data/doc/developer/ScoutPythonInternals.md +504 -0
- data/doc/user/Cookbook.md +337 -0
- data/doc/user/DefiningPythonTasks.md +708 -0
- data/doc/user/DrivingWorkflowsFromPython.md +418 -0
- data/doc/user/IteratingPythonData.md +142 -0
- data/doc/user/PassingDataToPython.md +223 -0
- data/doc/user/PythonEnvironments.md +197 -0
- data/doc/user/RunningPythonFromRuby.md +267 -0
- data/doc/user/ScriptingPython.md +281 -0
- data/doc/user/TestingAndPackaging.md +487 -0
- data/lib/scout/python/script.rb +11 -2
- data/lib/scout/workflow/python/inputs.rb +33 -23
- data/lib/scout/workflow/python/task.rb +219 -22
- data/lib/scout/workflow/python.rb +236 -9
- data/lib/scout-rig/testing/availability.rb +608 -0
- data/python/README.md +51 -0
- data/python/scout/__init__.py +2 -0
- data/python/scout/runner.py +193 -29
- data/python/scout/task_errors.py +6 -0
- data/research/00_scope_and_themes.md +131 -0
- data/research/data-conversion-and-scripting-analysis.md +209 -0
- data/research/design-philosophy-analysis.md +246 -0
- data/research/python-helper-package-analysis.md +272 -0
- data/research/pythonworkflow-analysis.md +199 -0
- data/research/scoutpython-bridge-analysis.md +185 -0
- data/research/synthesis-report.md +109 -0
- data/research/testing-availability-baseline.md +1625 -0
- data/research/testing-availability-final-report.md +500 -0
- data/research/validation-report.md +149 -0
- data/scout-rig.gemspec +35 -5
- data/test/scout/python/test_util.rb +13 -0
- data/test/scout/test_python.rb +10 -0
- data/test/scout/workflow/python/test_task.rb +1119 -1
- data/test/scout-rig/testing/test_availability.rb +495 -0
- data/test/scout-rig/testing/test_test_support.rb +166 -0
- data/test/test_helper.rb +96 -19
- metadata +47 -4
- data/doc/Python.md +0 -482
- data/doc/PythonWorkflow.md +0 -163
|
@@ -1,13 +1,66 @@
|
|
|
1
1
|
require_relative '../../python'
|
|
2
2
|
require 'shellwords'
|
|
3
|
+
require 'tmpdir'
|
|
3
4
|
module PythonWorkflow
|
|
4
5
|
|
|
6
|
+
class PythonTaskException < ScoutException
|
|
7
|
+
def initialize(message, python_traceback = nil)
|
|
8
|
+
super(message)
|
|
9
|
+
self.python_traceback = python_traceback if python_traceback
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def python_traceback
|
|
13
|
+
@python_traceback || Array(backtrace).join("\n")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def python_traceback=(traceback)
|
|
17
|
+
@python_traceback = traceback
|
|
18
|
+
set_backtrace(traceback.lines(chomp: true))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.json_create(data)
|
|
22
|
+
exception = new(data.fetch('m'), data['python_traceback'])
|
|
23
|
+
exception.set_backtrace(data['b']) if data['b']
|
|
24
|
+
exception
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def as_json(*)
|
|
28
|
+
super.merge('python_traceback' => @python_traceback)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
5
32
|
def self.read_python_metadata(file)
|
|
6
33
|
out = ScoutPython.run_file file, '--scout-metadata'
|
|
7
|
-
raise "Error getting metadata from #{file}
|
|
34
|
+
raise "Error getting metadata from #{file}" unless out.exit_status == 0
|
|
8
35
|
JSON.parse(out.read)
|
|
9
36
|
end
|
|
10
37
|
|
|
38
|
+
# ScoutCoder: batch mode runs each file with __name__ == '__main__' and
|
|
39
|
+
# collects registrations immediately, but imported modules are shared/cached
|
|
40
|
+
# across files, so task registrations hidden in a shared import may be absent
|
|
41
|
+
# from later files' metadata. Use the default per-file mode when isolation is
|
|
42
|
+
# required.
|
|
43
|
+
def self.read_python_metadata_files(files)
|
|
44
|
+
runner = File.expand_path('../../../../python/scout/runner.py', __dir__)
|
|
45
|
+
out = ScoutPython.run_file runner, Shellwords.shelljoin(['--scout-metadata-files', *files.map(&:to_s)])
|
|
46
|
+
raise "Error getting metadata for Python task files: #{files.join(', ')}" unless out.exit_status == 0
|
|
47
|
+
JSON.parse(out.read).each_with_object({}) do |entry, result|
|
|
48
|
+
result[File.expand_path(entry.fetch('file'))] = entry.fetch('metadata')
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def self.read_task_error(file)
|
|
53
|
+
payload = JSON.parse(File.read(file))
|
|
54
|
+
return nil unless payload.is_a?(Hash) && payload.keys.sort == %w[error message traceback version]
|
|
55
|
+
return nil unless payload['version'].instance_of?(Integer) && payload['version'] == 1
|
|
56
|
+
return nil unless payload['error'] == 'ScoutTaskError'
|
|
57
|
+
return nil unless payload['message'].is_a?(String) && payload['traceback'].is_a?(String)
|
|
58
|
+
|
|
59
|
+
PythonTaskException.new(payload['message'], payload['traceback'])
|
|
60
|
+
rescue JSON::ParserError, Errno::ENOENT, Errno::EACCES, Errno::EISDIR
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
|
|
11
64
|
def self.map_returns(py_type)
|
|
12
65
|
case py_type
|
|
13
66
|
when nil then :string
|
|
@@ -27,11 +80,23 @@ module PythonWorkflow
|
|
|
27
80
|
end
|
|
28
81
|
end
|
|
29
82
|
|
|
30
|
-
|
|
83
|
+
# Python annotation -> Scout input type:
|
|
84
|
+
# string -> :string integer -> :integer float -> :float
|
|
85
|
+
# boolean -> :boolean binary -> :binary path -> :file
|
|
86
|
+
# list[subtype] -> :array (subtype path -> :file_array)
|
|
87
|
+
# Optional[X] / X | None and Annotated[X, ...] map like their inner type;
|
|
88
|
+
# required-ness comes from the Python default, not the annotation.
|
|
89
|
+
#
|
|
90
|
+
# literal_strings mirrors the workflow's python_literal_strings flag: when
|
|
91
|
+
# true a `str` parameter's Scout input carries :noload, so Scout's input
|
|
92
|
+
# layer (Task.format_input) no longer reads a String naming an existing
|
|
93
|
+
# file into its contents and the literal path reaches Python. Only the
|
|
94
|
+
# string->:string mapping gets :noload; :file/:file_array semantics are
|
|
95
|
+
# already "filename" and are unchanged.
|
|
96
|
+
def self.map_param(p, literal_strings: false)
|
|
31
97
|
desc = p['help'] || ""
|
|
32
98
|
default = p['default']
|
|
33
99
|
required = p['required'] ? true : false
|
|
34
|
-
|
|
35
100
|
ruby_type =
|
|
36
101
|
case p['type']
|
|
37
102
|
when 'string' then :string
|
|
@@ -55,55 +120,182 @@ module PythonWorkflow
|
|
|
55
120
|
|
|
56
121
|
options = {}
|
|
57
122
|
options[:required] = true if required
|
|
123
|
+
options[:noload] = true if literal_strings && p['type'] == 'string'
|
|
58
124
|
|
|
59
125
|
{ name: p['name'], type: ruby_type, desc: desc, default: default, options: options }
|
|
60
126
|
end
|
|
61
127
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
128
|
+
# Effective input contract for one Python parameter, documenting both the
|
|
129
|
+
# Python declaration and the Scout input it produces.
|
|
130
|
+
def self.input_contract(p, literal_strings: false)
|
|
131
|
+
subtype = p['type'].start_with?('list[') ? p['type'][5..-2] : nil
|
|
132
|
+
mapped = map_param(p, literal_strings: literal_strings)
|
|
133
|
+
options = mapped[:options] || {}
|
|
134
|
+
behavior =
|
|
135
|
+
case mapped[:type]
|
|
136
|
+
when :file then 'filename'
|
|
137
|
+
when :file_array then 'list of filenames'
|
|
138
|
+
when :array then 'list of strings'
|
|
139
|
+
when :string then literal_strings ? 'literal text' : 'text or loaded file contents'
|
|
140
|
+
else 'value'
|
|
141
|
+
end
|
|
142
|
+
{
|
|
143
|
+
'name' => p['name'],
|
|
144
|
+
'python_annotation' => p['annotation'],
|
|
145
|
+
'python_type' => p['type'],
|
|
146
|
+
'scout_type' => mapped[:type],
|
|
147
|
+
'required' => p['required'] ? true : false,
|
|
148
|
+
'default' => p['default'],
|
|
149
|
+
'help' => p['help'] || '',
|
|
150
|
+
'behavior' => behavior,
|
|
151
|
+
'flags' => {
|
|
152
|
+
'python_flag' => "--#{p['name']}",
|
|
153
|
+
'nargs' => p['type'].start_with?('list[') ? '+' : nil,
|
|
154
|
+
'elem_type' => subtype,
|
|
155
|
+
'noload' => options[:noload] ? true : nil
|
|
156
|
+
}.compact
|
|
157
|
+
}
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
def python_task(task_sym = nil, file: nil, returns: nil, extension: nil, desc: nil, metadata: nil)
|
|
161
|
+
name = task_sym ? task_sym.to_s : nil
|
|
162
|
+
file ||= name && Open.readpath(python_task_dir[name].find_with_extension('py'))
|
|
65
163
|
raise "Python task file not found: #{file}" unless File.exist?(file)
|
|
66
164
|
|
|
67
|
-
metas = PythonWorkflow.read_python_metadata(file)
|
|
165
|
+
metas = metadata || PythonWorkflow.read_python_metadata(file)
|
|
166
|
+
register_python_task_metadata(file, metas, returns: returns, extension: extension, desc: desc)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Whether a task-object block file is one of ours: a task registered by
|
|
170
|
+
# this codebase (task.rb defines the only Python task blocks). Live-task
|
|
171
|
+
# provenance is the single ownership signal — the tracking map holds
|
|
172
|
+
# names, not task objects.
|
|
173
|
+
private def owns_task_definition?(file)
|
|
174
|
+
# Same file under any spelling (dotted relative, symlink, vendored copy):
|
|
175
|
+
# realpath collapses both, and missing files fall back to expand_path so a
|
|
176
|
+
# stale source_location still compares rather than raising.
|
|
177
|
+
ours = File.realpath(__FILE__) rescue File.expand_path(__FILE__)
|
|
178
|
+
theirs = File.realpath(file) rescue File.expand_path(file)
|
|
179
|
+
theirs == ours
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
# Validate exported-task metadata before any DSL side effect. The same
|
|
183
|
+
# Python file may be re-registered (that replaces its entry); a task name
|
|
184
|
+
# already exported by a different file is a conflict, and a name already
|
|
185
|
+
# held by a native (Ruby-defined) task is rejected rather than silently
|
|
186
|
+
# shadowed.
|
|
187
|
+
def validate_python_task_metadata(file, metas, skip_cross_file: false)
|
|
188
|
+
raise "No tasks exported by #{file}" if metas.empty?
|
|
189
|
+
|
|
190
|
+
metas.each_with_index do |meta, i|
|
|
191
|
+
next if Hash === meta
|
|
192
|
+
raise "Malformed task metadata at position #{i} in #{file}: expected a Hash, got #{meta.class}"
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
metas.each do |meta|
|
|
196
|
+
raise "Malformed task metadata in #{file}: missing 'name' in #{meta.inspect}" if meta['name'].nil? || meta['name'].to_s.empty?
|
|
197
|
+
raise "Malformed task metadata in #{file}: missing 'params' for task #{meta['name']}" unless meta.key?('params')
|
|
198
|
+
params = meta['params']
|
|
199
|
+
raise "Malformed task metadata in #{file}: 'params' must be a list for task #{meta['name']}, got #{params.class}" unless params.nil? || Array === params
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
seen = {}
|
|
203
|
+
metas.each do |meta|
|
|
204
|
+
name = meta['name'].to_s
|
|
205
|
+
raise "Duplicate exported task name #{name} in #{file}" if seen[name]
|
|
206
|
+
seen[name] = true
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
unless skip_cross_file
|
|
210
|
+
@python_task_registrations.to_a.each do |other_file, other_registration|
|
|
211
|
+
next if File.expand_path(other_file) == File.expand_path(file)
|
|
212
|
+
other_registration['metas'].each do |meta|
|
|
213
|
+
name = meta['name'].to_s
|
|
214
|
+
raise "Task #{name} in #{file} already registered by #{File.expand_path(other_file)}" if seen[name]
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# A native (Ruby-defined) task already holding one of these names is an
|
|
220
|
+
# explicit conflict: registering would silently shadow it. A name is
|
|
221
|
+
# ours only when the live task object under it is a PythonWorkflow
|
|
222
|
+
# registration — i.e. its block was defined in this file, which is the
|
|
223
|
+
# only place Python task blocks are built.
|
|
224
|
+
metas.map { |meta| meta['name'].to_s }.each do |name|
|
|
225
|
+
existing = tasks[name.to_sym]
|
|
226
|
+
next unless existing
|
|
227
|
+
location = existing.source_location
|
|
228
|
+
next if location && owns_task_definition?(location.first)
|
|
229
|
+
raise "Task name #{name} in #{file} conflicts with a native (Ruby-defined) task; pick another name"
|
|
230
|
+
end
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def register_python_task_metadata(file, metas, returns: nil, extension: nil, desc: nil)
|
|
68
235
|
metas = [metas] unless Array === metas
|
|
236
|
+
validate_python_task_metadata(file, metas)
|
|
237
|
+
|
|
238
|
+
# Registration record: per file, the Python metadata plus the
|
|
239
|
+
# registration-time configuration (literal_strings, which cannot be
|
|
240
|
+
# applied after the inputs are built).
|
|
241
|
+
registration = {
|
|
242
|
+
'literal_strings' => python_literal_strings,
|
|
243
|
+
'metas' => metas
|
|
244
|
+
}
|
|
69
245
|
|
|
70
|
-
# For each function defined in the python file, register a workflow task
|
|
71
246
|
metas.each do |meta|
|
|
72
247
|
meta['returns'] = returns.to_s if returns
|
|
73
248
|
task_desc = desc || meta['description']
|
|
74
249
|
|
|
250
|
+
# literal_strings must reach map_param while the input options are
|
|
251
|
+
# being built (noload cannot be applied later). It is fixed at
|
|
252
|
+
# registration time in the record above.
|
|
253
|
+
literal_strings = registration['literal_strings']
|
|
254
|
+
|
|
75
255
|
ruby_returns = PythonWorkflow.map_returns(meta['returns'])
|
|
76
|
-
|
|
77
|
-
ruby_inputs = []
|
|
78
|
-
else
|
|
79
|
-
ruby_inputs = meta['params'].map { |p| PythonWorkflow.map_param(p) }
|
|
80
|
-
end
|
|
256
|
+
ruby_inputs = meta['params'].nil? ? [] : meta['params'].map { |p| PythonWorkflow.map_param(p, literal_strings: literal_strings) }
|
|
81
257
|
|
|
82
258
|
ruby_inputs.each do |inp|
|
|
83
259
|
input(inp[:name].to_sym, inp[:type], inp[:desc], inp[:default], inp[:options] || {})
|
|
84
260
|
end
|
|
85
261
|
|
|
86
262
|
self.desc(task_desc) if task_desc && !task_desc.empty?
|
|
87
|
-
|
|
263
|
+
effective_extension = meta['extension'] || extension
|
|
264
|
+
self.extension(effective_extension) if effective_extension
|
|
88
265
|
|
|
89
266
|
task({ meta['name'].to_sym => ruby_returns }) do |*args|
|
|
90
267
|
arg_names = ruby_inputs.map { |i| i[:name] }
|
|
91
268
|
values = {}
|
|
92
|
-
arg_names.each_with_index { |n,i| values[n] = args[i] }
|
|
269
|
+
arg_names.each_with_index { |n, i| values[n] = args[i] }
|
|
93
270
|
|
|
94
271
|
argv = PythonWorkflow.build_python_argv(meta['params'], values)
|
|
95
|
-
# prefix with function name so the python script runs the desired function
|
|
96
272
|
full_argv = [meta['name']] + argv
|
|
273
|
+
output = nil
|
|
274
|
+
Dir.mktmpdir('scout-python-task-') do |protocol_dir|
|
|
275
|
+
protocol_file = File.join(protocol_dir, 'controlled-error.json')
|
|
276
|
+
invocation_argv = full_argv + ['--scout-error-file', protocol_file]
|
|
277
|
+
begin
|
|
278
|
+
output = ScoutPython.run_file file, Shellwords.shelljoin(invocation_argv),
|
|
279
|
+
env: { 'SCOUT_JOB_FILES_DIR' => files_dir.to_s }
|
|
280
|
+
rescue ProcessFailed
|
|
281
|
+
controlled_error = PythonWorkflow.read_task_error(protocol_file)
|
|
282
|
+
if controlled_error
|
|
283
|
+
controlled_error.set_backtrace(controlled_error.python_traceback.lines(chomp: true))
|
|
284
|
+
raise controlled_error
|
|
285
|
+
end
|
|
286
|
+
raise
|
|
287
|
+
end
|
|
288
|
+
end
|
|
97
289
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
290
|
+
txt = output.read.to_s
|
|
291
|
+
if output.exit_status != 0
|
|
292
|
+
stderr_text = (output.stderr.respond_to?(:read) ? output.stderr.read : output.stderr.to_s).to_s
|
|
293
|
+
detail = stderr_text.strip.empty? ? txt.strip : stderr_text.strip
|
|
294
|
+
raise "Python task #{meta['name']} failed with exit status #{output.exit_status}" + (detail.empty? ? '' : ":\n#{detail}")
|
|
295
|
+
end
|
|
103
296
|
begin
|
|
104
297
|
next JSON.parse(txt)
|
|
105
298
|
rescue JSON::ParserError
|
|
106
|
-
# not JSON; for list returns, split by newline
|
|
107
299
|
if ruby_returns == :array || ruby_returns == :file_array
|
|
108
300
|
next txt.split("\n").map(&:to_s)
|
|
109
301
|
end
|
|
@@ -111,5 +303,10 @@ module PythonWorkflow
|
|
|
111
303
|
end
|
|
112
304
|
end
|
|
113
305
|
end
|
|
306
|
+
|
|
307
|
+
# Commit the tracking only after every task in the file registered
|
|
308
|
+
# cleanly; a raise leaves the previous record intact.
|
|
309
|
+
(@python_task_registrations ||= {})[File.expand_path(file)] = registration
|
|
310
|
+
metas
|
|
114
311
|
end
|
|
115
312
|
end
|
|
@@ -9,10 +9,236 @@ module PythonWorkflow
|
|
|
9
9
|
attr_accessor :python_task_dir
|
|
10
10
|
|
|
11
11
|
def python_task_dir
|
|
12
|
-
@python_task_dir ||=
|
|
12
|
+
@python_task_dir ||= libdir.python.tasks.find
|
|
13
13
|
end
|
|
14
14
|
|
|
15
|
-
def
|
|
15
|
+
def python_task_dir=(path)
|
|
16
|
+
@python_task_dir = path
|
|
17
|
+
load_python_tasks(batch: true)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# When true, `str`-annotated Python parameters register their Scout input
|
|
21
|
+
# with the :noload option, so a String naming an existing file is passed to
|
|
22
|
+
# Python literally instead of being loaded into its contents by Scout's
|
|
23
|
+
# input layer (Task#process_inputs / Task.format_input). Default false
|
|
24
|
+
# preserves the historical load behaviour. Takes effect at task
|
|
25
|
+
# registration time: set it before load_directory (load_directory accepts
|
|
26
|
+
# a literal_strings: keyword), or call refresh_python_tasks! after
|
|
27
|
+
# flipping — a config-only change with identical Python metadata
|
|
28
|
+
# re-registers the affected tasks.
|
|
29
|
+
def python_literal_strings
|
|
30
|
+
return @python_literal_strings if defined?(@python_literal_strings)
|
|
31
|
+
@python_literal_strings = false
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def python_literal_strings=(value)
|
|
35
|
+
@python_literal_strings = value ? true : false
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Input contract for one Python task, or all of them as a hash, without
|
|
39
|
+
# executing any function. There is ONE view per task, built from the STORED
|
|
40
|
+
# registration metadata (the last registration this workflow applied), not
|
|
41
|
+
# from a fresh extraction of the Python source: it reports what the
|
|
42
|
+
# workflow is registered to do today. Call refresh_python_tasks! (or
|
|
43
|
+
# re-register) to pick up source changes.
|
|
44
|
+
#
|
|
45
|
+
# { 'task' => ..., 'file' => ..., 'returns' => ...,
|
|
46
|
+
# 'inputs' => [ { 'name', 'python_annotation', 'python_type', 'scout_type',
|
|
47
|
+
# 'required', 'default', 'help', 'behavior',
|
|
48
|
+
# 'flags' => { 'python_flag', 'nargs', 'elem_type', 'noload' } } ] }
|
|
49
|
+
#
|
|
50
|
+
# literal_strings semantics come from the registration-time value recorded
|
|
51
|
+
# in the tracking map, so a flag flip without a refresh does not silently
|
|
52
|
+
# change what is reported.
|
|
53
|
+
def python_input_contracts(task_name = nil)
|
|
54
|
+
contracts = {}
|
|
55
|
+
@python_task_registrations.to_a.each do |file, registration|
|
|
56
|
+
registration['metas'].each do |meta|
|
|
57
|
+
next if task_name && meta['name'] != task_name.to_s
|
|
58
|
+
contracts[meta['name']] = {
|
|
59
|
+
'file' => file,
|
|
60
|
+
'task' => meta['name'],
|
|
61
|
+
'returns' => PythonWorkflow.map_returns(meta['returns']),
|
|
62
|
+
'inputs' => meta['params'].to_a.map { |p| PythonWorkflow.input_contract(p, literal_strings: registration['literal_strings']) }
|
|
63
|
+
}
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
task_name ? contracts[task_name.to_s] : contracts
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def python_input_contract(task_name, input_name)
|
|
70
|
+
contract = python_input_contracts(task_name)
|
|
71
|
+
contract && contract['inputs'].find { |i| i['name'] == input_name.to_s }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Re-read the Python task files and reconcile the registered tasks:
|
|
75
|
+
#
|
|
76
|
+
# new Python function -> register it
|
|
77
|
+
# changed signature/metadata -> re-register it
|
|
78
|
+
# deleted Python function -> remove its registration
|
|
79
|
+
# unchanged definition -> left alone (the task object and cached
|
|
80
|
+
# job results survive; a body-only edit
|
|
81
|
+
# keeps the registration and executes the
|
|
82
|
+
# current source on a fresh run)
|
|
83
|
+
# name collision, incl. with a native (Ruby-defined) task
|
|
84
|
+
# -> raise, nothing changed
|
|
85
|
+
#
|
|
86
|
+
# Ordering: collect -> validate the COMPLETE desired state -> apply. One
|
|
87
|
+
# batch metadata subprocess reads every *.py file; every added/changed
|
|
88
|
+
# metadata set is validated (shape, duplicates, cross-file names, native
|
|
89
|
+
# collisions) BEFORE the first mutation, so a failed refresh raises with
|
|
90
|
+
# the registry and tracking map exactly as they were. Returns a summary
|
|
91
|
+
# hash: { :added => [...], :changed => [...], :removed => [...],
|
|
92
|
+
# :unchanged => [...] }. Safe to call repeatedly: with no source changes
|
|
93
|
+
# the result is all-unchanged.
|
|
94
|
+
def refresh_python_tasks!
|
|
95
|
+
names = python_task_dir.glob_names('*.py')
|
|
96
|
+
files = names.map { |name| Open.realpath(python_task_dir[name].find_with_extension('py')) }
|
|
97
|
+
metadata_by_file = files.empty? ? {} : PythonWorkflow.read_python_metadata_files(files)
|
|
98
|
+
|
|
99
|
+
previous = @python_task_registrations || {}
|
|
100
|
+
current_config = python_literal_strings
|
|
101
|
+
|
|
102
|
+
# Name -> [file, meta, literal_strings] of every registered task.
|
|
103
|
+
previous_by_name = {}
|
|
104
|
+
previous.each do |other_file, registration|
|
|
105
|
+
registration['metas'].each do |meta|
|
|
106
|
+
previous_by_name[meta['name']] = [other_file, meta, registration['literal_strings']]
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
# Name -> file across the whole directory: a task that moved between
|
|
111
|
+
# files is one change against the same name, not an add/remove pair, and
|
|
112
|
+
# one name exported by two files is a validation error.
|
|
113
|
+
new_names = {}
|
|
114
|
+
metadata_by_file.each do |file, metas|
|
|
115
|
+
metas.each { |meta| new_names[meta['name']] = file }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
decisions = []
|
|
119
|
+
metadata_by_file.each do |file, metas|
|
|
120
|
+
metas.each do |meta|
|
|
121
|
+
prior = previous_by_name[meta['name']]
|
|
122
|
+
kind =
|
|
123
|
+
if prior.nil?
|
|
124
|
+
:added
|
|
125
|
+
elsif prior[0] != file || prior[1] != meta || prior[2] != current_config
|
|
126
|
+
:changed
|
|
127
|
+
else
|
|
128
|
+
:unchanged
|
|
129
|
+
end
|
|
130
|
+
decisions << [kind, meta['name'], file, meta]
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Every previously registered task the directory no longer exports
|
|
135
|
+
# anywhere is a removal.
|
|
136
|
+
removed_keys = []
|
|
137
|
+
previous.each do |file, registration|
|
|
138
|
+
registration['metas'].each do |meta|
|
|
139
|
+
next if new_names.key?(meta['name'])
|
|
140
|
+
removed_keys << [file, meta['name']]
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Validate the COMPLETE desired state before the first mutation.
|
|
145
|
+
pending = {}
|
|
146
|
+
decisions.each do |kind, _name, file, meta|
|
|
147
|
+
next unless %i[added changed].include?(kind)
|
|
148
|
+
(pending[file] ||= []) << meta
|
|
149
|
+
end
|
|
150
|
+
validate_refresh_metadata!(metadata_by_file, pending)
|
|
151
|
+
|
|
152
|
+
registrations = {}
|
|
153
|
+
|
|
154
|
+
summary = { added: [], changed: [], removed: [], unchanged: [] }
|
|
155
|
+
|
|
156
|
+
# Apply. Removals first, so a freed name can be re-homed. Then the
|
|
157
|
+
# tracking map becomes the complete desired state — a plain file ->
|
|
158
|
+
# {metas, literal_strings} map — so per-file validation during apply
|
|
159
|
+
# sees the desired ownership (a task that moved files is owned by its
|
|
160
|
+
# new file). Then ONLY added/changed tasks re-register: unchanged tasks
|
|
161
|
+
# keep their task object and their cached job results.
|
|
162
|
+
removed_keys.each do |_file, name|
|
|
163
|
+
Log.debug "Removing python task #{name}"
|
|
164
|
+
tasks.delete(name.to_sym)
|
|
165
|
+
summary[:removed] << name
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
decisions.each { |kind, name, _file, _meta| summary[kind] << name }
|
|
169
|
+
|
|
170
|
+
# The tracking map becomes the complete desired state — a plain file ->
|
|
171
|
+
# {metas, literal_strings} map. The task objects of unchanged tasks are
|
|
172
|
+
# not replaced (their cached job results keep replaying), so a changed
|
|
173
|
+
# task is re-registered only AFTER its file's record exists, and a task
|
|
174
|
+
# that moved between files is owned by its new file.
|
|
175
|
+
decisions.group_by { |_kind, _name, file, _meta| file }.each do |file, entries|
|
|
176
|
+
metas = entries.map(&:last)
|
|
177
|
+
registrations[File.expand_path(file)] = { 'literal_strings' => current_config, 'metas' => metas }
|
|
178
|
+
|
|
179
|
+
pending = entries.select { |kind, _n, _f, _m| %i[added changed].include?(kind) }.map(&:last)
|
|
180
|
+
self.python_task(file: file, metadata: pending) unless pending.empty?
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
@python_task_registrations = registrations
|
|
184
|
+
summary
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# Validate the complete desired registration before any mutation: every
|
|
188
|
+
# file's metadata passes strict registration validation, no name is
|
|
189
|
+
# exported twice across the directory, and no added/changed name collides
|
|
190
|
+
# with a native task. Raises with nothing changed on any failure.
|
|
191
|
+
def validate_refresh_metadata!(metadata_by_file, pending)
|
|
192
|
+
desired = {}
|
|
193
|
+
metadata_by_file.each do |file, metas|
|
|
194
|
+
expanded = File.expand_path(file)
|
|
195
|
+
metas.each do |meta|
|
|
196
|
+
name = meta['name'].to_s
|
|
197
|
+
owner = desired[name]
|
|
198
|
+
raise "Task #{name} exported by both #{owner} and #{expanded}" if owner && owner != expanded
|
|
199
|
+
desired[name] = expanded
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# Shape, within-file duplicates and native collisions; cross-file
|
|
204
|
+
# ownership is decided against the desired map above, so the per-file
|
|
205
|
+
# cross-file check is skipped here.
|
|
206
|
+
pending.each do |file, metas|
|
|
207
|
+
validate_python_task_metadata(file, metas, skip_cross_file: true)
|
|
208
|
+
end
|
|
209
|
+
desired
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def load_python_tasks(batch: true)
|
|
213
|
+
names = python_task_dir.glob_names('*.py')
|
|
214
|
+
metadata = if batch && names.any?
|
|
215
|
+
files = names.map { |name| Open.realpath(python_task_dir[name].find_with_extension('py')) }
|
|
216
|
+
PythonWorkflow.read_python_metadata_files(files)
|
|
217
|
+
else
|
|
218
|
+
{}
|
|
219
|
+
end
|
|
220
|
+
names.each do |name|
|
|
221
|
+
task_name = name.sub '.py', ''
|
|
222
|
+
Log.debug "Loading python task #{task_name}"
|
|
223
|
+
file = Open.realpath(python_task_dir[task_name].find_with_extension('py'))
|
|
224
|
+
if batch
|
|
225
|
+
self.python_task task_name, file: file, metadata: metadata.fetch(file)
|
|
226
|
+
else
|
|
227
|
+
self.python_task task_name
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
def self.extended(workflow)
|
|
233
|
+
workflow.load_python_tasks(batch: true) if workflow.respond_to?(:load_python_tasks)
|
|
234
|
+
workflow
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
# literal_strings: initializes the flag before any task is loaded, so
|
|
238
|
+
# string inputs register with :noload from the start (the reviewer-preferred
|
|
239
|
+
# initialization option; the plain setter deliberately does not re-register
|
|
240
|
+
# anything). No new configuration surface beyond the existing flag.
|
|
241
|
+
def self.load_directory(path = nil, workflow_name = nil, batch: true, literal_strings: nil)
|
|
16
242
|
workflow = begin
|
|
17
243
|
m = Module.new
|
|
18
244
|
m.extend Workflow
|
|
@@ -22,15 +248,16 @@ module PythonWorkflow
|
|
|
22
248
|
m
|
|
23
249
|
end
|
|
24
250
|
|
|
25
|
-
Kernel.const_set
|
|
251
|
+
Kernel.const_set(workflow_name || "PythonWorkflow", workflow)
|
|
26
252
|
|
|
27
|
-
path =
|
|
253
|
+
path = workflow.libdir.python.tasks.find if path.nil?
|
|
28
254
|
|
|
29
|
-
workflow.
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
255
|
+
workflow.python_literal_strings = literal_strings unless literal_strings.nil?
|
|
256
|
+
|
|
257
|
+
# Set directly so load_directory's batch option remains authoritative and
|
|
258
|
+
# directory tasks are registered exactly once.
|
|
259
|
+
workflow.instance_variable_set(:@python_task_dir, path)
|
|
260
|
+
workflow.load_python_tasks(batch: batch)
|
|
34
261
|
|
|
35
262
|
workflow
|
|
36
263
|
end
|