Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/tools/dev/verify_encoding.rb
55632 views
1
#!/usr/bin/env ruby
2
3
require 'find'
4
5
def report_non_ascii(file_path)
6
has_errors = false
7
File.foreach(file_path).with_index(1) do |line, line_number|
8
line_utf8 = line.force_encoding('UTF-8')
9
next unless line_utf8.valid_encoding?
10
11
line_utf8.chars.each_with_index do |char, index|
12
unless char.ascii_only?
13
has_errors = true
14
puts "Error - non-ascii character found. #{file_path}: line #{line_number}, char #{index+1} => #{char.inspect}"
15
end
16
end
17
end
18
has_errors
19
end
20
21
path_to_search = File.expand_path(File.join(__FILE__, '..', '..', '..'))
22
puts "Verifying binary encoding files in '#{path_to_search}' do not contain non-ASCII characters..."
23
24
has_errors = false
25
ran_at_least_once = false
26
27
# Walk all files
28
Find.find(path_to_search) do |path|
29
next unless File.file?(path)
30
next if path.include?('/vendor/')
31
next unless path.end_with?('.rb')
32
33
# Only check files that declare binary encoding
34
first_two_lines = File.open(path, "r") { |f| f.each_line.take(2).join }
35
next unless first_two_lines =~ /coding:\s*binary/
36
37
ran_at_least_once = true
38
has_errors |= report_non_ascii(path)
39
end
40
41
if !ran_at_least_once
42
puts "Did not run on any files, did not find any files with binary encoding declaration. Please check the script and ensure it is looking for the correct encoding declaration."
43
exit(1)
44
end
45
46
if has_errors
47
puts "Finished with errors. Please fix the above issues and run again."
48
exit(1)
49
end
50
51
puts "Finished without errors"
52
53
54