polyrun-legacy 0.1.0

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 (37) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +8 -0
  3. data/CODE_OF_CONDUCT.md +26 -0
  4. data/CONTRIBUTING.md +23 -0
  5. data/LICENSE +21 -0
  6. data/README.md +115 -0
  7. data/SECURITY.md +25 -0
  8. data/bin/polyrun +5 -0
  9. data/lib/polyrun/legacy/cli.rb +102 -0
  10. data/lib/polyrun/legacy/compat.rb +53 -0
  11. data/lib/polyrun/legacy/config_commands.rb +52 -0
  12. data/lib/polyrun/legacy/configuration.rb +115 -0
  13. data/lib/polyrun/legacy/coverage_formats.rb +95 -0
  14. data/lib/polyrun/legacy/coverage_reports.rb +74 -0
  15. data/lib/polyrun/legacy/failure_reports.rb +100 -0
  16. data/lib/polyrun/legacy/junit_reports.rb +115 -0
  17. data/lib/polyrun/legacy/lifecycle.rb +95 -0
  18. data/lib/polyrun/legacy/merge.rb +139 -0
  19. data/lib/polyrun/legacy/options.rb +48 -0
  20. data/lib/polyrun/legacy/partition.rb +22 -0
  21. data/lib/polyrun/legacy/partition_plan.rb +105 -0
  22. data/lib/polyrun/legacy/paths.rb +59 -0
  23. data/lib/polyrun/legacy/planning_commands.rb +104 -0
  24. data/lib/polyrun/legacy/process_launch.rb +78 -0
  25. data/lib/polyrun/legacy/report_commands.rb +61 -0
  26. data/lib/polyrun/legacy/run_shards.rb +59 -0
  27. data/lib/polyrun/legacy/shard_commands.rb +132 -0
  28. data/lib/polyrun/legacy/shard_env.rb +34 -0
  29. data/lib/polyrun/legacy/shard_heap.rb +35 -0
  30. data/lib/polyrun/legacy/table.rb +54 -0
  31. data/lib/polyrun/legacy/timings.rb +103 -0
  32. data/lib/polyrun/legacy/version.rb +5 -0
  33. data/lib/polyrun/legacy/worker_wait.rb +80 -0
  34. data/lib/polyrun/legacy/yaml_data.rb +116 -0
  35. data/lib/polyrun/legacy.rb +7 -0
  36. data/polyrun-legacy.gemspec +44 -0
  37. metadata +91 -0
