-
Notifications
You must be signed in to change notification settings - Fork 715
/
file_parser.rb
80 lines (69 loc) · 2.14 KB
/
file_parser.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
require 'parallel'
module Brakeman
ASTFile = Struct.new(:path, :ast)
# This class handles reading and parsing files.
class FileParser
attr_reader :file_list, :errors
def initialize app_tree, timeout
@app_tree = app_tree
@timeout = timeout
@file_list = []
@errors = []
end
def parse_files list
# Parse the files in parallel.
# By default, the parsing will be in separate processes.
# So we map the result to ASTFiles and/or Exceptions
# then partition them into ASTFiles and Exceptions
# and add the Exceptions to @errors
#
# Basically just a funky way to deal with two possible
# return types that are returned from isolated processes.
#
# Note this method no longer uses read_files
@file_list, new_errors = Parallel.map(list) do |file_name|
file_path = @app_tree.file_path(file_name)
contents = file_path.read
begin
if ast = parse_ruby(contents, file_path.relative)
ASTFile.new(file_name, ast)
end
rescue Exception => e
e
end
end.compact.partition do |result|
result.is_a? ASTFile
end
errors.concat new_errors
end
def read_files list
list.each do |path|
file = @app_tree.file_path(path)
begin
result = yield file, file.read
if result
@file_list << result
end
rescue Exception => e
@errors << e
end
end
end
# _path_ can be a string or a Brakeman::FilePath
def parse_ruby input, path
if path.is_a? Brakeman::FilePath
path = path.relative
end
begin
Brakeman.debug "Parsing #{path}"
RubyParser.new.parse input, path, @timeout
rescue Racc::ParseError => e
raise e.exception(e.message + "\nCould not parse #{path}")
rescue Timeout::Error => e
raise Exception.new("Parsing #{path} took too long (> #{@timeout} seconds). Try increasing the limit with --parser-timeout")
rescue => e
raise e.exception(e.message + "\nWhile processing #{path}")
end
end
end
end