File size: 6,215 Bytes
edbf722
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env ruby
# frozen_string_literal: true

require "optparse"
require "yaml"

module FormalizationTemplate
  SENTINEL = /\ATEMPLATE(?::|\z)/
  REQUIRED_LICENSE = "Apache-2.0"
  REQUIRED_SECTIONS = %w[project classification automation review].freeze
  EXPECTED_TEMPLATE_PATHS = [
    "$.project.name",
    "$.project.description",
    "$.project.authors[0]",
    "$.project.responsible_maintainers[0]",
    "$.classification.arxiv[0]",
    "$.classification.msc2020[0]",
    "$.sources[0].title",
    "$.sources[0].authors[0]",
    "$.sources[0].contributors[0].name",
    "$.sources[0].contributors[0].role",
    "$.sources[0].id",
    "$.sources[0].type",
    "$.sources[0].location",
    "$.sources[0].relationship",
    "$.sources[0].note",
    "$.sources[0].license",
    "$.sources[0].author_endorsement",
    "$.related_formalizations[0].id",
    "$.related_formalizations[0].relationship",
    "$.related_formalizations[0].note",
    "$.status.scope",
    "$.status.sorry_count",
    "$.status.sorry_in_definitions",
    "$.status.axioms[0]",
    "$.automation.methods[0].method",
    "$.automation.methods[0].models[0]",
    "$.automation.methods[0].framework",
    "$.automation.methods[0].tool_setup",
    "$.automation.methods[0].cost.wall_time",
    "$.automation.methods[0].cost.spend_usd",
    "$.automation.methods[0].cost.hardware",
    "$.automation.methods[0].prompting_notes",
    "$.automation.spend_usd",
    "$.automation.notes",
    "$.fidelity.divergences",
    "$.review.status",
    "$.review.reviewers[0]",
    "$.review.notes",
    "$.alignment.namespace",
    "$.alignment.statements[0].source",
    "$.alignment.statements[0].lean",
    "$.alignment.statements[0].module",
    "$.alignment.statements[0].status",
    "$.alignment.statements[0].note",
    "$.acknowledgements"
  ].freeze

  class ValidationError < StandardError; end

  def self.load_document(path)
    text = File.binread(path).force_encoding(Encoding::UTF_8)
    raise ValidationError, "#{path} must be valid UTF-8" unless text.valid_encoding?

    document = YAML.safe_load(
      text,
      permitted_classes: [],
      permitted_symbols: [],
      aliases: false
    )
    raise ValidationError, "#{path} must contain one top-level mapping" unless document.is_a?(Hash)

    missing = REQUIRED_SECTIONS.reject { |section| document[section].is_a?(Hash) }
    unless missing.empty?
      raise ValidationError,
            "#{path} must contain the required mapping sections: #{missing.join(', ')}"
    end

    document
  rescue Psych::Exception => error
    detail = error.message.lines.first&.strip
    raise ValidationError, "cannot parse #{path} as YAML: #{detail}"
  rescue SystemCallError => error
    raise ValidationError, "cannot read #{path}: #{error.message}"
  end

  def self.placeholder_paths(value, path = "$")
    case value
    when Hash
      value.flat_map do |key, child|
        placeholder_paths(child, "#{path}.#{key}")
      end
    when Array
      value.each_with_index.flat_map do |child, index|
        placeholder_paths(child, "#{path}[#{index}]")
      end
    when String
      value.lstrip.match?(SENTINEL) ? [path] : []
    else
      []
    end
  end

  def self.validate(path, expect_template: false)
    document = load_document(path)
    unless document["version"] == "v0.4"
      raise ValidationError,
            "#{path} $.version must be \"v0.4\", not #{document["version"].inspect}"
    end
    actual_license = document.dig("project", "license")
    unless actual_license == REQUIRED_LICENSE
      raise ValidationError, <<~MESSAGE.chomp
        #{path} $.project.license must be #{REQUIRED_LICENSE.inspect}, not #{actual_license.inspect}.
        Keep the repository's Apache-2.0 LICENSE file unchanged and set project.license to #{REQUIRED_LICENSE.inspect}.
      MESSAGE
    end
    description = document.dig("project", "description")
    unless description.is_a?(String) && !description.strip.empty? && description.strip.length <= 10_000
      raise ValidationError,
            "#{path} $.project.description must be nonempty text of at most 10000 characters"
    end

    placeholders = placeholder_paths(document)
    if expect_template
      missing = EXPECTED_TEMPLATE_PATHS - placeholders
      unexpected = placeholders - EXPECTED_TEMPLATE_PATHS
      return placeholders if missing.empty? && unexpected.empty?

      details = []
      details << "missing expected values: #{missing.join(', ')}" unless missing.empty?
      details << "unexpected values: #{unexpected.join(', ')}" unless unexpected.empty?
      raise ValidationError,
            "#{path} does not have the expected TEMPLATE sentinel surface; #{details.join('; ')}"
    end

    return placeholders if placeholders.empty?

    locations = placeholders.map { |item| "  #{item}" }.join("\n")
    raise ValidationError, <<~MESSAGE.chomp
      #{path} still contains #{placeholders.length} TEMPLATE value(s):
      #{locations}
      Replace every listed value with project-specific metadata; use [] for a placeholder list where the honest answer is none.
    MESSAGE
  end

  def self.run_cli(arguments, output: $stdout, errors: $stderr)
    expect_template = false
    parser = OptionParser.new do |options|
      options.banner = "Usage: #{File.basename($PROGRAM_NAME)} [--expect-template] [formalization.yaml]"
      options.on(
        "--expect-template",
        "require the canonical template's exact TEMPLATE sentinel surface"
      ) { expect_template = true }
    end
    remaining = parser.parse(arguments)
    raise OptionParser::InvalidArgument, "expected at most one metadata path" if remaining.length > 1

    path = remaining.fetch(0, "formalization.yaml")
    placeholders = validate(path, expect_template: expect_template)
    if expect_template
      output.puts "#{path} contains the expected #{placeholders.length} TEMPLATE values"
    else
      output.puts "#{path} contains no TEMPLATE values"
    end
    0
  rescue OptionParser::ParseError => error
    errors.puts error.message
    errors.puts parser
    2
  rescue ValidationError => error
    errors.puts error.message
    1
  end
end

exit FormalizationTemplate.run_cli(ARGV) if $PROGRAM_NAME == __FILE__