Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
Resolve cherry pick conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
rculbertson authored and regadas committed Feb 7, 2019
1 parent eec9cb3 commit ad18d8e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ public static URI downloadIfNonLocal(final URI path) throws IOException {
return copyDir(src, temp, true).toUri();
}

static Path copyDir(final Path src, final Path dest, final boolean overwrite)
throws IOException {
final List<URI> uris = Files.walk(src)
.filter(path -> !path.equals(src))
.map(Path::toUri)
.collect(Collectors.toList());

for (final URI uri : uris) {
final String relative =
uri.toString().substring(src.toUri().toString().length(), uri.toString().length());
final Path fullDst = Paths.get(dest.toUri().resolve(relative));
final CopyOption[] flags = overwrite ? new CopyOption[]{StandardCopyOption.REPLACE_EXISTING}
: new CopyOption[]{};
Files.copy(Paths.get(uri), fullDst, flags);
static Path copyDir(final Path src, final Path dest, final boolean overwrite) throws IOException {
final List<Path> paths =
Files.walk(src).filter(path -> !path.equals(src)).collect(Collectors.toList());

for (final Path path : paths) {
final Path relative = src.relativize(path);
// The 'resolve' method can be passed a String or a Path - we must pass String. If copying
// from a jar file, the relative path will be ZipPath, while our destination directory will
// be UnixPath. This difference will cause resolve to throw ProviderMismatchException, so
// we must first convert relative to String.
final Path fullDst = dest.resolve(relative.toString());
final CopyOption[] flags =
overwrite ? new CopyOption[] {StandardCopyOption.REPLACE_EXISTING} : new CopyOption[] {};
Files.copy(path, fullDst, flags);
}

return dest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

package com.spotify.zoltar.fs;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
Expand All @@ -35,7 +35,6 @@
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -54,10 +53,7 @@ public void localPath() throws IOException {

@Test
public void jarPath() throws IOException {
final String file = getClass().getResource("/model.jar").getFile();
final URI uri = URI.create(String.format("jar:file:%s!/tensorflow/", file));
final Path path = FileSystemExtras.path(uri);
assertThat(path, notNullValue());
assertThat(pathForJar(), notNullValue());
}

@Test(expected = IllegalArgumentException.class)
Expand Down Expand Up @@ -98,27 +94,38 @@ public void noCopyIfDefaultFileSystem() throws IOException {
assertFalse(new File(uri).getName().startsWith("zoltar"));
}

@Test
public void copyDirectoryFromJar() throws IOException {
final Path src = pathForJar();
final Path dest = Files.createTempDirectory("zoltar-");
final File file = FileSystemExtras.copyDir(src, dest, true).toFile();
file.deleteOnExit();
checkCopiedDirectory(file);
}

@Test
public void copyDirectory() throws IOException, URISyntaxException {
final URI resource = getClass().getResource("/trained_model").toURI();
final String abspath = new File(resource).getAbsolutePath();
final Path model = Paths.get(abspath);

final Path src = new File(resource).toPath();
final Path dest = Files.createTempDirectory("zoltar-");
final Path path = FileSystemExtras.copyDir(model, dest, true);

assertTrue(path.toFile().exists());
assertTrue(path.toFile().isDirectory());
assertTrue(path.toFile().getName().startsWith("zoltar"));
final File file = FileSystemExtras.copyDir(src, dest, true).toFile();
file.deleteOnExit();
checkCopiedDirectory(file);
}

final List<String> dirContents = Arrays.stream(path.toFile().listFiles())
.map(File::getName)
.collect(Collectors.toList());
private void checkCopiedDirectory(final File file) {
assertTrue(file.exists());
assertTrue(file.isDirectory());

final List<String> expected = Arrays.asList("variables", "saved_model.pb", "trained_model.txt");
final List<String> dirContents =
Arrays.stream(file.listFiles()).map(File::getName).collect(Collectors.toList());

assertThat(dirContents.containsAll(expected), is(true));
assertThat(dirContents, containsInAnyOrder("variables", "saved_model.pb", "trained_model.txt"));
}

path.toFile().deleteOnExit();
private Path pathForJar() throws IOException {
final String file = getClass().getResource("/trained_model.jar").getFile();
final URI uri = URI.create(String.format("jar:file:%s!/", file));
return FileSystemExtras.path(uri);
}
}
Binary file removed zoltar-tests/src/test/resources/model.jar
Binary file not shown.
Binary file added zoltar-tests/src/test/resources/trained_model.jar
Binary file not shown.

0 comments on commit ad18d8e

Please sign in to comment.