summaryrefslogtreecommitdiffstats
path: root/bin/sanity-check-website
blob: 0e725d6741a002361d2bbcf598fd39855586587e (plain)
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
#!/usr/bin/env ruby
require 'date'

require 'test/unit'
Test::Unit.run = true
include Test::Unit::Assertions

# Force UTF-8. Ruby will default to the system locale, and if it is
# non-UTF-8, String-methods will fail when operating on non-ASCII
# strings.
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

GIT_DIR=`git rev-parse --show-toplevel`
assert_equal(
  0, $?.exitstatus,
  "Failed to find Tails' Git root; this command must be run " +
  "inside Tails Git repo"
)

def rfc2822_date?(date)
  !!DateTime.rfc2822(date)
rescue ArgumentError
  false
end

def page_meta_date_is_ok?(path)
  meta_date_post_re = /^\[\[!meta\s+date="(?<date>.*)"\]\]$/
  success = true
  content_lines = File.new(path).read.split("\n")
  matches = content_lines.grep(meta_date_post_re)
  if matches.size != 1
    STDERR.puts "#{path}: has #{matches.size} well-formed 'meta date' " +
                "directives (must be 1)"
    success = false
  else
    meta_date_line = matches.first
    m = meta_date_post_re.match(meta_date_line)
    meta_date = m['date']
    if not(rfc2822_date?(meta_date))
      STDERR.puts "#{path}: 'meta date' directive contains non-rfc2822 " +
                  "date: #{meta_date}"
      success = false
    end
  end
  return success
end

def po_file_meta_date_is_ok?(path)
  meta_date_po_re_str = '\[\[!meta\s+date=\\\"(?<date>.*)\\\"\]\]\\\n'
  success = true
  content_lines = File.new(path).read.split("\n")
  matches = content_lines.grep(Regexp.new("^msgid \"#{meta_date_po_re_str}\"$"))
  if matches.size != 1
    STDERR.puts "#{path}: has #{matches.size} 'meta date' msgid:s (must be 1)"
    success = false
  else
    msgid = matches.first
    msgid_index = content_lines.find_index(msgid)
    msgstr_index = msgid_index + 1
    msgstr_line = content_lines[msgstr_index]
    m = Regexp.new("^msgstr \"(?:#{meta_date_po_re_str})?\"$").match(msgstr_line)
    if m.nil?
      STDERR.puts "#{path}: the 'meta date' msgid is not followed by a msgstr"
      success = false
    elsif m['date']
      meta_date = m['date']
      if not(rfc2822_date?(meta_date))
        STDERR.puts "#{path}: 'meta date' msgstr contains non-rfc2822 " +
                    "date: #{meta_date}"
        success = false
      end
    end
  end
  return success
end

# Main

success = true

meta_date_sorted_pages =
  Dir.glob('wiki/src/{news,security,security/audits}/*.{mdwn,html}') - [
  'wiki/src/security/audits.mdwn',
  ]

meta_date_sorted_pages.each do |post_path|
  success = false unless page_meta_date_is_ok?(post_path)
  Dir.glob(post_path.sub(/.(html|mdwn)$/, '') + '.*.po').each do |po_path|
    success = false unless po_file_meta_date_is_ok?(po_path)
  end
end

exit(success)