Skip to content

Commit

Permalink
fix(python): handling non-utf8 encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
iseki0 committed Sep 14, 2024
1 parent a7baf38 commit 36e4723
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions module/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func collectDepsInfo(ctx context.Context, dir string) ([][2]string, error) {
return nil
}
if isDockerfile(filename) {
data, e := readFile(path, 64*1024) // Dockerfile max 64K
data, e := readTextFile(path, 64*1024) // Dockerfile max 64K
if e != nil {
logger.Warnf("read dockerfile: %s %v", path, e)
return nil
Expand All @@ -133,7 +133,7 @@ func collectDepsInfo(ctx context.Context, dir string) ([][2]string, error) {
return nil
}
if isRequirementsFile(filename) {
data, e := readFile(path, 64*1024)
data, e := readTextFile(path, 64*1024)
if e != nil {
logger.Warnf("read requirement: %s %v", path, e)
return nil
Expand All @@ -148,7 +148,7 @@ func collectDepsInfo(ctx context.Context, dir string) ([][2]string, error) {
return nil
}
if filepath.Ext(filename) == ".py" {
data, e := readFile(path, 256*1024)
data, e := readTextFile(path, 256*1024)
if e != nil {
logger.Warnf("read py: %s %v", path, e)
return nil
Expand Down
14 changes: 12 additions & 2 deletions module/python/utils.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package python

import (
"golang.org/x/net/html/charset"
"io"
"os"
)

func readFile(path string, maxLength int) ([]byte, error) {
func readTextFile(path string, maxLength int) ([]byte, error) {
f, e := os.Open(path)
if e != nil {
return nil, e
}
defer func() { _ = f.Close() }()
return io.ReadAll(io.LimitReader(f, int64(maxLength)))
r, e := charset.NewReader(f, "")
if e != nil {
_ = f.Close()
f, e = os.Open(path)
if e != nil {
return nil, e
}
r = f
}
return io.ReadAll(io.LimitReader(r, int64(maxLength)))
}

0 comments on commit 36e4723

Please sign in to comment.