Skip to content

Commit

Permalink
[#22]: Allow using include/exclude with the collector
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed May 14, 2018
1 parent c08ea68 commit 9b60069
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 66 deletions.
99 changes: 99 additions & 0 deletions src/it/test6-scanner/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.dentrassi.maven.rpm.test</groupId>
<artifactId>test2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Test Package #6</name>
<description>
Test the directory scanner
</description>

<url>http://dentrassi.de</url>

<organization>
<name>Jens Reimann</name>
<url>http://dentrassi.de</url>
</organization>

<licenses>
<license>
<name>Eclipse Public License - v 1.0</name>
<distribution>repo</distribution>
<url>https://www.eclipse.org/legal/epl-v10.html</url>
</license>
</licenses>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<skipSigning>true</skipSigning>
</properties>

<build>

<plugins>
<plugin>
<groupId>de.dentrassi.maven</groupId>
<artifactId>rpm</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<goals>
<goal>rpm</goal>
</goals>
<configuration>
<attach>false</attach>
<group>Application/Misc</group>

<signature>
<keyId>${keyId}</keyId>
<keyringFile>${user.home}/.gnupg/secring.gpg</keyringFile>
<passphrase>${passphrase}</passphrase>
<hashAlgorithm>SHA1</hashAlgorithm>
<skip>${skipSigning}</skip>
</signature>

<outputFileName>test6.rpm</outputFileName>

<entries>
<entry>
<name>/usr/share/test6</name>
<collect>
<from>src/main/data</from>
<includes>
<include>*.foo</include>
<include>include/**</include>
</includes>
<excludes>
<exclude>bar.*</exclude>
<exclude>exclude/**</exclude>
</excludes>
</collect>
</entry>
</entries>

</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>sign</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<skipSigning>false</skipSigning>
</properties>
</profile>
</profiles>

</project>
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
12 changes: 12 additions & 0 deletions src/it/test6-scanner/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

Process rpm = ("rpm -qlp " + basedir + "/target/test6.rpm").execute();

def entries = rpm.in.readLines();

println entries;

return entries.equals( [
"/usr/share/test6/a.foo",
"/usr/share/test6/include",
"/usr/share/test6/include/d.bar"
] );
26 changes: 25 additions & 1 deletion src/main/java/de/dentrassi/rpm/builder/PackageEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public static class Collector

private boolean symbolicLinks = false;

private String[] includes;

private String[] excludes;

public File getFrom ()
{
return this.from;
Expand Down Expand Up @@ -52,10 +56,30 @@ public boolean isSymbolicLinks ()
return this.symbolicLinks;
}

public void setIncludes ( final String[] includes )
{
this.includes = includes;
}

public String[] getIncludes ()
{
return this.includes;
}

public void setExcludes ( final String[] excludes )
{
this.excludes = excludes;
}

public String[] getExcludes ()
{
return this.excludes;
}

@Override
public String toString ()
{
return String.format ( "[collector - from: %s, directories: %s, symLinks: %s]", this.from, this.directories, this.symbolicLinks );
return String.format ( "[collector - from: %s, directories: %s, symLinks: %s, includes: %s, excludes: %s]", this.from, this.directories, this.symbolicLinks, this.includes, this.excludes );
}
}

Expand Down
152 changes: 88 additions & 64 deletions src/main/java/de/dentrassi/rpm/builder/RpmMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,16 @@
package de.dentrassi.rpm.builder;

import static com.google.common.io.Files.readFirstLine;
import static java.nio.file.Files.walkFileTree;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
Expand All @@ -51,6 +47,7 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.codehaus.plexus.util.DirectoryScanner;
import org.eclipse.packagedrone.utils.rpm.Architecture;
import org.eclipse.packagedrone.utils.rpm.HashAlgorithm;
import org.eclipse.packagedrone.utils.rpm.OperatingSystem;
Expand Down Expand Up @@ -776,87 +773,114 @@ private void fillFromEntry ( final BuilderContext ctx, final PackageEntry entry

if ( entry.getDirectory () != null && entry.getDirectory () )
{
this.logger.debug ( " as directory:" );
ctx.addDirectory ( entry.getName (), makeProvider ( entry, " - " ) );
fillFromEntryDirectory ( ctx, entry );
}
else if ( entry.getFile () != null )
{
this.logger.debug ( " as file:" );
final Path source = entry.getFile ().toPath ().toAbsolutePath ();
this.logger.debug ( " - source: %s", source );

ctx.addFile ( entry.getName (), source, makeProvider ( entry, " - " ) );
fillFromEntryFile ( ctx, entry );
}
else if ( entry.getLinkTo () != null )
{
this.logger.debug ( " as symbolic link:" );
this.logger.debug ( " - linkTo: %s", entry.getLinkTo () );
ctx.addSymbolicLink ( entry.getName (), entry.getLinkTo (), makeProvider ( entry, " - " ) );
fillFromEntryLinkTo ( ctx, entry );
}
else if ( entry.getCollect () != null )
{
this.logger.debug ( " as collector:" );
fillFromEntryCollect ( ctx, entry );
}
}

private void fillFromEntryDirectory ( final BuilderContext ctx, final PackageEntry entry ) throws IOException
{
this.logger.debug ( " as directory:" );
ctx.addDirectory ( entry.getName (), makeProvider ( entry, " - " ) );
}

private void fillFromEntryFile ( final BuilderContext ctx, final PackageEntry entry ) throws IOException
{
this.logger.debug ( " as file:" );
final Path source = entry.getFile ().toPath ().toAbsolutePath ();
this.logger.debug ( " - source: %s", source );

ctx.addFile ( entry.getName (), source, makeProvider ( entry, " - " ) );
}

private void fillFromEntryLinkTo ( final BuilderContext ctx, final PackageEntry entry ) throws IOException
{
this.logger.debug ( " as symbolic link:" );
this.logger.debug ( " - linkTo: %s", entry.getLinkTo () );
ctx.addSymbolicLink ( entry.getName (), entry.getLinkTo (), makeProvider ( entry, " - " ) );
}

private void fillFromEntryCollect ( final BuilderContext ctx, final PackageEntry entry ) throws IOException
{
this.logger.debug ( " as collector:" );

final Collector collector = entry.getCollect ();
final Collector collector = entry.getCollect ();

this.logger.debug ( " - configuration: %s", collector );
this.logger.debug ( " - configuration: %s", collector );

final String padding = " ";
final String padding = " ";

final Path from = collector.getFrom ().toPath ();
final String targetPrefix = entry.getName ().endsWith ( "/" ) ? entry.getName () : entry.getName () + "/";
final Path from = collector.getFrom ().toPath ();
final String targetPrefix = entry.getName ().endsWith ( "/" ) ? entry.getName () : entry.getName () + "/";

this.logger.debug ( " - files:" );
this.logger.debug ( " - files:" );

final MojoFileInformationProvider provider = makeProvider ( entry, " - " );
final MojoFileInformationProvider provider = makeProvider ( entry, " - " );

walkFileTree ( from, new SimpleFileVisitor<Path> () {
@Override
public FileVisitResult visitFile ( final Path file, final BasicFileAttributes attrs ) throws IOException
final DirectoryScanner scanner = new DirectoryScanner ();
scanner.setBasedir ( from.toFile () );
scanner.setCaseSensitive ( true );
scanner.setFollowSymlinks ( true );
scanner.setIncludes ( collector.getIncludes () );
scanner.setExcludes ( collector.getExcludes () );
scanner.scan ();

if ( collector.isDirectories () )
{
for ( final String directory : scanner.getIncludedDirectories () )
{
final Path dir = from.resolve ( directory );
if ( dir.equals ( from ) )
{
final Path relative = from.relativize ( file );
final String targetName = makeUnix ( targetPrefix + relative.toString () );

if ( java.nio.file.Files.isSymbolicLink ( file ) )
{
RpmMojo.this.logger.debug ( "%s%s (symlink)", padding, file );
if ( collector.isSymbolicLinks () )
{
final Path sym = java.nio.file.Files.readSymbolicLink ( file );
RpmMojo.this.logger.debug ( "%s%s (symlink)", padding, file );
RpmMojo.this.logger.debug ( "%s - target: %s", padding, targetName );
RpmMojo.this.logger.debug ( "%s - linkTo: %s", padding, sym.toString () );
}
else
{
RpmMojo.this.logger.debug ( "%s%s (symlink) - ignoring symbolic links", padding, file );
}
}
else
{
RpmMojo.this.logger.debug ( "%s%s (file)", padding, file );
RpmMojo.this.logger.debug ( "%s - target: %s", padding, targetName );

ctx.addFile ( targetName, file, provider );
}
return FileVisitResult.CONTINUE;
continue;
}

@Override
public FileVisitResult preVisitDirectory ( final Path dir, final BasicFileAttributes attrs ) throws IOException
RpmMojo.this.logger.debug ( "%s%s (dir)", padding, dir );
final Path relative = from.relativize ( dir );
final String targetName = makeUnix ( targetPrefix + relative.toString () );
RpmMojo.this.logger.debug ( "%s - target: %s", padding, targetName );
ctx.addDirectory ( targetName, provider );
}
}

for ( final String relative : scanner.getIncludedFiles () )
{
final Path file = from.resolve ( relative );
final String targetName = makeUnix ( targetPrefix + relative );

if ( java.nio.file.Files.isSymbolicLink ( file ) )
{
RpmMojo.this.logger.debug ( "%s%s (symlink)", padding, file );
if ( collector.isSymbolicLinks () )
{
final Path sym = java.nio.file.Files.readSymbolicLink ( file );
RpmMojo.this.logger.debug ( "%s%s (symlink)", padding, file );
RpmMojo.this.logger.debug ( "%s - target: %s", padding, targetName );
RpmMojo.this.logger.debug ( "%s - linkTo: %s", padding, sym.toString () );
}
else
{
// only add when we add directories and it is not be base
if ( collector.isDirectories () && !dir.equals ( from ) )
{
RpmMojo.this.logger.debug ( "%s%s (dir)", padding, dir );
final Path relative = from.relativize ( dir );
final String targetName = makeUnix ( targetPrefix + relative.toString () );
RpmMojo.this.logger.debug ( "%s - target: %s", padding, targetName );
ctx.addDirectory ( targetName, provider );
}
return FileVisitResult.CONTINUE;
RpmMojo.this.logger.debug ( "%s%s (symlink) - ignoring symbolic links", padding, file );
}
} );
}
else
{
RpmMojo.this.logger.debug ( "%s%s (file)", padding, file );
RpmMojo.this.logger.debug ( "%s - target: %s", padding, targetName );

ctx.addFile ( targetName, file, provider );
}
}
}

Expand Down
18 changes: 17 additions & 1 deletion src/site/markdown/entry.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Assuming you have the following director tree in the file system:
src/main/resources/dir2/foo3.txt
src/main/resources/dir2/foo4.txt
src/main/resources/dir2/foo5.txt -> foo4.txt (symlink)

Using the collector configuration:

<entry>
Expand All @@ -83,6 +83,22 @@ Will result in the following payload entries:
/usr/lib/foo/dir2/foo3.txt (file)
/usr/lib/foo/dir2/foo4.txt (file)

As of version 1.0.0 it is also possible to use the standard Maven `includes`/`excludes` elements
which follow the standard Maven include/exclude pattern. For example:

<entry>
<name>/usr/lib/foo</name>
<collect>
<from>src/main/resources</from>
<includes>
<include>…</include>
</includes>
<excludes>
<exclude>…</exclude>
</excludes>
</collect>
</entry>

## Entry information

Each payload entry has additional information such as "user", "group", "mode", "configuration", ...
Expand Down

0 comments on commit 9b60069

Please sign in to comment.