Merge remote-tracking branch 'origin/stable' into devel
[tails.git] / bin / sanity-check-website
blob131d23b898db0ed04a204118abc98d2c96e6072e
1 #!/usr/bin/env ruby
2 require 'date'
3 require 'English'
5 require 'test/unit'
6 Test::Unit.run = true
7 # Make all the assert_* methods easily accessible.
8 include Test::Unit::Assertions # rubocop:disable Style/MixinUsage
10 # Force UTF-8. Ruby will default to the system locale, and if it is
11 # non-UTF-8, String-methods will fail when operating on non-ASCII
12 # strings.
13 Encoding.default_external = Encoding::UTF_8
14 Encoding.default_internal = Encoding::UTF_8
16 GIT_DIR = `git rev-parse --show-toplevel`.freeze
17 assert_equal(
18 0, $CHILD_STATUS.exitstatus,
19 "Failed to find Tails' Git root; this command must be run " \
20 'inside Tails Git repo'
23 def rfc2822_date?(date)
24 !DateTime.rfc2822(date).nil?
25 rescue ArgumentError
26 false
27 end
29 def page_meta_date_is_ok?(path)
30 meta_date_post_re = /^\[\[!meta\s+date="(?<date>.*)"\]\]$/
31 success = true
32 content_lines = File.new(path).read.split("\n")
33 matches = content_lines.grep(meta_date_post_re)
34 if matches.size != 1
35 warn "#{path}: has #{matches.size} well-formed 'meta date' " \
36 'directives (must be 1)'
37 success = false
38 else
39 meta_date_line = matches.first
40 m = meta_date_post_re.match(meta_date_line)
41 meta_date = m['date']
42 unless rfc2822_date?(meta_date)
43 warn "#{path}: 'meta date' directive contains non-rfc2822 " \
44 "date: #{meta_date}"
45 success = false
46 end
47 end
48 success
49 end
51 def po_file_meta_date_is_ok?(path)
52 meta_date_po_re_str = '\[\[!meta\s+date=\\\"(?<date>.*)\\\"\]\]\\\n'
53 success = true
54 content_lines = File.new(path).read.split("\n")
55 matches = content_lines.grep(Regexp.new("^msgid \"#{meta_date_po_re_str}\"$"))
56 if matches.size != 1
57 warn "#{path}: has #{matches.size} 'meta date' msgid:s (must be 1)"
58 success = false
59 else
60 msgid = matches.first
61 msgid_index = content_lines.find_index(msgid)
62 msgstr_index = msgid_index + 1
63 msgstr_line = content_lines[msgstr_index]
64 m = Regexp.new("^msgstr \"(?:#{meta_date_po_re_str})?\"$")
65 .match(msgstr_line)
66 if m.nil?
67 warn "#{path}: the 'meta date' msgid is not followed by a msgstr"
68 success = false
69 elsif m['date']
70 meta_date = m['date']
71 unless rfc2822_date?(meta_date)
72 warn "#{path}: 'meta date' msgstr contains non-rfc2822 " \
73 "date: #{meta_date}"
74 success = false
75 end
76 end
77 end
78 success
79 end
81 # Main
83 success = true
85 meta_date_sorted_pages =
86 Dir.glob('wiki/src/{news,security,security/audits}/*.{mdwn,html}') - [
87 'wiki/src/security/audits.mdwn',
88 'wiki/src/security/fixed.mdwn',
91 meta_date_sorted_pages.each do |post_path|
92 success = false unless page_meta_date_is_ok?(post_path)
93 basename = post_path.sub(/.(html|mdwn)$/, '')
94 Dir.glob("#{basename}.*.po").each do |po_path|
95 success = false unless po_file_meta_date_is_ok?(po_path)
96 end
97 end
99 exit(success)