Skip to content

Commit

Permalink
refactor: Update Python analyzers to exclude files from site-packages…
Browse files Browse the repository at this point in the history
… directory

Signed-off-by: ksg <[email protected]>
  • Loading branch information
ksg97031 committed Oct 11, 2024
1 parent eeaedc0 commit 8197d50
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/analyzer/analyzers/python/django.cr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module Analyzer::Python
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|
Expand Down Expand Up @@ -111,7 +112,7 @@ module Analyzer::Python

content = content.split(keyword, 2)[1]
end

# TODO: Parse correct urlpatterns from variable concatenation case
content.scan(REGEX_ROUTE_MAPPING) do |route_match|
next if route_match.size != 3
Expand Down
1 change: 1 addition & 0 deletions src/analyzer/analyzers/python/fastapi.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Analyzer::Python
# Iterate through all Python files in the base path
Dir.glob("#{base_path}/**/*.py") do |path|
next if File.directory?(path)
next if path.includes?("/site-packages/")
source = File.read(path, encoding: "utf-8", invalid: :skip)

source.each_line do |line|
Expand Down
7 changes: 4 additions & 3 deletions src/analyzer/analyzers/python/flask.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module Analyzer::Python
@file_content_cache = Hash(::String, ::String).new
@parsers = Hash(::String, PythonParser).new
@routes = Hash(::String, Array(Tuple(Int32, ::String, ::String, ::String))).new

def analyze
flask_instances = Hash(::String, ::String).new
flask_instances["app"] ||= "" # Common flask instance name
Expand All @@ -40,8 +40,9 @@ module Analyzer::Python
# Iterate through all Python files in the base path
Dir.glob("#{base_path}/**/*.py") do |path|
next if File.directory?(path)
next if path.includes?("/site-packages/")
@logger.debug "Analyzing #{path}"

File.open(path, "r", encoding: "utf-8", invalid: :skip) do |file|
lines = file.each_line.to_a
next unless lines.any?(&.includes?("flask"))
Expand Down Expand Up @@ -281,7 +282,7 @@ module Analyzer::Python
@logger.debug "Parsing #{path}"
parser = PythonParser.new(path, tokens, @parsers)
@logger.debug "Parsed #{path}"
return parser
parser
end

# Get a parser for a given path
Expand Down
2 changes: 1 addition & 1 deletion src/miniparsers/python.cr
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class PythonParser
if import_part == ".."
path = File.dirname(package_dir)
end

# Order of checking is important
if File.directory?(path)
package_dir = path
Expand Down

0 comments on commit 8197d50

Please sign in to comment.