Skip to content
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

Add new cacheWheels:boolean configuration to the habushu-maven-plugin that caches build wheel files. #75

Merged
merged 5 commits into from
Dec 27, 2023
Merged
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
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,12 @@ Folder in which Python test files are located - should align with Poetry's proje

Default: `${project.basedir}/tests`

#### cacheWheels ####

Enables or Disables the copying of wheels into Poetry cache.

Default: `false`

#### managedDependencies ####

Optional set of dependencies to manage across modules extending a parent pom. This allows packages to be managed to a
Expand Down Expand Up @@ -692,5 +698,4 @@ If you are working on Habushu, please be aware of some nuances in working with a

* `mvn clean install -Pbootstrap`: Builds the `habushu-maven-plugin` such that the custom `habushu` lifecycle may be utilized within subsequent builds.
* **NOTE:** If updates are made to the `habushu` lifecycle (i.e. updates to the `habushu` lifecycle mapping configuration made in `habushu-maven-plugin/src/main/resources/META-INF/plexus/components.xml`), developers **MUST** changes require two builds to test - one to build the lifecycle, then a second to use that updated lifecycle. Code changes to `Mojo` classes within the existing `habushu` lifecycle work via normal builds without the need for a second pass.
* `mvn clean install -Pdefault`: (ACTIVE BY DEFAULT - `-Pdefault` does not need to be specified) builds all modules. Developers may use this profile to build and apply changes to existing `habushu-maven-plugin` `Mojo` classes

* `mvn clean install -Pdefault`: (ACTIVE BY DEFAULT - `-Pdefault` does not need to be specified) builds all modules. Developers may use this profile to build and apply changes to existing `habushu-maven-plugin` `Mojo` classes
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package org.technologybrewery.habushu;

import com.electronwill.nightconfig.core.Config;
import com.electronwill.nightconfig.core.conversion.Path;
import com.electronwill.nightconfig.core.file.FileConfig;

import org.apache.commons.io.FileUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.technologybrewery.habushu.exec.PoetryCommandHelper;
import org.technologybrewery.habushu.util.HabushuUtil;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Delegates to Poetry during the {@link LifecyclePhase#PACKAGE} build phase to
Expand All @@ -37,6 +46,12 @@ public class BuildDeploymentArtifactsMojo extends AbstractHabushuMojo {
@Parameter(property = "habushu.exportRequirementsFile", required = false, defaultValue = "true")
protected boolean exportRequirementsFile;

/**
* By default, do not cache wheel (*.whl) file(s).
*/
@Parameter(property = "habushu.cacheWheels", required = false, defaultValue = "false")
protected boolean cacheWheels;

/**
* By default, do not include the --without-urls flag when exporting.
*/
Expand Down Expand Up @@ -112,6 +127,10 @@ public void doExecute() throws MojoExecutionException, MojoFailureException {

setUpPlaceholderFileAsMavenArtifact();
}

if(cacheWheels){
cacheWheelFile();
}
}

private void logLocalMonorepoCaveats() {
Expand Down Expand Up @@ -149,4 +168,25 @@ protected void setUpPlaceholderFileAsMavenArtifact() {
project.getArtifact().setFile(mavenArtifactFile);
}

}
private void cacheWheelFile() {
PoetryCommandHelper poetryHelper = createPoetryCommandHelper();
try{
File wheelSourceDirectory = new File(project.getBuild().getDirectory());
String poetryCacheDirectoryPath = poetryHelper.getPoetryCacheDirectoryPath();
File poetryWheelCacheDirectory = new File(String.format("%s/cache/repositories/wheels/%s", poetryCacheDirectoryPath, project.getArtifactId()));
//conditional will throw an error if cache directory isn't created
if(poetryWheelCacheDirectory.exists() || poetryWheelCacheDirectory.mkdirs()){
JeffreyRoss marked this conversation as resolved.
Show resolved Hide resolved
List<File> wheelFiles = Stream.of(wheelSourceDirectory.listFiles())
.filter(file -> file.getAbsolutePath().endsWith(".whl"))
.map(File::getAbsoluteFile)
.collect(Collectors.toList());
for(File file : wheelFiles){
HabushuUtil.copyFile(file.getPath(), String.format("%s/%s", poetryWheelCacheDirectory.toPath().toString(), file.getName()));
getLog().info(String.format("Cached the %s file", file.getName()));
JeffreyRoss marked this conversation as resolved.
Show resolved Hide resolved
}
}
} catch (Exception e){
throw new HabushuException("Could not cache the " + project.getArtifactId() + " wheel file(s)!", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Collectors;
import java.io.InputStreamReader;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
Expand Down Expand Up @@ -57,6 +58,16 @@ public Pair<Boolean, String> getIsPoetryInstalledAndVersion() {
}
}

/**
* Returns a {@link String} indicating the relative path to the poetry
* cache directory. This is equivalent to {@code poetry config cache-dir}.
*
* @return
*/
public String getPoetryCacheDirectoryPath() throws MojoExecutionException {
return execute(Arrays.asList("config", "cache-dir"));
}

/**
* Returns whether the specified dependency package is installed within this
* Poetry project's virtual environment (and pyproject.toml).
Expand Down Expand Up @@ -203,4 +214,4 @@ protected ProcessExecutor createPoetryExecutor(List<String> arguments) {
fullCommandArgs.addAll(arguments);
return new ProcessExecutor(workingDirectory, fullCommandArgs, Platform.guess(), null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.io.FileUtils;
import org.apache.maven.settings.Server;
import org.apache.maven.settings.Settings;
import org.technologybrewery.habushu.HabushuException;
Expand Down Expand Up @@ -181,4 +182,20 @@ public static void giveFullFilePermissions(String filePath) {
}
}

}
/**
* Copies specified file into specified path.
*
* @param filePath the path to the file to copy
* @param destinationFilePath the path to where the new copy should be created
*/
public static void copyFile(String sourceFilePath, String destinationFilePath) {
try{
File sourceFile = new File(sourceFilePath);
File destinationFile = new File(destinationFilePath);
FileUtils.copyFile(sourceFile, destinationFile);
} catch(IOException ioe){
throw new HabushuException("Could not copy the file ["+ sourceFilePath +"] to [" + destinationFilePath +"]!", ioe);
}

}
}
3 changes: 3 additions & 0 deletions habushu-mixology/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<plugin>
<groupId>org.technologybrewery.habushu</groupId>
<artifactId>habushu-maven-plugin</artifactId>
<configuration>
<cacheWheels>true</cacheWheels>
</configuration>
</plugin>
</plugins>
</build>
Expand Down
Loading