Skip to content

Commit

Permalink
Implementing distribution package building in platform independent ma…
Browse files Browse the repository at this point in the history
…nner. xargs has various set of command line options on linux/windows/macos. Windows has command line length restriction = 2048 symbols.
  • Loading branch information
barancev committed Oct 26, 2017
1 parent e649643 commit edd24f7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
4 changes: 2 additions & 2 deletions java/client/src/org/openqa/selenium/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ java_binary(
genrule(
name = 'client-combined-sources',
out = 'client-combined-' + SE_VERSION + '-sources.jar',
cmd = 'mkdir temp && echo $(query_paths "inputs(kind(java_library, deps(//java/client/src/org/openqa/selenium:client-combined)))") | xargs $(exe //java/client/src/org/openqa/selenium/tools:package) $OUT',
cmd = 'mkdir temp && $(exe //java/client/src/org/openqa/selenium/tools:package) $OUT $(@query_paths "inputs(kind(java_library, deps(//java/client/src/org/openqa/selenium:client-combined)))")',
)

# So we hide it in another zip file, which will be merged. Zip file merging isn't recursive.
Expand All @@ -243,6 +243,6 @@ zip_file(
genrule(
name = 'client-libs',
out = 'libs-sources.jar',
cmd = 'mkdir libs && echo $(classpath :client-combined) | tr : "\\n" | grep third_party/java | grep .jar | xargs -J % cp % libs && jar cMf $OUT libs/*',
cmd = 'mkdir libs && python -c "import sys,shutil;map(lambda f:shutil.copy(f,\'libs\'),[l for l in open(sys.argv[1][1:]).read().split(\';\' if sys.platform.startswith(\'win\') else \':\') if l.endswith(\'.jar\') and \'third_party/java\' in l.replace(\'\\\\\',\'/\')])" $(@classpath :client-combined) && jar cMf $OUT libs/*',

This comment has been minimized.

Copy link
@mach6

mach6 Nov 1, 2017

Member

@barancev this change (and the one to java/server/.../BUCK) breaks the build for me on macOS. The error is below. What version of python are you expecting to be in place? My build environment is using Python 2.7.10. Perhaps it is not python related?

BUILD FAILED: //java/client/src/org/openqa/selenium:client-libs failed with exit code 1:
genrule
stderr:   File "<string>", line 1
    import sys,shutil;map(lambda f:shutil.copy(f,'libs'),[l for l in open(sys.argv[1][1:]).read().split(';' if sys.platform.startswith('win') else ':') if l.endswith('.jar') and 'third_party/java' in l.replace('\','/')])
                                                                                                                                                                                                                           ^
SyntaxError: EOL while scanning string literal

Also FYI, since I'm building on a *nix compatible environment, I hacked the code, changing in l.replace(\'\\\\\',\'/\')])" to in l.replace(\'\',\'\')])".

It built successfully, but is not a real fix..

This comment has been minimized.

Copy link
@barancev

barancev Nov 2, 2017

Author Member

Another attempt to make it work in all environments: d59647e

This comment has been minimized.

Copy link
@mach6

mach6 Nov 2, 2017

Member

👍 works for me! thanks!

)

1 change: 1 addition & 0 deletions java/client/src/org/openqa/selenium/tools/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ java_library(
],
deps = [
"//third_party/java/javaparser:javaparser-core",
"//third_party/java/guava:guava",
],
)

Expand Down
45 changes: 30 additions & 15 deletions java/client/src/org/openqa/selenium/tools/PackageParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.tools;

import com.google.common.collect.ImmutableMap;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseProblemException;
import com.github.javaparser.ast.CompilationUnit;
Expand All @@ -29,6 +31,8 @@
import java.util.Calendar;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

Expand All @@ -40,32 +44,43 @@ public class PackageParser {
TIME_STAMP = c.getTimeInMillis();
}


public static void main(String[] args) throws IOException {
Path out = Paths.get(args[0]);

Map<String, Path> outToIn = new TreeMap<>();

for (int i = 1; i < args.length; i++) {
Path source = Paths.get(args[i]);
if (!source.getFileName().toString().endsWith(".java")) {
continue;
if (args[i].startsWith("@")) {
Path macro = Paths.get(args[i].substring(1));
Stream.of(Files.lines(macro).collect(Collectors.joining(" ")).split(" "))
.forEach(line -> outToIn.putAll(processSingleSourceFile(Paths.get(line))));

} else {
outToIn.putAll(processSingleSourceFile(Paths.get(args[i])));
}
}

try {
CompilationUnit unit = JavaParser.parse(source);
String packageName = unit.getPackageDeclaration()
.map(decl -> decl.getName().asString())
.orElse("");
Path target = Paths.get(packageName.replace('.', File.separatorChar))
.resolve(source.getFileName());
zip(outToIn, out);
}

outToIn.put(target.toString(), source);
} catch (ParseProblemException ignored) {
// carry on
}
private static Map<String, Path> processSingleSourceFile(Path source) {
if (!source.getFileName().toString().endsWith(".java")) {
return ImmutableMap.of();
}
try {
CompilationUnit unit = JavaParser.parse(source);
String packageName = unit.getPackageDeclaration()
.map(decl -> decl.getName().asString())
.orElse("");
Path target = Paths.get(packageName.replace('.', File.separatorChar))
.resolve(source.getFileName());
return ImmutableMap.of(target.toString(), source);
} catch (ParseProblemException|IOException ignored) {
return ImmutableMap.of();
}
}

private static void zip(Map<String, Path> outToIn, Path out) throws IOException {
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(out))) {
outToIn.forEach((target, source) -> {
ZipEntry entry = new ZipEntry(target);
Expand Down
4 changes: 2 additions & 2 deletions java/server/src/org/openqa/grid/selenium/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ java_binary(
genrule(
name = 'selenium-server-sources',
out = 'selenium-' + SE_VERSION + '-nodeps-sources.jar',
cmd = 'mkdir temp && echo $(query_paths "inputs(kind(java_library, deps(:selenium)))") | xargs $(exe //java/client/src/org/openqa/selenium/tools:package) $OUT',
cmd = 'mkdir temp && $(exe //java/client/src/org/openqa/selenium/tools:package) $OUT $(@query_paths "inputs(kind(java_library, deps(:selenium)))")',
)

# So we hide it in another zip file, which will be merged. Zip file merging isn't recursive.
Expand All @@ -91,5 +91,5 @@ zip_file(
genrule(
name = 'server-libs',
out = 'server-libs-sources.jar',
cmd = 'mkdir libs && echo $(classpath :selenium) | tr : "\\n" | grep third_party/java | grep .jar | xargs -J % cp % libs && jar cMf $OUT libs/*',
cmd = 'mkdir libs && python -c "import sys,shutil;map(lambda f:shutil.copy(f,\'libs\'),[l for l in open(sys.argv[1][1:]).read().split(\';\' if sys.platform.startswith(\'win\') else \':\') if l.endswith(\'.jar\') and \'third_party/java\' in l.replace(\'\\\\\',\'/\')])" $(@classpath :selenium) && jar cMf $OUT libs/*',
)

0 comments on commit edd24f7

Please sign in to comment.