-
Notifications
You must be signed in to change notification settings - Fork 20
/
xml_checks.rb
183 lines (155 loc) · 6.5 KB
/
xml_checks.rb
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
178
179
180
181
182
183
# Copyright (c) [2020] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "erb"
require "yast"
require "autoinstall/xml_validator"
require "yast2/popup"
require "yaml"
Yast.import "AutoinstConfig"
Yast.import "Report"
module Y2Autoinstallation
# Validates an AutoYaST XML document and displays an error popup
# for not well formed or invalid documents. It is possible to continue
# anyway and ignore the found problems at your risk.
class XmlChecks
include Singleton
include Yast::Logger
include Yast::I18n
# paths to the default AutoYaST schema files
PROFILE_SCHEMA = "/usr/share/YaST2/schema/autoyast/rng/profile.rng".freeze
RULES_SCHEMA = "/usr/share/YaST2/schema/autoyast/rng/rules.rng".freeze
CLASSES_SCHEMA = "/usr/share/YaST2/schema/autoyast/rng/classes-use.rng".freeze
ERRORS_PATH = "/var/lib/YaST2/xml_checks_errors".freeze
# Constructor
def initialize
textdomain "autoinst"
@reported_errors = errors_from_file
end
# Is the file a valid AutoYaST XML profile?
# @param file [String] pahth to the classes file
# @return [Boolean] true if valid, false otherwise
def valid_profile?(file = Yast::AutoinstConfig.xml_tmpfile)
# TRANSLATORS: Error message
msg = _("The AutoYaST profile is not a valid XML document.")
check(file, PROFILE_SCHEMA, msg)
end
# Is the file a valid AutoYaST XML profile?
# @return [Boolean] true if valid, false otherwise
def valid_modified_profile?
# TRANSLATORS: Error message
msg = _("The AutoYaST pre-script generated an invalid XML document.")
check(Yast::AutoinstConfig.modified_profile, PROFILE_SCHEMA, msg)
end
# Is the file a valid AutoYaST rules file?
# @return [Boolean] true if valid, false otherwise
def valid_rules?
# TRANSLATORS: Error message
msg = _("The AutoYaST rules file is not a valid XML document.")
check(Yast::AutoinstConfig.local_rules_file, RULES_SCHEMA, msg)
end
# Is the file a valid AutoYaST class file?
# @param file [String] path to the classes file
# @return [Boolean] true if valid, false otherwise
def valid_classes?(file)
# TRANSLATORS: Error message
msg = _("The AutoYaST class file is not a valid XML document.")
check(file, RULES_SCHEMA, msg)
end
# generic validation check
# @param file [String] path to the XML file
# @param schema [String] path to the RNG schema
# @param msg [String] title displayed in the popup when validation fails
# @return [Boolean] true if valid, false otherwise
def check(file, schema, msg)
validator = XmlValidator.new(file, schema)
return true if validator.valid? || !new_errors?(validator.errors)
if ENV["YAST_SKIP_XML_VALIDATION"] == "1"
log.warn "Skipping invalid XML!"
return true
end
ret = Yast2::Popup.show(message(msg, validator.errors, file, schema),
richtext: true,
headline: :warning,
buttons: :continue_cancel,
timeout: Yast::Report.Export["warnings"]["timeout"],
focus: :cancel) == :continue
if ret
log.warn "Skipping invalid XML on user request and storing validation errors!"
store_errors(validator.errors)
end
ret
end
private
# Convenience method to check whether the list of validation errors have
# been already reported and accepted by the user or not.
#
# @param errors [Array<String>] list of validation errors
# @return [Boolean] whether the validation errors are new or not
def new_errors?(errors)
return true if (reported_errors || []).empty?
!reported_errors.include?(digest_hash(errors))
end
# Convenience method to save to a file the hash sum of the already
# reported errors
#
# @param errors [Array<String>] list of validation errors
def store_errors(errors)
@reported_errors << digest_hash(errors)
File.write(ERRORS_PATH, reported_errors.to_yaml)
end
# Convenience method to obtain the digest hash for the given errors
#
# @param errors [Array<String>] list of validation errors
def digest_hash(errors)
Digest::MD5.hexdigest(errors.to_yaml)
end
# Convenience method to load the errors reported from a file
def errors_from_file
return [] unless File.exist?(ERRORS_PATH)
YAML.safe_load(File.read(ERRORS_PATH))
end
# @return [Array<String>] array with the reported errors hash sums
def reported_errors
@reported_errors ||= []
end
# an internal helper for building the error message
# @param msg [String] title text
# @param errors [Array<String>] list of validation errors
# @param file [String] path to the XML file (only the base name is displayed)
# @param schema [String] path to the RNG schema
def message(msg, errors, file, schema)
xml_file = File.basename(file)
jing_command = "jing #{schema} #{xml_file}"
xmllint_command = "xmllint --noout --relaxng #{schema} #{xml_file}"
"<h3>" + msg + "</h3>" + "<p>" +
# TRANSLATORS: Warn user about using invalid XML
_("Using an invalid XML document might result in an unexpected behavior, " \
"crash or even data loss!") +
"</p><h4>" + _("Details") + "</h4><p>" +
ERB::Util.html_escape(errors.join("<br>")) + "</p><h4>" + _("Note") + "</h4>" +
# TRANSLATORS: A hint how to check a XML file, displayed as a part of the
# validation error message, %{jing} and %{xmllint} are replaced by shell commands,
# use HTML tags and entities (non-breaking space) for formatting the message
"<p>" + format(_("You can check the file manually with these commands:<br><br>" \
" %{jing}<br>" \
" %{xmllint}"), jing: jing_command, xmllint: xmllint_command) +
"</p>"
end
end
end