Skip to content

Commit 43b0eb3

Browse files
authored
Merge pull request #206 from ethankhall/removing-input-from-some-tasks
Fixed Java Serialization issue
2 parents 1a560da + 95d7e6b commit 43b0eb3

File tree

6 files changed

+113
-22
lines changed

6 files changed

+113
-22
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2016 LinkedIn Corp.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.linkedin.gradle.python.plugin
17+
18+
import com.linkedin.gradle.python.plugin.testutils.DefaultProjectLayoutRule
19+
import com.linkedin.gradle.python.plugin.testutils.PyGradleTestBuilder
20+
import org.gradle.testkit.runner.GradleRunner
21+
import org.gradle.testkit.runner.TaskOutcome
22+
import org.junit.Rule
23+
import spock.lang.Specification
24+
25+
class PythonSerializableTest extends Specification {
26+
27+
@Rule
28+
final DefaultProjectLayoutRule testProjectDir = new DefaultProjectLayoutRule()
29+
30+
def "can serialize inputs and outputs"() {
31+
given:
32+
testProjectDir.buildFile << """\
33+
|import com.linkedin.gradle.python.tasks.InstallVirtualEnvironmentTask
34+
|import com.linkedin.gradle.python.extension.PythonDetails
35+
|import com.linkedin.gradle.python.tasks.PipInstallTask
36+
|
37+
|import static com.linkedin.gradle.python.util.StandardTextValues.CONFIGURATION_PYTHON
38+
|
39+
|plugins {
40+
| id 'com.linkedin.python-sdist'
41+
|}
42+
|version = '1.2.3'
43+
|${ PyGradleTestBuilder.createRepoClosure() }
44+
|apply plugin: com.linkedin.gradle.python.plugin.WheelFirstPlugin
45+
|
46+
|dependencies {
47+
| python 'pypi:requests:2.18.1'
48+
|}
49+
|
50+
|
51+
|ext.anotherVenv = new File("\$buildDir/AnotherVenv")
52+
|
53+
|task installAnotherVenv ( type: InstallVirtualEnvironmentTask ) {
54+
| pythonDetails = new PythonDetails(project, anotherVenv)
55+
|}
56+
|
57+
|task installPythonRequirementsInAnotherVenv ( type: PipInstallTask ){
58+
| dependsOn installAnotherVenv
59+
| mustRunAfter installAnotherVenv
60+
| pythonDetails = new PythonDetails(project, anotherVenv)
61+
| installFileCollection = project.getConfigurations().getByName(CONFIGURATION_PYTHON.getValue())
62+
| outputs.dir anotherVenv
63+
|}
64+
""".stripMargin().stripIndent()
65+
66+
when:
67+
def result = GradleRunner.create()
68+
.withProjectDir(testProjectDir.root)
69+
.withArguments('installPythonRequirementsInAnotherVenv')
70+
.withPluginClasspath()
71+
.withDebug(true)
72+
.build()
73+
println result.output
74+
75+
then:
76+
77+
result.output.contains("BUILD SUCCESS")
78+
result.task(':foo:installPythonRequirementsInAnotherVenv').outcome == TaskOutcome.SUCCESS
79+
}
80+
}

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/extension/PythonDefaultVersions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
import org.gradle.api.GradleException;
1919

20+
import java.io.Serializable;
2021
import java.util.Collection;
2122
import java.util.TreeSet;
2223

2324

