Skip to content

Changes for submission #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
.metadata/
.settings/
.project
.classpath
*.class
*.pyc
target/
.idea
*.iml
RemoteSystemsTempFiles
.DS_Store

21 changes: 21 additions & 0 deletions maven-python-mojos/maven-bdd-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
30 changes: 27 additions & 3 deletions maven-python-mojos/maven-python-distribute-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,40 @@
<parent>
<artifactId>maven-python-mojos</artifactId>
<groupId>maven-python-mojos</groupId>
<version>1.2</version>
<version>1.3</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>maven-python-mojos</groupId>
<artifactId>maven-python-distribute-plugin</artifactId>
<version>0.1.1</version>
<version>0.1.3</version>
<packaging>maven-plugin</packaging>
<name>Maven Python Distribute Plugin</name>

<dependencies />
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>


<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,94 +16,157 @@
* limitations under the License.
*/

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.io.IOUtils;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
* Packages a Python module using distribute
*
*
* @goal package
* @phase package
*/
public class PackageMojo extends AbstractMojo {

private static final String VERSION = "${VERSION}";

/**
* @parameter expression="${project.version}"
* @required
*/
private String projectVersion;

/**
* Allows overriding the default version
*/
@Getter @Setter private String version = null;

/* (non-Javadoc)
* @see org.apache.maven.plugin.AbstractMojo#execute()
*/
public void execute() throws MojoExecutionException, MojoFailureException {

File setup = new File("src/main/python/setup.py");

try {

if (version != null) {
version = projectVersion;
}


//update VERSION to latest version
List<String> lines = IOUtils.readLines(new BufferedInputStream(new FileInputStream(setup)));
for(String line : lines) {
if (line.contains(VERSION)) {
line = line.replace(VERSION,version);
}
}
IOUtils.writeLines(lines,"\n", new BufferedOutputStream(new FileOutputStream(setup)));

//execute setup script
ProcessBuilder t = new ProcessBuilder("python","setup.py","bdist_egg");
t.directory(new File("src/test/python"));
t.redirectErrorStream(true);

Process pr = t.start();
int exitCode = pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line = buf.readLine()) != null) {
getLog().info(line);
}

if (exitCode != 0) {
throw new MojoExecutionException("python setup.py returned error code " + exitCode);
}

} catch (FileNotFoundException e) {
throw new MojoExecutionException("Unable to find " + setup.getPath(),e);
} catch (IOException e) {
throw new MojoExecutionException("Unable to read " + setup.getPath(),e);
} catch (InterruptedException e) {
throw new MojoExecutionException("Unable to execute python " + setup.getPath(),e);
}


}
private static final String PROJECT_NAME = "${PROJECT_NAME}";
private static final String VERSION = "${VERSION}";

/**
* @parameter default-value="${project.version}"
* @required
*/
private String packageVersion;

/**
* Allows overriding the default version
*/
@Getter
@Setter
private String version;

/**
* @parameter default-value="${project}"
* @required
* @readonly
*/
private MavenProject project;

/**
* @parameter default-value="${project.basedir}/src/main/python"
* @required
*/
private String sourceDirectory;

/**
* @parameter default-value="${project.artifactId}"
* @required
*/
private String packageName;

/**
* @parameter default-value="python"
* @required
*/
private String pythonExecutable;

/* (non-Javadoc)
* @see org.apache.maven.plugin.AbstractMojo#execute()
*/
public void execute() throws MojoExecutionException, MojoFailureException {

if (version != null) {
packageVersion = version;
}

//Copy sourceDirectory
final File sourceDirectoryFile = new File(sourceDirectory);
final File buildDirectory = Paths.get(project.getBuild().getDirectory(), "py").toFile();

try {
FileUtils.copyDirectory(sourceDirectoryFile, buildDirectory);
} catch (IOException e) {
throw new MojoExecutionException("Failed to copy source", e);
}

final File setup = Paths.get(buildDirectory.getPath(), "setup.py").toFile();
final boolean setupProvided = setup.exists();

final File setupTemplate = setupProvided ? setup : Paths.get(buildDirectory.getPath(), "setup-template.py").toFile();

try {
if (!setupProvided) {
//update VERSION to latest version
List<String> lines = new ArrayList<String>();
final InputStream inputStream = new BufferedInputStream(new FileInputStream(setupTemplate));
try {
lines.addAll(IOUtils.readLines(inputStream));
} finally {
inputStream.close();
}

int index = 0;
for (String line : lines) {
line = line.replace(VERSION, packageVersion);
line = line.replace(PROJECT_NAME, packageName);
lines.set(index, line);
index++;
}

final OutputStream outputStream = new FileOutputStream(setup);
try {
IOUtils.writeLines(lines, "\n", outputStream);
} finally {
outputStream.flush();
outputStream.close();
}
}

//execute setup script
ProcessBuilder processBuilder = new ProcessBuilder(pythonExecutable, setup.getCanonicalPath(), "bdist_egg");
processBuilder.directory(buildDirectory);
processBuilder.redirectErrorStream(true);

Process pr = processBuilder.start();
int exitCode = pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line = buf.readLine()) != null) {
getLog().info(line);
}

if (exitCode != 0) {
throw new MojoExecutionException("python setup.py returned error code " + exitCode);
}

} catch (FileNotFoundException e) {
throw new MojoExecutionException("Unable to find " + setup.getPath(), e);
} catch (IOException e) {
throw new MojoExecutionException("Unable to read " + setup.getPath(), e);
} catch (InterruptedException e) {
throw new MojoExecutionException("Unable to execute python " + setup.getPath(), e);
}


}
}
Loading