Skip to content

Commit

Permalink
feat(analyzer): enhance concurrency in Django URL finding with WaitGr…
Browse files Browse the repository at this point in the history
…oup and Channel

Signed-off-by: HAHWUL <[email protected]>
  • Loading branch information
hahwul committed Jan 16, 2025
1 parent e868923 commit 7348ff4
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/analyzer/analyzers/python/django.cr
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,44 @@ module Analyzer::Python
# Find all root Django URLs
def find_root_django_urls : Array(DjangoUrls)
root_django_urls_list = [] of DjangoUrls

channel = Channel(String).new
search_dir = @base_path
begin

spawn do
Dir.glob("#{search_dir}/**/*") do |file|
spawn do
begin
next if File.directory?(file)
next if file.includes?("/site-packages/")
if file.ends_with? ".py"
content = File.read(file, encoding: "utf-8", invalid: :skip)
content.scan(REGEX_ROOT_URLCONF) do |match|
next if match.size != 2
dotted_as_urlconf = match[1].split(".")
relative_path = "#{dotted_as_urlconf.join("/")}.py"

Dir.glob("#{search_dir}/**/#{relative_path}") do |filepath|
basepath = filepath.split("/")[..-(dotted_as_urlconf.size + 1)].join("/")
root_django_urls_list << DjangoUrls.new("", filepath, basepath)
channel.send(file)
end
channel.close
end

WaitGroup.wait do |wg|
@options["concurrency"].to_s.to_i.times do
wg.spawn do
loop do
begin
file = channel.receive?
break if file.nil?
next if File.directory?(file)
next if file.includes?("/site-packages/")
if file.ends_with? ".py"
content = File.read(file, encoding: "utf-8", invalid: :skip)
content.scan(REGEX_ROOT_URLCONF) do |match|
next if match.size != 2
dotted_as_urlconf = match[1].split(".")
relative_path = "#{dotted_as_urlconf.join("/")}.py"

Dir.glob("#{search_dir}/**/#{relative_path}") do |filepath|
basepath = filepath.split("/")[..-(dotted_as_urlconf.size + 1)].join("/")
root_django_urls_list << DjangoUrls.new("", filepath, basepath)
end
end
end
rescue e : File::NotFoundError
logger.debug "File not found: #{file}"
end
rescue e : File::NotFoundError
logger.debug "File not found: #{file}"
end
end
Fiber.yield
end
rescue e
logger.debug e
end

root_django_urls_list.uniq
Expand Down

0 comments on commit 7348ff4

Please sign in to comment.