Skip to content

Commit

Permalink
Merge pull request #206 from ethankhall/removing-input-from-some-tasks
Browse files Browse the repository at this point in the history
Fixed Java Serialization issue
  • Loading branch information
zvezdan authored Mar 23, 2018
2 parents 1a560da + 95d7e6b commit 43b0eb3
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2016 LinkedIn Corp.
*
* 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 com.linkedin.gradle.python.plugin

import com.linkedin.gradle.python.plugin.testutils.DefaultProjectLayoutRule
import com.linkedin.gradle.python.plugin.testutils.PyGradleTestBuilder
import org.gradle.testkit.runner.GradleRunner
import org.gradle.testkit.runner.TaskOutcome
import org.junit.Rule
import spock.lang.Specification

class PythonSerializableTest extends Specification {

@Rule
final DefaultProjectLayoutRule testProjectDir = new DefaultProjectLayoutRule()

def "can serialize inputs and outputs"() {
given:
testProjectDir.buildFile << """\
|import com.linkedin.gradle.python.tasks.InstallVirtualEnvironmentTask
|import com.linkedin.gradle.python.extension.PythonDetails
|import com.linkedin.gradle.python.tasks.PipInstallTask
|
|import static com.linkedin.gradle.python.util.StandardTextValues.CONFIGURATION_PYTHON
|
|plugins {
| id 'com.linkedin.python-sdist'
|}
|version = '1.2.3'
|${ PyGradleTestBuilder.createRepoClosure() }
|apply plugin: com.linkedin.gradle.python.plugin.WheelFirstPlugin
|
|dependencies {
| python 'pypi:requests:2.18.1'
|}
|
|
|ext.anotherVenv = new File("\$buildDir/AnotherVenv")
|
|task installAnotherVenv ( type: InstallVirtualEnvironmentTask ) {
| pythonDetails = new PythonDetails(project, anotherVenv)
|}
|
|task installPythonRequirementsInAnotherVenv ( type: PipInstallTask ){
| dependsOn installAnotherVenv
| mustRunAfter installAnotherVenv
| pythonDetails = new PythonDetails(project, anotherVenv)
| installFileCollection = project.getConfigurations().getByName(CONFIGURATION_PYTHON.getValue())
| outputs.dir anotherVenv
|}
""".stripMargin().stripIndent()

when:
def result = GradleRunner.create()
.withProjectDir(testProjectDir.root)
.withArguments('installPythonRequirementsInAnotherVenv')
.withPluginClasspath()
.withDebug(true)
.build()
println result.output

then:

result.output.contains("BUILD SUCCESS")
result.task(':foo:installPythonRequirementsInAnotherVenv').outcome == TaskOutcome.SUCCESS
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

import org.gradle.api.GradleException;

import java.io.Serializable;
import java.util.Collection;
import java.util.TreeSet;


public class PythonDefaultVersions {
public class PythonDefaultVersions implements Serializable {
private final String defaultPython2Version;
private final String defaultPython3Version;
private final Collection<String> allowedVersions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public class PythonDetails implements Serializable {
private String virtualEnvPrompt;
private PythonVersion pythonVersion;
private PythonDefaultVersions pythonDefaultVersions;
private OperatingSystem operatingSystem = OperatingSystem.current();

private List<File> searchPath;

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

public PythonDetails(Project project, File venvDir) {
this.project = project;
activateLink = new File(project.getProjectDir(), operatingSystem.getScriptName("activate"));
activateLink = new File(project.getProjectDir(), OperatingSystem.current().getScriptName("activate"));
virtualEnvPrompt = String.format("(%s)", project.getName());
searchPath = operatingSystem.getPath();
searchPath = OperatingSystem.current().getPath();
venvOverride = venvDir;
this.virtualEnvironment = new VirtualEnvironment(this);
pythonDefaultVersions = new PythonDefaultVersions();
Expand Down Expand Up @@ -80,7 +79,7 @@ public File getVirtualEnv() {

public File getVirtualEnvInterpreter() {
String binDir = VirtualEnvironment.getPythonApplicationDirectory();
String binName = operatingSystem.getExecutableName("python");
String binName = OperatingSystem.current().getExecutableName("python");
return Paths.get(getVirtualEnv().getAbsolutePath(), binDir, binName).toFile();
}

Expand Down Expand Up @@ -131,6 +130,7 @@ public PythonDefaultVersions getPythonDefaultVersions() {

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

private void findPythonWhenAbsent() {
if (pythonInterpreter == null) {
OperatingSystem operatingSystem = OperatingSystem.current();
File python = operatingSystem.findInPath(searchPath, operatingSystem.getExecutableName("python"));
if (python == null) {
python = new File("/usr/bin/python");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
import com.linkedin.gradle.python.util.OperatingSystem;

import java.io.File;
import java.io.Serializable;
import java.nio.file.Path;

public class VirtualEnvironment {
public class VirtualEnvironment implements Serializable {

private final PythonDetails details;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.apache.commons.io.FileUtils
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.logging.Logger
import org.gradle.api.logging.Logging
Expand Down Expand Up @@ -71,6 +72,15 @@ class BuildWheelsTask extends DefaultTask implements SupportsWheelCache, Support

EnvironmentMerger environmentMerger = new DefaultEnvironmentMerger()

public BuildWheelsTask() {
getOutputs().doNotCacheIf('When package packageExcludeFilter is set', new Spec<Task>() {
@Override
boolean isSatisfiedBy(Task element) {
return ((BuildWheelsTask) element).packageExcludeFilter != null
}
})
}

@TaskAction
void buildWheelsTask() {
buildWheels(project, DependencyOrder.getConfigurationFiles(installFileCollection), getPythonDetails())
Expand All @@ -97,13 +107,7 @@ class BuildWheelsTask extends DefaultTask implements SupportsWheelCache, Support
/**
* Will return true when the package should be excluded from being installed.
*/
@Input
Spec<PackageInfo> packageExcludeFilter = new Spec<PackageInfo>() {
@Override
boolean isSatisfiedBy(PackageInfo packageInfo) {
return false
}
}
Spec<PackageInfo> packageExcludeFilter = null

@Input
PythonDetails getPythonDetails() {
Expand Down Expand Up @@ -159,7 +163,7 @@ class BuildWheelsTask extends DefaultTask implements SupportsWheelCache, Support
LOGGER.lifecycle("Installing {} wheel", shortHand)
}

if (packageExcludeFilter.isSatisfiedBy(packageInfo)) {
if (packageExcludeFilter != null && packageExcludeFilter.isSatisfiedBy(packageInfo)) {
if (PythonHelpers.isPlainOrVerbose(project)) {
LOGGER.lifecycle("Skipping {} wheel - Excluded", shortHand)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.linkedin.gradle.python.wheel.WheelCache
import groovy.transform.CompileStatic
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Task
import org.gradle.api.file.FileCollection
import org.gradle.api.specs.Spec
import org.gradle.api.tasks.Input
Expand Down Expand Up @@ -80,16 +81,19 @@ class PipInstallTask extends DefaultTask implements FailureReasonProvider, Suppo

EnvironmentMerger environmentMerger = new DefaultEnvironmentMerger()

public PipInstallTask() {
getOutputs().doNotCacheIf('When package packageExcludeFilter is set', new Spec<Task>() {
@Override
boolean isSatisfiedBy(Task element) {
return ((PipInstallTask) element).packageExcludeFilter != null
}
})
}

/**
* Will return true when the package should be excluded from being installed.
*/
@Input
Spec<PackageInfo> packageExcludeFilter = new Spec<PackageInfo>() {
@Override
boolean isSatisfiedBy(PackageInfo packageInfo) {
return false
}
}
Spec<PackageInfo> packageExcludeFilter = null

private String lastInstallMessage = null

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

0 comments on commit 43b0eb3

Please sign in to comment.