Skip to content

Commit

Permalink
Do not run pip check in the pip buildpack for Python 3.7
Browse files Browse the repository at this point in the history
This preserves the behavior of the current build system.

PiperOrigin-RevId: 327876585
Change-Id: I5e614a0b66c26e1a3073f82389be78db022c7dc3
  • Loading branch information
lukasberger authored and copybara-github committed Aug 21, 2020
1 parent ac80583 commit b2b5c8b
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 6 deletions.
5 changes: 5 additions & 0 deletions builders/gae/python37/acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func TestAcceptance(t *testing.T) {
{
App: "pip_dependency",
},
{
App: "pip_check",
// The warning message is cut short because it's not deterministic.
MustOutput: []string{`WARNING: Found incompatible dependencies: "sub-dependency-`},
},
{
App: "gunicorn_present",
},
Expand Down
23 changes: 23 additions & 0 deletions builders/gae/python38/acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,26 @@ func TestAcceptance(t *testing.T) {
})
}
}

func TestFailures(t *testing.T) {
builder, cleanup := acceptance.CreateBuilder(t)
t.Cleanup(cleanup)

testCases := []acceptance.FailureTest{
{
App: "pip_check",
MustMatch: `sub-dependency-\w 1\.0\.0 has requirement sub-dependency-\w.*1\.0\.0, but you.* have sub-dependency-\w 1\.0\.0`,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.App, func(t *testing.T) {
t.Parallel()

tc.Env = append(tc.Env, "GOOGLE_RUNTIME=python38")

acceptance.TestBuildFailure(t, builder, tc)
})
}
}
23 changes: 23 additions & 0 deletions builders/gae/python39/acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,26 @@ func TestAcceptance(t *testing.T) {
})
}
}

func TestFailures(t *testing.T) {
builder, cleanup := acceptance.CreateBuilder(t)
t.Cleanup(cleanup)

testCases := []acceptance.FailureTest{
{
App: "pip_check",
MustMatch: `sub-dependency-\w 1\.0\.0 has requirement sub-dependency-\w.*1\.0\.0, but you.* have sub-dependency-\w 1\.0\.0`,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.App, func(t *testing.T) {
t.Parallel()

tc.Env = append(tc.Env, "GOOGLE_RUNTIME=python39")

acceptance.TestBuildFailure(t, builder, tc)
})
}
}
27 changes: 27 additions & 0 deletions builders/testdata/python/pip_check/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2020 Google LLC
#
# 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.

from flask import Flask
import os

app = Flask(__name__)


@app.route('/')
def hello():
return 'PASS'


if __name__ == '__main__':
app.run(port=os.environ['PORT'], debug=True)
2 changes: 2 additions & 0 deletions builders/testdata/python/pip_check/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
flask==1.1.1
diamond-dependency==1.0.0 # This package will fail a build with pip check.
19 changes: 14 additions & 5 deletions cmd/python/pip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"fmt"
"os"
"strings"

"github.com/GoogleCloudPlatform/buildpacks/pkg/cache"
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
Expand Down Expand Up @@ -68,11 +69,19 @@ func buildFn(ctx *gcp.Context) error {

l.SharedEnvironment.PrependPath("PYTHONPATH", l.Path)

// Check for broken dependencies.
ctx.Logf("Checking for incompatible dependencies.")
checkDeps := ctx.Exec([]string{"python3", "-m", "pip", "check"}, gcp.WithEnv("PYTHONPATH="+l.Path+":"+os.Getenv("PYTHONPATH")), gcp.WithUserAttribution)
if checkDeps.ExitCode != 0 {
return fmt.Errorf("incompatible dependencies installed: %q", checkDeps.Stdout)
result, err := ctx.ExecWithErr([]string{"python3", "-m", "pip", "check"}, gcp.WithEnv("PYTHONPATH="+l.Path+":"+os.Getenv("PYTHONPATH")), gcp.WithUserAttribution)
if result == nil {
return fmt.Errorf("pip check: %w", err)
}
return nil
if result.ExitCode == 0 {
return nil
}
// HACK: For backwards compatibility on App Engine and Cloud Functions Python 3.7 only report a warning.
if strings.HasPrefix(python.Version(ctx), "Python 3.7") {
ctx.Warnf("Found incompatible dependencies: %q", result.Stdout)
return nil
}
return fmt.Errorf("found incompatible dependencies: %q", result.Stdout)

}
2 changes: 1 addition & 1 deletion pkg/python/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const (
// Version returns the installed version of Python.
func Version(ctx *gcp.Context) string {
result := ctx.Exec([]string{"python3", "--version"})
return strings.TrimSpace(result.Stderr)
return strings.TrimSpace(result.Stdout)
}

// CheckCache checks whether cached dependencies exist and match.
Expand Down

0 comments on commit b2b5c8b

Please sign in to comment.