24-
public class PythonDefaultVersions {
25+
public class PythonDefaultVersions implements Serializable {
2526
private final String defaultPython2Version;
2627
private final String defaultPython3Version;
2728
private final Collection<String> allowedVersions;

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/extension/PythonDetails.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public class PythonDetails implements Serializable {
3737
private String virtualEnvPrompt;
3838
private PythonVersion pythonVersion;
3939
private PythonDefaultVersions pythonDefaultVersions;
40-
private OperatingSystem operatingSystem = OperatingSystem.current();
4140

4241
private List<File> searchPath;
4342

@@ -47,9 +46,9 @@ public PythonDetails(Project project) {
4746

4847
public PythonDetails(Project project, File venvDir) {
4948
this.project = project;
50-
activateLink = new File(project.getProjectDir(), operatingSystem.getScriptName("activate"));
49+
activateLink = new File(project.getProjectDir(), OperatingSystem.current().getScriptName("activate"));
5150
virtualEnvPrompt = String.format("(%s)", project.getName());
52-
searchPath = operatingSystem.getPath();
51+
searchPath = OperatingSystem.current().getPath();
5352
venvOverride = venvDir;
5453
this.virtualEnvironment = new VirtualEnvironment(this);
5554
pythonDefaultVersions = new PythonDefaultVersions();
@@ -80,7 +79,7 @@ public File getVirtualEnv() {
8079

8180
public File getVirtualEnvInterpreter() {
8281
String binDir = VirtualEnvironment.getPythonApplicationDirectory();
83-
String binName = operatingSystem.getExecutableName("python");
82+
String binName = OperatingSystem.current().getExecutableName("python");
8483
return Paths.get(getVirtualEnv().getAbsolutePath(), binDir, binName).toFile();
8584
}
8685

@@ -131,6 +130,7 @@ public PythonDefaultVersions getPythonDefaultVersions() {
131130

132131
public void setPythonVersion(String version) {
133132
version = pythonDefaultVersions.normalize(version);
133+
OperatingSystem operatingSystem = OperatingSystem.current();
134134
pythonInterpreter = operatingSystem.findInPath(searchPath, operatingSystem.getExecutableName(String.format("python%s", version)));
135135
updateFromPythonInterpreter();
136136
}
@@ -147,6 +147,7 @@ public PythonVersion getPythonVersion() {
147147

148148
private void findPythonWhenAbsent() {
149149
if (pythonInterpreter == null) {
150+
OperatingSystem operatingSystem = OperatingSystem.current();
150151
File python = operatingSystem.findInPath(searchPath, operatingSystem.getExecutableName("python"));
151152
if (python == null) {
152153
python = new File("/usr/bin/python");

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/extension/VirtualEnvironment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
import com.linkedin.gradle.python.util.OperatingSystem;
1919

2020
import java.io.File;
21+
import java.io.Serializable;
2122
import java.nio.file.Path;
2223

23-
public class VirtualEnvironment {
24+
public class VirtualEnvironment implements Serializable {
2425

2526
private final PythonDetails details;
2627

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/tasks/BuildWheelsTask.groovy

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.apache.commons.io.FileUtils
3434
import org.gradle.api.DefaultTask
3535
import org.gradle.api.GradleException
3636
import org.gradle.api.Project
37+
import org.gradle.api.Task
3738
import org.gradle.api.file.FileCollection
3839
import org.gradle.api.logging.Logger
3940
import org.gradle.api.logging.Logging
@@ -71,6 +72,15 @@ class BuildWheelsTask extends DefaultTask implements SupportsWheelCache, Support
7172

7273
EnvironmentMerger environmentMerger = new DefaultEnvironmentMerger()
7374

75+
public BuildWheelsTask() {
76+
getOutputs().doNotCacheIf('When package packageExcludeFilter is set', new Spec<Task>() {
77+
@Override
78+
boolean isSatisfiedBy(Task element) {
79+
return ((BuildWheelsTask) element).packageExcludeFilter != null
80+
}
81+
})
82+
}
83+
7484
@TaskAction
7585
void buildWheelsTask() {
7686
buildWheels(project, DependencyOrder.getConfigurationFiles(installFileCollection), getPythonDetails())
@@ -97,13 +107,7 @@ class BuildWheelsTask extends DefaultTask implements SupportsWheelCache, Support
97107
/**
98108
* Will return true when the package should be excluded from being installed.
99109
*/
100-
@Input
101-
Spec<PackageInfo> packageExcludeFilter = new Spec<PackageInfo>() {
102-
@Override
103-
boolean isSatisfiedBy(PackageInfo packageInfo) {
104-
return false
105-
}
106-
}
110+
Spec<PackageInfo> packageExcludeFilter = null
107111

108112
@Input
109113
PythonDetails getPythonDetails() {
@@ -159,7 +163,7 @@ class BuildWheelsTask extends DefaultTask implements SupportsWheelCache, Support
159163
LOGGER.lifecycle("Installing {} wheel", shortHand)
160164
}
161165

162-
if (packageExcludeFilter.isSatisfiedBy(packageInfo)) {
166+
if (packageExcludeFilter != null && packageExcludeFilter.isSatisfiedBy(packageInfo)) {
163167
if (PythonHelpers.isPlainOrVerbose(project)) {
164168
LOGGER.lifecycle("Skipping {} wheel - Excluded", shortHand)
165169
}

pygradle-plugin/src/main/groovy/com/linkedin/gradle/python/tasks/PipInstallTask.groovy

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import com.linkedin.gradle.python.wheel.WheelCache
3434
import groovy.transform.CompileStatic
3535
import org.gradle.api.DefaultTask
3636
import org.gradle.api.GradleException
37+
import org.gradle.api.Task
3738
import org.gradle.api.file.FileCollection
3839
import org.gradle.api.specs.Spec
3940
import org.gradle.api.tasks.Input
@@ -80,16 +81,19 @@ class PipInstallTask extends DefaultTask implements FailureReasonProvider, Suppo
8081

8182
EnvironmentMerger environmentMerger = new DefaultEnvironmentMerger()
8283

84+
public PipInstallTask() {
85+
getOutputs().doNotCacheIf('When package packageExcludeFilter is set', new Spec<Task>() {
86+
@Override
87+
boolean isSatisfiedBy(Task element) {
88+
return ((PipInstallTask) element).packageExcludeFilter != null
89+
}
90+
})
91+
}
92+
8393
/**
8494
* Will return true when the package should be excluded from being installed.
8595
*/
86-
@Input
87-
Spec<PackageInfo> packageExcludeFilter = new Spec<PackageInfo>() {
88-
@Override
89-
boolean isSatisfiedBy(PackageInfo packageInfo) {
90-
return false
91-
}
92-
}
96+
Spec<PackageInfo> packageExcludeFilter = null
9397

9498
private String lastInstallMessage = null
9599

@@ -158,7 +162,7 @@ class PipInstallTask extends DefaultTask implements FailureReasonProvider, Suppo
158162
@SuppressWarnings("ParameterCount")
159163
private void doInstall(String shortHand, PackageInfo packageInfo, Path sitePackages,
160164
String pyVersion, PythonExtension extension, File installable) {
161-
if (packageExcludeFilter.isSatisfiedBy(packageInfo)) {
165+
if (packageExcludeFilter != null && packageExcludeFilter.isSatisfiedBy(packageInfo)) {
162166
if (PythonHelpers.isPlainOrVerbose(project)) {
163167
logger.lifecycle("Skipping {} - Excluded", shortHand)
164168
}

0 commit comments

Comments
 (0)