This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathpip_diff.go
154 lines (134 loc) · 4.91 KB
/
pip_diff.go
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2017 Google, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package differs
import (
"io/ioutil"
"path/filepath"
"regexp"
"strings"
pkgutil "github.com/GoogleCloudPlatform/container-diff/pkg/util"
"github.com/GoogleCloudPlatform/container-diff/util"
"github.com/sirupsen/logrus"
)
type PipAnalyzer struct {
}
func (a PipAnalyzer) Name() string {
return "PipAnalyzer"
}
// PipDiff compares pip-installed Python packages between layers of two different images.
func (a PipAnalyzer) Diff(image1, image2 pkgutil.Image) (util.Result, error) {
diff, err := multiVersionDiff(image1, image2, a)
return diff, err
}
func (a PipAnalyzer) Analyze(image pkgutil.Image) (util.Result, error) {
analysis, err := multiVersionAnalysis(image, a)
return analysis, err
}
func (a PipAnalyzer) getPackages(image pkgutil.Image) (map[string]map[string]util.PackageInfo, error) {
path := image.FSPath
packages := make(map[string]map[string]util.PackageInfo)
pythonPaths := []string{}
if image.Config.Config.Env != nil {
paths := getPythonPaths(image.Config.Config.Env)
for _, p := range paths {
pythonPaths = append(pythonPaths, p)
}
}
pythonVersions, err := getPythonVersion(path)
if err != nil {
// Image doesn't have Python installed
return packages, nil
}
// default python package installation directories in unix
// these are hardcoded in the python source; unfortunately no way to retrieve them from env
for _, pythonVersion := range pythonVersions {
pythonPaths = append(pythonPaths, filepath.Join(path, "usr/lib", pythonVersion))
pythonPaths = append(pythonPaths, filepath.Join(path, "usr/lib", pythonVersion, "dist-packages"))
pythonPaths = append(pythonPaths, filepath.Join(path, "usr/lib", pythonVersion, "site-packages"))
pythonPaths = append(pythonPaths, filepath.Join(path, "usr/local/lib", pythonVersion, "dist-packages"))
pythonPaths = append(pythonPaths, filepath.Join(path, "usr/local/lib", pythonVersion, "site-packages"))
}
for _, pythonPath := range pythonPaths {
contents, err := ioutil.ReadDir(pythonPath)
if err != nil {
// python version folder doesn't have a site-packages folder
continue
}
for i := 0; i < len(contents); i++ {
c := contents[i]
fileName := c.Name()
// check if package
packageDir := regexp.MustCompile("^([a-z|A-Z|0-9|_]+)-(([0-9]+?\\.){2,3})(dist-info|egg-info)$")
packageMatch := packageDir.FindStringSubmatch(fileName)
if len(packageMatch) != 0 {
packageName := packageMatch[1]
version := packageMatch[2][:len(packageMatch[2])-1]
// Retrieves size for actual package/script corresponding to each dist-info metadata directory
// by taking the file entry alphabetically before it (for a package) or after it (for a script)
var size int64
if i-1 >= 0 && contents[i-1].Name() == packageName {
packagePath := filepath.Join(pythonPath, packageName)
size = pkgutil.GetSize(packagePath)
} else if i+1 < len(contents) && contents[i+1].Name() == packageName+".py" {
size = contents[i+1].Size()
} else {
logrus.Errorf("Could not find Python package %s for corresponding metadata info", packageName)
continue
}
currPackage := util.PackageInfo{Version: version, Size: size}
mapPath := strings.Replace(pythonPath, path, "", 1)
addToMap(packages, packageName, mapPath, currPackage)
}
}
}
return packages, nil
}
func addToMap(packages map[string]map[string]util.PackageInfo, pack string, path string, packInfo util.PackageInfo) {
if _, ok := packages[pack]; !ok {
// package not yet seen
infoMap := make(map[string]util.PackageInfo)
infoMap[path] = packInfo
packages[pack] = infoMap
return
}
packages[pack][path] = packInfo
}
func getPythonVersion(pathToLayer string) ([]string, error) {
matches := []string{}
libPath := filepath.Join(pathToLayer, "usr/local/lib")
libContents, err := ioutil.ReadDir(libPath)
if err != nil {
return matches, err
}
for _, file := range libContents {
pattern := regexp.MustCompile("^python[0-9]+\\.[0-9]+$")
match := pattern.FindString(file.Name())
if match != "" {
matches = append(matches, match)
}
}
return matches, nil
}
func getPythonPaths(vars []string) []string {
paths := []string{}
for _, envVar := range vars {
pythonPathPattern := regexp.MustCompile("^PYTHONPATH=(.*)")
match := pythonPathPattern.FindStringSubmatch(envVar)
if len(match) != 0 {
pythonPath := match[1]
paths = strings.Split(pythonPath, ":")
break
}
}
return paths
}