Skip to content
This repository was archived by the owner on Mar 27, 2024. It is now read-only.

Commit 973d9af

Browse files
committed
Switch to a for loop.
1 parent 34c9b66 commit 973d9af

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

differs/pip_diff.go

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ limitations under the License.
1717
package differs
1818

1919
import (
20+
"errors"
2021
"io/ioutil"
21-
"os"
2222
"path/filepath"
2323
"regexp"
2424
"strings"
@@ -124,34 +124,26 @@ func addToMap(packages map[string]map[string]util.PackageInfo, pack string, path
124124

125125
func getPythonVersion(pathToLayer string) ([]string, error) {
126126
matches := []string{}
127+
pattern := regexp.MustCompile("^python[0-9]+\\.[0-9]+$")
127128

128-
// Debian based distros
129-
libPath := filepath.Join(pathToLayer, "usr/local/lib")
130-
libContents, err := ioutil.ReadDir(libPath)
131-
if err != nil {
132-
logrus.Warnf("Could not find /usr/local/lib to determine Python version, trying /usr/lib: %s", err)
133-
libContents = []os.FileInfo{}
134-
}
135-
136-
// Fedora, CentOS, RHEL, ...
137-
libPath = filepath.Join(pathToLayer, "usr/lib")
138-
libContents2, err2 := ioutil.ReadDir(libPath)
139-
if err2 != nil {
129+
libPaths := []string{"usr/local/lib", "usr/lib"}
130+
for _, lp := range libPaths {
131+
libPath := filepath.Join(pathToLayer, lp)
132+
libContents, err := ioutil.ReadDir(libPath)
140133
if err != nil {
141-
logrus.Errorf("Could not find /usr/lib nor /usr/local/lib to determine Python version: %s", err2)
142-
return matches, err
134+
logrus.Debugf("Could not find %s to determine Python version", err)
135+
continue
143136
}
144-
logrus.Warnf("Could not find /usr/lib to determine Python version, using /usr/local/lib: %s", err2)
145-
libContents2 = []os.FileInfo{}
146-
}
147-
148-
for _, file := range append(libContents, libContents2...) {
149-
pattern := regexp.MustCompile("^python[0-9]+\\.[0-9]+$")
150-
match := pattern.FindString(file.Name())
151-
if match != "" {
152-
matches = append(matches, match)
137+
for _, file := range libContents {
138+
match := pattern.FindString(file.Name())
139+
if match != "" {
140+
matches = append(matches, match)
141+
}
153142
}
154143
}
144+
if len(matches) == 0 {
145+
return nil, errors.New("unable to determine Python version")
146+
}
155147
return matches, nil
156148
}
157149

0 commit comments

Comments
 (0)