Skip to content

Commit

Permalink
Merge pull request #2 from TV4Fun/master
Browse files Browse the repository at this point in the history
Add deploy option and some other changes.
  • Loading branch information
UltimateDogg authored Feb 28, 2018
2 parents 2b7b3cb + 0012205 commit e772579
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 129 deletions.
64 changes: 48 additions & 16 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ which is useful for IT shops that develop in both of these languages.
Functionality
-------------

* keeps the *setup.py* version in sync with the Maven project version by updating setup.py in the **process-sources** phase
* packages the Python module during the Maven **package** phase
* allows specifying which format should the Python module be distributed as: source, RPM, egg, tar, zip, etc.
* Keeps the *setup.py* version in sync with the Maven project version by updating setup.py in the **process-sources** phase.
* Packages the Python module during the Maven **package** phase.
* Allows specifying which format should the Python module be distributed as: source, RPM, egg, tar, zip, etc.
* Supports uploading packages to PyPI during the **deploy** phase.

Configuration
-------------

Add the following to your *pom.xml* build section:
::
Add the following to your *pom.xml* build section::

<plugin>
<groupId>maven-python-mojos</groupId>
Expand All @@ -43,19 +43,59 @@ Add the following to your *pom.xml* build section:
</executions>
</plugin>

This defaults to building an egg file. If you would like to use another distribution type, you may specify something else::

<execution>
<id>package</id>
<goals>
<goal>package</goal>
</goals>
<configuration>
<distributionType>rpm</distributionType>
</configuration>
</execution>

If you wish to build multiple different distribtion types, you can add multiple ``<execution>`` blocks with different types.
Supported types are egg, wheel, wininst, rpm, bdist, dumb, source, or docs.

If you wish to upload your packaged files to PyPI, add the following::

<execution>
<id>deploy</id>
<goals>
<goal>deploy</goal>
</goals>
</execution>

This will default to uploading all of the packages generated by the **package** goal. If you don't want that, you can specify
a distribution type in the same way as you can for packaging. The deploy goal supports all distribution types except for docs.

You can also optionally specify a repository to upload to::

<execution>
<id>deploy</id>
<goals>
<goal>deploy</goal>
</goals>
<configuration>
<repository>https://pypi.myserver.com</repository>
</configuration>
</execution>

setup.py
--------

To make the code runnable outside maven you can have a setup.py. If a setup-template.py is there in
your source root setup.py will be replaced.

setup-template.py
--------
-----------------

setup template allows for using maven controlled variables in your setup.py file.
Setup template allows for using maven controlled variables in your setup.py file.
Set the *version* field in your *setup-template.py* to a hardcoded constant of **${VERSION}**, e.g.
Set the *name* field in your *setup-template.py* to a hardcoded constant of **${PROJECT_NAME}**, e.g.
::

from setuptools import setup, find_packages
setup(
Expand All @@ -65,22 +105,14 @@ Set the *name* field in your *setup-template.py* to a hardcoded constant of **${
packages = find_packages('.')
)


Maven Repository
----------------

Add the following plugin repository to your *pom.xml* in order to use this plugin:

::
Add the following plugin repository to your *pom.xml* in order to use this plugin::

<pluginRepositories>
<pluginRepository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</pluginRepository>
</pluginRepositories>





Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package com.github.mojos.distribute;

/*
* Copyright 2001-2018 The Apache Software Foundation.
*
* 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.
*/

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Abstract class to run a setup.py command
*/
public abstract class AbstractSetupCommandMojo extends AbstractMojo {
/**
* @parameter default-value="${project}"
* @required
* @readonly
*/
private MavenProject project;

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

/**
* Implementations should add any commands and arguments they wish to pass to setup.py here.
*
* @param args List to add setup.py commands to.
* @throws MojoExecutionException
*/
abstract void addSetupArgs(List<String> args) throws MojoExecutionException;

/* (non-Javadoc)
* @see org.apache.maven.plugin.AbstractMojo#execute()
*/
@Override
public void execute() throws MojoExecutionException {
final File buildDirectory = Paths.get(project.getBuild().getDirectory(), "maven-python").toFile();
final String setupOutputCanonicalPath = project.getProperties().getProperty("python.distribute.plugin.setup.path", "src/main/python/setup.py");

try {
List<String> args = new ArrayList<>();
args.add(pythonExecutable);
args.add(setupOutputCanonicalPath);
addSetupArgs(args);
//execute setup script
ProcessBuilder processBuilder = new ProcessBuilder(args.toArray(new String[args.size()]));
processBuilder.directory(buildDirectory);

Process pr = processBuilder.start();
BufferedReader stdout = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader stderr = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
while (!pr.waitFor(100, TimeUnit.MILLISECONDS)) {
stdout.lines().forEachOrdered(line -> getLog().debug(line));
stderr.lines().forEachOrdered(line -> getLog().warn(line));
}

stdout.lines().forEachOrdered(line -> getLog().debug(line));
stderr.lines().forEachOrdered(line -> getLog().warn(line));

int exitCode = pr.exitValue();
if (exitCode != 0) {
throw new MojoExecutionException("'" + String.join(" ", processBuilder.command()) + "' returned error code " + exitCode);
}
} catch (FileNotFoundException e) {
throw new MojoExecutionException("Unable to find " + setupOutputCanonicalPath, e);
} catch (IOException e) {
throw new MojoExecutionException("Unable to read " + setupOutputCanonicalPath, e);
} catch (InterruptedException e) {
throw new MojoExecutionException("Unable to execute python " + setupOutputCanonicalPath, e);
}
}

private void logErrorOrWarning(String line) {
if (line.toLowerCase().contains("error"))
getLog().error(line);
else
getLog().warn(line);
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/github/mojos/distribute/DeployMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.github.mojos.distribute;

/*
* Copyright 2001-2018 The Apache Software Foundation.
*
* 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.
*/

import org.apache.maven.plugin.MojoExecutionException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Uploads a Python module to PyPI using distribute
*
* @goal deploy
* @phase deploy
*/
public class DeployMojo extends AbstractSetupCommandMojo {
/**
* @parameter
*/
private String repository;

/**
* @parameter
*/
private String distributionType;

static List<String> builtDistributionTypes = Collections.synchronizedList(new ArrayList<>());

@Override
void addSetupArgs(List<String> args) throws MojoExecutionException {
if (distributionType != null) {
args.add(PackageMojo.getDistributionTypeArg(distributionType));
} else {
args.addAll(builtDistributionTypes);
}
args.add("upload");
if (repository != null) {
args.add("-r");
args.add(repository);
}
}
}
54 changes: 8 additions & 46 deletions src/main/java/com/github/mojos/distribute/InstallMojo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.mojos.distribute;

/*
* Copyright 2001-2005 The Apache Software Foundation.
* Copyright 2001-2018 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,55 +16,17 @@
* limitations under the License.
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import java.util.List;

/**
* Installs a Python module using distribute
*
*
* @goal install
* @phase install
*/
public class InstallMojo extends AbstractMojo {

/* (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 {
//execute setup script
ProcessBuilder t = new ProcessBuilder("python","setup.py","install");
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 install' 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);
}
}
public class InstallMojo extends AbstractSetupCommandMojo {
@Override
void addSetupArgs(List<String> args) {
args.add("install");
}
}
Loading

0 comments on commit e772579

Please sign in to comment.