@@ -0,0 +1,74 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module CoverageReports
4
+ DEFAULT_GLOB = "coverage/polyrun-fragment-*.json"
5
+ DEFAULT_OUTPUT = "coverage/polyrun-merged.json"
6
+
7
+ module_function
8
+
9
+ def merge(argv)
10
+ defaults = {:output => DEFAULT_OUTPUT, :format => "json,console"} # rubocop:disable Style/HashSyntax
11
+ settings = ReportCommands.arguments(argv, "merge-coverage", defaults)
12
+ return 0 unless settings
13
+
14
+ settings[:inputs] << DEFAULT_GLOB if settings[:inputs].empty?
15
+ merge_and_write(Options.files(settings[:inputs]), settings[:output], settings[:format])
16
+ end
17
+
18
+ def after_shards(output, format_name)
19
+ paths = Dir.glob(DEFAULT_GLOB).sort.select { |path| File.file?(path) }
20
+ return 0 if paths.empty?
21
+
22
+ format_name ||= Options.env_value("POLYRUN_MERGE_FORMATS", "json,console")
23
+ merge_and_write(paths, output || DEFAULT_OUTPUT, format_name)
24
+ end
25
+
26
+ def merge_and_write(paths, output, format_name)
27
+ coverage = Merge.merge_files(paths)
28
+ CoverageFormats.names(format_name).each do |name|
29
+ content = CoverageFormats.render(coverage, name)
30
+ if name == "console"
31
+ print content
32
+ else
33
+ destination = (name == "json") ? output : output.sub(/\.json\z/, "") + CoverageFormats::EXTENSIONS.fetch(name)
34
+ destination += CoverageFormats::EXTENSIONS.fetch(name) if name != "json" && destination == output
35
+ Compat.write_report(destination, content)
36
+ end
37
+ end
38
+ 0
39
+ end
40
+
41
+ def report(argv)
42
+ defaults = {
43
+ :output => "coverage/polyrun", :basename => "polyrun-coverage", :format => CoverageFormats::NAMES.join(",") # rubocop:disable Style/HashSyntax
44
+ }
45
+ settings = ReportCommands.arguments(argv, "report-coverage", defaults) do |flags, options|
46
+ flags.on("--basename NAME", "Report file basename") { |value| options[:basename] = value }
47
+ end
48
+ return 0 unless settings
49
+
50
+ path = ReportCommands.inputs(settings)
51
+ raise ArgumentError, "provide one coverage file" unless path.size == 1
52
+
53
+ coverage = Merge.merge_files(path)
54
+ puts JSON.generate(write_all(coverage, settings))
55
+ 0
56
+ end
57
+
58
+ def write_all(coverage, settings)
59
+ basename = settings[:basename]
60
+ if basename.empty? || basename != File.basename(basename) || %w[. ..].include?(basename)
61
+ raise ArgumentError, "basename must be a file name"
62
+ end
63
+
64
+ results = {}
65
+ CoverageFormats.names(settings[:format]).each do |name|
66
+ destination = File.join(settings[:output], basename + CoverageFormats::EXTENSIONS.fetch(name))
67
+ Compat.write_report(destination, CoverageFormats.render(coverage, name))
68
+ results[name] = destination
69
+ end
70
+ results
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,100 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module FailureReports
4
+ DIRECTORY = "tmp/polyrun_failures"
5
+ FRAGMENTS = "polyrun-failure-fragment-*.jsonl"
6
+ HEADERS = %w[id full_description location file_path line_number message exception_class polyrun_shard_index polyrun_shard_total source].freeze
7
+ EXTENSIONS = {"jsonl" => "jsonl", "json" => "json", "csv" => "csv", "markdown" => "md"}.freeze
8
+
9
+ module_function
10
+
11
+ def command(argv)
12
+ settings = ReportCommands.arguments(argv, "merge-failures", {:format => "jsonl"}) # rubocop:disable Style/HashSyntax
13
+ return 0 unless settings
14
+
15
+ merge(ReportCommands.inputs(settings), settings[:output], settings[:format])
16
+ end
17
+
18
+ def after_shards(settings)
19
+ directory = Options.env_value("POLYRUN_FAILURE_FRAGMENT_DIR", DIRECTORY)
20
+ paths = Dir.glob(File.join(directory, FRAGMENTS)).sort.select { |path| File.file?(path) }
21
+ return 0 if paths.empty?
22
+
23
+ output = settings[:failures_output] || ENV["POLYRUN_MERGED_FAILURES_OUT"]
24
+ merge(paths, output, settings[:failures_format] || "jsonl")
25
+ end
26
+
27
+ def merge(paths, output, format_name)
28
+ rows = []
29
+ Compat.load_json
30
+ paths.each { |path| rows.concat(read(path)) }
31
+ extension = EXTENSIONS[format_name]
32
+ raise ArgumentError, "choose jsonl, json, csv, or markdown for --format" unless extension
33
+
34
+ output ||= File.join(DIRECTORY, "merged.#{extension}")
35
+ ReportCommands.write(output, render(paths, rows, format_name))
36
+ end
37
+
38
+ def read(path)
39
+ return read_jsonl(path) if File.extname(path).downcase == ".jsonl"
40
+
41
+ document = Compat.read_json(path)
42
+ unless document.is_a?(Hash) && document["examples"].is_a?(Array)
43
+ raise ArgumentError, "#{path}: expected an RSpec examples array"
44
+ end
45
+
46
+ failed = document["examples"].select { |example| example.is_a?(Hash) && example["status"] == "failed" }
47
+ failed.map { |example| failed_example(example) }
48
+ end
49
+
50
+ def read_jsonl(path)
51
+ rows = []
52
+ line_number = 0
53
+ File.foreach(path) do |line|
54
+ line_number += 1
55
+ next if line.strip.empty?
56
+
57
+ row = Compat.parse_json(line)
58
+ raise ArgumentError, "#{path}: expected an object at line #{line_number}" unless row.is_a?(Hash)
59
+
60
+ rows << row
61
+ end
62
+ rows
63
+ rescue JSON::ParserError
64
+ raise ArgumentError, "#{path}: invalid JSON at line #{line_number}"
65
+ end
66
+
67
+ def failed_example(example)
68
+ exception = example["exception"]
69
+ exception = {} unless exception.is_a?(Hash)
70
+
71
+ row = {}
72
+ %w[id full_description file_path line_number].each { |key| row[key] = example[key] if example[key] }
73
+ if row["file_path"] && row["line_number"]
74
+ row["location"] = "#{row["file_path"]}:#{row["line_number"]}"
75
+ elsif row["full_description"]
76
+ row["location"] = row["full_description"]
77
+ end
78
+ message = exception["message"] || row["full_description"]
79
+ row["message"] = message if message
80
+ row["exception_class"] = exception["class"] if exception["class"]
81
+ row["source"] = "rspec_json"
82
+ row
83
+ end
84
+
85
+ def render(paths, rows, format_name)
86
+ case format_name
87
+ when "json"
88
+ meta = {"polyrun_merge" => true, "inputs" => paths.map { |path| File.expand_path(path) }, "failure_count" => rows.size}
89
+ JSON.generate({"meta" => meta, "failures" => rows}) + "\n"
90
+ when "jsonl"
91
+ rows.map { |row| JSON.generate(row) + "\n" }.join
92
+ when "csv"
93
+ Table.csv(HEADERS, rows.map { |row| row.values_at(*HEADERS) })
94
+ when "markdown"
95
+ Table.markdown("Polyrun failure report", HEADERS, rows.map { |row| row.values_at(*HEADERS) })
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,115 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module JunitReports
4
+ EXTENSIONS = {"xml" => "xml", "csv" => "csv", "markdown" => "md", "md" => "md"}.freeze
5
+ HEADERS = %w[classname name file line status time message].freeze
6
+
7
+ module_function
8
+
9
+ def command(argv)
10
+ settings = ReportCommands.arguments(argv, "report-junit", {:format => "xml"}) # rubocop:disable Style/HashSyntax
11
+ return 0 unless settings
12
+
13
+ paths = ReportCommands.inputs(settings)
14
+ extension = EXTENSIONS[settings[:format]]
15
+ raise ArgumentError, "choose xml, csv, or markdown for --format" unless extension
16
+
17
+ documents = paths.map { |path| read(path) }
18
+ name, cases = documents.first
19
+ if documents.length > 1
20
+ name = "Polyrun"
21
+ cases = []
22
+ documents.each { |document| cases.concat(document.last) }
23
+ end
24
+ output = settings[:output] || File.join(File.dirname(paths.first), "junit.#{extension}")
25
+ ReportCommands.write(output, render(name, cases, extension))
26
+ end
27
+
28
+ def read(path)
29
+ document = Compat.read_json(path)
30
+ raise ArgumentError, "#{path}: expected a test report object" unless document.is_a?(Hash)
31
+
32
+ if document["testcases"].is_a?(Array)
33
+ name = document["name"] || document["testsuite_name"] || "tests"
34
+ return [name, document["testcases"].select { |testcase| testcase.is_a?(Hash) }]
35
+ end
36
+ if document["examples"].is_a?(Array)
37
+ summary = document["summary"]
38
+ summary = {} unless summary.is_a?(Hash)
39
+ return [summary["summary_line"] || document["name"] || "RSpec", rspec_cases(document["examples"])]
40
+ end
41
+
42
+ raise ArgumentError, "#{path}: expected an examples or testcases array"
43
+ end
44
+
45
+ def rspec_cases(examples)
46
+ rows = []
47
+ examples.each do |example|
48
+ next unless example.is_a?(Hash)
49
+
50
+ file = example["file_path"].to_s.sub(%r{\A\./}, "")
51
+ exception = example["exception"]
52
+ exception = {} unless exception.is_a?(Hash)
53
+ rows << {
54
+ "classname" => file.empty? ? "rspec" : file, "file" => file, "line" => example["line_number"],
55
+ "name" => example["full_description"] || example["description"] || example["id"],
56
+ "status" => example["status"], "time" => example["run_time"] || example["time"],
57
+ "failure" => {"message" => exception["message"], "body" => Array(exception["backtrace"]).join("\n")}
58
+ }
59
+ end
60
+ rows
61
+ end
62
+
63
+ def render(name, cases, extension)
64
+ return xml(name, cases) if extension == "xml"
65
+
66
+ rows = cases.map { |testcase| testcase.values_at(*HEADERS.take(6)) + [failure(testcase)["message"]] }
67
+ (extension == "csv") ? Table.csv(HEADERS, rows) : Table.markdown(name, HEADERS, rows)
68
+ end
69
+
70
+ def xml(name, cases)
71
+ output = %(<?xml version="1.0" encoding="UTF-8"?>\n)
72
+ output << %(<testsuites name="#{Table.xml(name)}"><testsuite name="#{Table.xml(name)}" #{suite_stats(cases)}>\n)
73
+ cases.each { |testcase| output << testcase_xml(testcase) }
74
+ output << "</testsuite></testsuites>\n"
75
+ end
76
+
77
+ def suite_stats(cases)
78
+ counters = {"failed" => 0, "error" => 0, "skipped" => 0}
79
+ total_time = 0.0
80
+ cases.each do |testcase|
81
+ key = status(testcase)
82
+ counters[key] += 1 if counters.key?(key)
83
+ total_time += testcase["time"].to_f
84
+ end
85
+ %(tests="#{cases.size}" failures="#{counters["failed"]}" errors="#{counters["error"]}" ) +
86
+ %(skipped="#{counters["skipped"]}" time="#{format("%.6f", total_time)}")
87
+ end
88
+
89
+ def testcase_xml(testcase)
90
+ fields = %w[classname name file line].map { |key| %(#{key}="#{Table.xml(testcase[key])}") }
91
+ output = %(<testcase #{fields.join(" ")} time="#{format("%.6f", testcase["time"].to_f)}">)
92
+ case status(testcase)
93
+ when "failed" then output << failure_xml(testcase, "failure")
94
+ when "error" then output << failure_xml(testcase, "error")
95
+ when "skipped" then output << "<skipped/>"
96
+ end
97
+ output << "</testcase>\n"
98
+ end
99
+
100
+ def status(testcase)
101
+ (testcase["status"] == "pending") ? "skipped" : testcase["status"]
102
+ end
103
+
104
+ def failure(testcase)
105
+ value = testcase["failure"]
106
+ value.is_a?(Hash) ? value : {}
107
+ end
108
+
109
+ def failure_xml(testcase, tag)
110
+ details = failure(testcase)
111
+ %(<#{tag} message="#{Table.xml(details["message"] || status(testcase))}">#{Table.xml(details["body"])}</#{tag}>)
112
+ end
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,95 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module Lifecycle
4
+ PHASES = %w[before_suite after_suite before_shard after_shard before_worker after_worker].freeze
5
+
6
+ module_function
7
+
8
+ def prepare(argv, config)
9
+ dry_run = false
10
+ parser = OptionParser.new do |flags|
11
+ flags.banner = "Usage: polyrun prepare [--dry-run]"
12
+ flags.on("--dry-run", "Display the planned commands") { dry_run = true }
13
+ end
14
+ return 0 unless Options.parse(parser, argv)
15
+ raise ArgumentError, parser.banner unless argv.empty?
16
+
17
+ prepare_steps(config, dry_run, true)
18
+ end
19
+
20
+ def prepare_steps(config, dry_run, report)
21
+ settings = config.section("prepare")
22
+ recipe = settings["recipe"] || "default"
23
+ directory = File.expand_path(settings["rails_root"] || ".")
24
+ actions = prepare_actions(settings, recipe)
25
+ environment = settings.fetch("env", {})
26
+ raise ArgumentError, "prepare.env must be a mapping" unless environment.is_a?(Hash)
27
+
28
+ Compat.load_json if report
29
+ status = dry_run ? 0 : run_steps(actions, environment, directory, true)
30
+ if status == 0 && report
31
+ puts JSON.generate({"recipe" => recipe, "dry_run" => dry_run, "executed" => !dry_run, "actions" => actions})
32
+ end
33
+ status
34
+ end
35
+
36
+ def prepare_actions(settings, recipe)
37
+ case recipe
38
+ when "default" then []
39
+ when "shell"
40
+ steps = scripts(settings["commands"]) + scripts(settings["command"])
41
+ raise ArgumentError, "shell preparation requires a command" if steps.empty?
42
+
43
+ steps
44
+ when "assets"
45
+ steps = scripts(settings["command"])
46
+ steps.empty? ? ["bin/rails assets:precompile"] : steps
47
+ else
48
+ raise ArgumentError, "unknown prepare recipe #{recipe.inspect}"
49
+ end
50
+ end
51
+
52
+ def hook(argv, config)
53
+ phase = nil
54
+ index, total = ShardEnv.defaults(config.partition)
55
+ parser = OptionParser.new do |flags|
56
+ flags.banner = "Usage: polyrun hook run PHASE [--shard INDEX] [--total N]"
57
+ flags.on("--shard INDEX", "Zero-based shard index") { |value| index = value }
58
+ flags.on("--total N", "Number of shards") { |value| total = value }
59
+ end
60
+ return 0 unless Options.parse(parser, argv)
61
+
62
+ phase = argv.last if argv.length == 2 && argv.first == "run"
63
+ raise ArgumentError, parser.banner unless PHASES.include?(phase)
64
+
65
+ environment = ShardEnv.for_shard(Options.integer(index, "shard", 0), Options.integer(total, "total", 1))
66
+ environment.merge!("POLYRUN_HOOK" => "1", "POLYRUN_HOOK_PHASE" => phase, "POLYRUN_HOOK_CLI" => "1", "POLYRUN_HOOK_ORCHESTRATOR" => "1")
67
+ run_steps(scripts(config.section("hooks")[phase]), environment, nil, false)
68
+ end
69
+
70
+ def scripts(value)
71
+ Array(value).map do |step|
72
+ raise ArgumentError, "shell commands must be strings" unless step.is_a?(String)
73
+
74
+ step.strip
75
+ end.reject { |step| step.empty? }
76
+ end
77
+
78
+ def run_steps(actions, environment, directory, report)
79
+ children = []
80
+ options = {:chdir => directory, :report => report, :launch_failure_status => 2} # rubocop:disable Style/HashSyntax
81
+ actions.each do |script|
82
+ pid, failure = ProcessLaunch.start(environment, ["sh", "-c", script], options)
83
+ return failure if failure
84
+
85
+ children << [pid, 0, nil]
86
+ status = WorkerWait.wait(children, nil)
87
+ return status unless status == 0
88
+ end
89
+ 0
90
+ ensure
91
+ WorkerWait.stop(children)
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,139 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module Merge
4
+ module_function
5
+
6
+ def merge_files(paths)
7
+ Compat.load_json
8
+ merged = {}
9
+ paths.each do |path|
10
+ coverage_tables(path).each do |coverage|
11
+ coverage.each do |file, entry|
12
+ target = merged[file] ||= {"lines" => []}
13
+ merge_entry(target, entry, "#{path}: #{file}")
14
+ end
15
+ end
16
+ end
17
+ merged
18
+ end
19
+
20
+ def coverage_tables(path)
21
+ contents = File.open(path, "rb") { |input| input.read } # rubocop:disable Style/FileRead
22
+ document = Compat.parse_json(contents)
23
+ tables = []
24
+ if document.is_a?(Hash)
25
+ tables << document["coverage"] if document["coverage"].is_a?(Hash)
26
+ document.each do |name, suite|
27
+ next if name == "coverage" || !suite.is_a?(Hash)
28
+
29
+ tables << suite["coverage"] if suite["coverage"].is_a?(Hash)
30
+ end
31
+ end
32
+ raise ArgumentError, "#{path}: expected a coverage object" if tables.empty?
33
+
34
+ tables
35
+ rescue JSON::ParserError
36
+ raise ArgumentError, "#{path}: invalid coverage JSON"
37
+ end
38
+
39
+ def merge_entry(target, entry, source)
40
+ entry = {"lines" => entry} if entry.is_a?(Array)
41
+ if !entry.is_a?(Hash) || (!entry.key?("lines") && !entry.key?("branches"))
42
+ raise ArgumentError, "#{source}: expected line or branch coverage"
43
+ end
44
+
45
+ lines = entry["lines"]
46
+ unless lines.nil? || lines.is_a?(Array)
47
+ raise ArgumentError, "#{source}: expected a line coverage array"
48
+ end
49
+
50
+ (lines || []).each_with_index do |hits, index|
51
+ unless hits.nil? || hits == "ignored" || hit_count?(hits)
52
+ raise ArgumentError, "#{source}: invalid count at line #{index + 1}"
53
+ end
54
+
55
+ previous = target["lines"][index]
56
+ target["lines"][index] = merge_hits(previous, hits)
57
+ end
58
+ merge_branches(target, entry["branches"])
59
+ end
60
+
61
+ def merge_hits(previous, hits)
62
+ return "ignored" if previous == "ignored" || hits == "ignored"
63
+ return previous if hits.nil?
64
+ return hits if previous.nil?
65
+
66
+ previous + hits
67
+ end
68
+
69
+ def hit_count?(hits)
70
+ hits.is_a?(Integer) && hits >= 0
71
+ end
72
+
73
+ def merge_branches(target, incoming)
74
+ case incoming
75
+ when Hash
76
+ branches = target["branches"] ||= {}
77
+ merge_branch_map(branches, incoming) if branches.is_a?(Hash)
78
+ when Array
79
+ branches = target["branches"] ||= []
80
+ merge_branch_records(branches, incoming) if branches.is_a?(Array)
81
+ end
82
+ end
83
+
84
+ def merge_branch_map(branches, incoming)
85
+ incoming.each do |key, count|
86
+ if count.is_a?(Hash)
87
+ nested = branches[key] ||= {}
88
+ count.each { |arm, hits| add_branch_hits(nested, arm, hits) } if nested.is_a?(Hash)
89
+ else
90
+ add_branch_hits(branches, key, count)
91
+ end
92
+ end
93
+ end
94
+
95
+ def add_branch_hits(counts, key, hits)
96
+ return unless hit_count?(hits)
97
+
98
+ previous = counts[key]
99
+ counts[key] = (previous || 0) + hits unless previous.is_a?(Hash)
100
+ end
101
+
102
+ def merge_branch_records(branches, incoming)
103
+ indexed = {}
104
+ branches.each { |branch| indexed[branch_key(branch)] = branch }
105
+ incoming.each do |branch|
106
+ next unless branch.is_a?(Hash) && hit_count?(branch["coverage"])
107
+
108
+ key = branch_key(branch)
109
+ next if key.include?(nil)
110
+
111
+ if indexed.key?(key)
112
+ indexed[key]["coverage"] += branch["coverage"]
113
+ else
114
+ indexed[key] = branch.dup
115
+ branches << indexed[key]
116
+ end
117
+ end
118
+ end
119
+
120
+ def branch_key(branch)
121
+ branch.values_at("type", "start_line", "end_line")
122
+ end
123
+
124
+ def console_summary(coverage)
125
+ covered = total = 0
126
+ coverage.each_value do |entry|
127
+ entry["lines"].each do |hits|
128
+ next unless hit_count?(hits)
129
+
130
+ total += 1
131
+ covered += 1 if hits > 0
132
+ end
133
+ end
134
+ percent = (total == 0) ? 100.0 : covered * 100.0 / total
135
+ format("Polyrun coverage: %d/%d lines (%.2f%%)", covered, total, percent)
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,48 @@
1
+ require "optparse"
2
+
3
+ module Polyrun
4
+ module Legacy
5
+ module Options
6
+ module_function
7
+
8
+ def split_command(argv)
9
+ separator = argv.index("--")
10
+ separator ? [argv.take(separator), argv.drop(separator + 1)] : [argv, nil]
11
+ end
12
+
13
+ def parse(parser, argv)
14
+ if argv == ["--help"] || argv == ["-h"]
15
+ puts parser
16
+ return false
17
+ end
18
+
19
+ parser.parse!(argv)
20
+ true
21
+ end
22
+
23
+ def integer(value, label, minimum)
24
+ return value.to_i if value.to_s =~ /\A[0-9]+\z/ && value.to_i >= minimum
25
+
26
+ requirement = (minimum == 1) ? "a positive integer" : "a nonnegative integer"
27
+ raise ArgumentError, "#{label} must be #{requirement}"
28
+ end
29
+
30
+ def env_value(name, fallback)
31
+ value = ENV[name]
32
+ value.to_s.empty? ? fallback : value
33
+ end
34
+
35
+ def files(patterns)
36
+ paths = []
37
+ patterns.each do |pattern|
38
+ path = File.expand_path(pattern)
39
+ matches = File.file?(path) ? [path] : Dir.glob(path).sort.select { |match| File.file?(match) }
40
+ raise ArgumentError, "no files match #{pattern.inspect}" if matches.empty?
41
+
42
+ paths.concat(matches)
43
+ end
44
+ paths.uniq
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,22 @@
1
+ module Polyrun
2
+ module Legacy
3
+ module Partition
4
+ module_function
5
+
6
+ def round_robin(paths, workers)
7
+ distribute(paths.sort, workers)
8
+ end
9
+
10
+ def distribute(paths, workers)
11
+ unless workers.is_a?(Integer) && workers > 0
12
+ raise ArgumentError, "workers must be a positive integer"
13
+ end
14
+
15
+ count = paths.empty? ? workers : [workers, paths.length].min
16
+ shards = Array.new(count) { [] }
17
+ paths.each_with_index { |path, index| shards[index % workers] << path }
18
+ shards
19
+ end
20
+ end
21
+ end
22
+ end