-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresolve_pre_commit_python.rb
More file actions
144 lines (121 loc) · 3.59 KB
/
resolve_pre_commit_python.rb
File metadata and controls
144 lines (121 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require "yaml"
def detect_config_path
[".pre-commit-config.yaml", ".pre-commit-config.yml"].find { |path| File.exist?(path) }
end
def normalize_python_version(raw_value)
return nil if raw_value.nil?
value = raw_value.to_s.strip
return nil if value.empty?
case value.downcase
when "default", "system"
nil
when /\A\d+(?:\.\d+){0,2}\z/
value
when /\Apython(\d+(?:\.\d+){0,2})\z/i
Regexp.last_match(1)
when /\Apython(?:3)?\z/i
"3.x"
else
nil
end
end
def load_config(path)
YAML.safe_load(File.read(path), aliases: false) || {}
rescue Errno::ENOENT
warn "Failed to read #{path}: file not found"
exit 1
rescue Psych::Exception => error
warn "Failed to parse #{path}: #{error.message}"
exit 1
end
def collect_hook_python_versions(config)
repos = config.fetch("repos", [])
return [] unless repos.is_a?(Array)
repos.flat_map do |repo|
next [] unless repo.is_a?(Hash)
hooks = repo["hooks"]
next [] unless hooks.is_a?(Array)
hooks.filter_map do |hook|
next unless hook.is_a?(Hash)
raw_version = hook["language_version"]
normalized_version = normalize_python_version(raw_version)
next if normalized_version.nil?
{
"id" => hook["id"] || "<unknown>",
"raw_version" => raw_version.to_s,
"normalized_version" => normalized_version,
}
end
end
end
def emit_result(version:, source:, config_path:)
puts "python-version=#{version}"
puts "source=#{source}"
puts "config-path=#{config_path}"
end
default_python_version = ENV.fetch("DEFAULT_PYTHON_VERSION", "3.9")
input_python_version = ENV.fetch("INPUT_PYTHON_VERSION", "").strip
config_path = ARGV[0] || detect_config_path || ""
unless input_python_version.empty?
resolved_input_version = normalize_python_version(input_python_version) || input_python_version
emit_result(
version: resolved_input_version,
source: "workflow input",
config_path: config_path,
)
exit 0
end
if config_path.empty?
emit_result(
version: default_python_version,
source: "default fallback",
config_path: "",
)
exit 0
end
config = load_config(config_path)
unless config.is_a?(Hash)
warn "Invalid pre-commit config in #{config_path}: expected a top-level mapping"
exit 1
end
default_language_python_raw = config.dig("default_language_version", "python")
default_language_python = normalize_python_version(default_language_python_raw)
hook_python_versions = collect_hook_python_versions(config)
unique_hook_versions = hook_python_versions.map { |entry| entry["normalized_version"] }.uniq
if unique_hook_versions.length > 1
details = hook_python_versions.map do |entry|
"#{entry["id"]}=#{entry["raw_version"]}"
end.join(", ")
warn "Multiple Python language_version values found in #{config_path}: #{details}"
exit 1
end
if default_language_python && !unique_hook_versions.empty? && unique_hook_versions.first != default_language_python
warn(
"Conflicting Python versions found in #{config_path}: " \
"default_language_version.python=#{default_language_python_raw} " \
"and hook language_version=#{hook_python_versions.first["raw_version"]}"
)
exit 1
end
if default_language_python
emit_result(
version: default_language_python,
source: "default_language_version.python",
config_path: config_path,
)
exit 0
end
if unique_hook_versions.length == 1
emit_result(
version: unique_hook_versions.first,
source: "hook language_version",
config_path: config_path,
)
exit 0
end
emit_result(
version: default_python_version,
source: "default fallback",
config_path: config_path,
)