Skip to content

Commit

Permalink
Bugfix/relativepaths (#384)
Browse files Browse the repository at this point in the history
* Resolve input paths against input file

* Null check to avoid calling transform in cases with inline input definitions

* Using absolute path in input processing

* Null check for app location
  • Loading branch information
milos-ljubinkovic authored Dec 12, 2017
1 parent a1f38eb commit bb84fb3
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -383,17 +383,23 @@ public static void buildMissingInfo(Object value, HashAlgorithm alg, Path workDi
return;
}
}

if (location == null) {
actual = workDir.resolve(path);
location = actual.toUri().toString();
} else {
actual = Paths.get(URI.create(location));
}
if(!Paths.get(path).isAbsolute()){
path=workDir.resolve(path).toAbsolutePath().toString();
URI temp = URI.create(location);
if (temp.getScheme() != null) {
actual = Paths.get(temp);
} else {
actual = workDir.resolve(path);
}
}

if (!Paths.get(path).isAbsolute()) {
path = workDir.resolve(path).toAbsolutePath().toString();
}

String name = getName(value);
if (name == null) {
setNames(actual, value);
Expand All @@ -402,10 +408,10 @@ public static void buildMissingInfo(Object value, HashAlgorithm alg, Path workDi
path = Paths.get(path).resolveSibling(name).toString();
}
}

setPath(path, value);
setLocation(location, value);

if (getSize(value) == null)
setSize(Files.size(actual), value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,12 @@ public Map<String, Object> updateOutputFiles(Map<String, Object> outputs, FileTr

public Map<String, Object> setFileProperties(Map<String, Object> inputs) throws CWLPortProcessorException {
try {
return portProcessor.processInputs(inputs, new CWLFilePropertiesProcessorCallback(Paths.get(portProcessor.getJob().getApp().getAppFileLocation())));
String appFileLocation = portProcessor.getJob().getApp().getAppFileLocation();
return portProcessor.processInputs(inputs, new CWLFilePropertiesProcessorCallback(Paths.get(appFileLocation == null ? "." : appFileLocation).toAbsolutePath().normalize()));
} catch (CWLPortProcessorException e) {
throw new CWLPortProcessorException("Failed to set input file properties", e);
}
}

public Map<String, Object> setPathsToInputs(Map<String, Object> inputs) throws CWLPortProcessorException {
try {
return portProcessor.processInputs(inputs, new CWLFileLocationToPathProcessorCallback(this.portProcessor.getJob().getApp().getAppFileLocation()));
} catch (CWLPortProcessorException e) {
throw new CWLPortProcessorException("Failed to set paths", e);
}
}

public Map<String, Object> createFileLiteralFiles(Map<String, Object> inputs, Path workingDir) throws CWLPortProcessorException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ public Map<String, Object> stageInputFiles(Map<String, Object> inputs, File work

public Map<String, Object> setFileProperties(Map<String, Object> inputs) throws Draft2PortProcessorException {
try {
return portProcessor.processInputs(inputs, new Draft2FilePropertiesProcessorCallback(Paths.get(draft2Job.getApp().getAppFileLocation())));
String appFileLocation = draft2Job.getApp().getAppFileLocation();
return portProcessor.processInputs(inputs, new Draft2FilePropertiesProcessorCallback(Paths.get(appFileLocation == null ? "." : appFileLocation).toAbsolutePath().normalize()));
} catch (Draft2PortProcessorException e) {
throw new Draft2PortProcessorException("Failed to set input properties.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public Map<String, Object> stageInputFiles(Map<String, Object> inputs, File work

public Map<String, Object> setFileProperties(Map<String, Object> inputs) throws Draft3PortProcessorException {
try {
return portProcessor.processInputs(inputs, new Draft3FilePropertiesProcessorCallback(Paths.get(draft3Job.getApp().getAppFileLocation())));
String appFileLocation = this.draft3Job.getApp().getAppFileLocation();
return portProcessor.processInputs(inputs, new Draft3FilePropertiesProcessorCallback(Paths.get(appFileLocation == null ? "." : appFileLocation).toAbsolutePath().normalize()));
} catch (Draft3PortProcessorException e) {
throw new Draft3PortProcessorException("Failed to set input properties.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void setChecksum(Path actual, Object raw, HashAlgorithm hashAlgori
setValue(KEY_CHECKSUM, checksum, raw);
}
}

public static void setChecksum(String checksum, Object raw) {
setValue(KEY_CHECKSUM, checksum, raw);
}
Expand All @@ -92,7 +92,7 @@ public static void setContents(Object raw) throws IOException {
public static String getContents(Object raw) {
return getValue(KEY_CONTENTS, raw);
}

private static void setContents(String contents, Map<String, Object> raw) {
setValue(KEY_CONTENTS, contents, raw);
}
Expand All @@ -108,19 +108,19 @@ public static String getPath(Object raw) {
public static void setPath(String path, Object raw) {
setValue(KEY_PATH, path, raw);
}

public static String getLocation(Object raw) {
return getValue(KEY_LOCATION, raw);
}

public static void setLocation(String location, Object raw) {
setValue(KEY_LOCATION, location, raw);
}

public static void setOriginalPath(String path, Object raw) {
setValue(KEY_ORIGINAL_PATH, path, raw);
}

public static String getOriginalPath(Object raw) {
return getValue(KEY_ORIGINAL_PATH, raw);
}
Expand Down Expand Up @@ -168,7 +168,7 @@ public static Set<String> flattenPaths(Object value) {
}
return paths;
}

/**
* Load first CONTENTS_NUMBER_OF_BYTES bytes from file
*/
Expand All @@ -179,7 +179,7 @@ private static String loadContents(Object fileData) throws IOException {
try {
File file = new File(path);
is = new FileInputStream(file);
int bufferSize = file.length() > 0 && file.length() < CONTENTS_NUMBER_OF_BYTES ? (int) file.length(): CONTENTS_NUMBER_OF_BYTES;
int bufferSize = file.length() > 0 && file.length() < CONTENTS_NUMBER_OF_BYTES ? (int) file.length() : CONTENTS_NUMBER_OF_BYTES;
byte[] buffer = new byte[bufferSize];
is.read(buffer);
return new String(buffer, "UTF-8");
Expand All @@ -193,7 +193,7 @@ private static String loadContents(Object fileData) throws IOException {
}
}
}

public static FileValue createFileValue(Object value) {
String path = SBFileValueHelper.getPath(value);
String name = SBFileValueHelper.getName(value);
Expand All @@ -202,7 +202,7 @@ public static FileValue createFileValue(Object value) {
String contents = SBFileValueHelper.getContents(value);
String dirname = SBFileValueHelper.getDirname(value);
Long size = SBFileValueHelper.getSize(value);

Map<String, Object> properties = new HashMap<>();
properties.put(SBBindingHelper.KEY_SBG_METADATA, SBFileValueHelper.getMetadata(value));

Expand All @@ -218,10 +218,10 @@ public static FileValue createFileValue(Object value) {
ret.setDirname(dirname);
return ret;
}

public static Map<String, Object> createFileRaw(FileValue fileValue) {
Map<String, Object> raw = new HashMap<>();

setFileType(raw);
setPath(fileValue.getPath(), raw);
setName(fileValue.getName(), raw);
Expand All @@ -235,7 +235,7 @@ public static Map<String, Object> createFileRaw(FileValue fileValue) {
if (properties != null) {
setMetadata(properties.get(SBBindingHelper.KEY_SBG_METADATA), raw);
}

List<FileValue> secondaryFileValues = fileValue.getSecondaryFiles();
if (secondaryFileValues != null) {
List<Map<String, Object>> secondaryFilesRaw = new ArrayList<>();
Expand All @@ -254,13 +254,7 @@ public static void buildMissingInfo(Object value, HashAlgorithm alg, Path workDi

if (path == null) {
if (location != null) {
URI uri = URI.create(location);
if (uri.getScheme() == null) {
uri = new URI("file", location, null);
}
if (uri.isOpaque()) {
uri = new URI("file", workDir.resolve(uri.getSchemeSpecificPart()).toAbsolutePath().toString(), null);
}
URI uri = createFullURI(location, workDir);
location = uri.toString();
actual = Paths.get(uri);
if (!actual.isAbsolute()) {
Expand All @@ -271,17 +265,17 @@ public static void buildMissingInfo(Object value, HashAlgorithm alg, Path workDi
return;
}
}

if (location == null) {
actual = workDir.resolve(path).toAbsolutePath();
location = actual.toUri().toString();
} else {
actual = Paths.get(URI.create(location));
actual = Paths.get(createFullURI(location, workDir));
}
if(!Paths.get(path).isAbsolute()){
path=workDir.resolve(path).toAbsolutePath().toString();
if (!Paths.get(path).isAbsolute()) {
path = workDir.resolve(path).toAbsolutePath().toString();
}

String name = getName(value);
if (name == null) {
setNames(actual, value);
Expand All @@ -290,10 +284,10 @@ public static void buildMissingInfo(Object value, HashAlgorithm alg, Path workDi
path = Paths.get(path).resolveSibling(name).toString();
}
}

setPath(path, value);
setLocation(location, value);

if (getSize(value) == null && Files.exists(actual)) {
setSize(Files.size(actual), value);
if (alg != null)
Expand All @@ -311,6 +305,17 @@ public static void buildMissingInfo(Object value, HashAlgorithm alg, Path workDi
}
}

private static URI createFullURI(String val, Path parent) throws URISyntaxException {
URI uri = URI.create(val);
if (uri.getScheme() == null) {
uri = new URI("file", val, null);
}
if (uri.isOpaque()) {
uri = new URI("file", parent.resolve(uri.getSchemeSpecificPart()).toAbsolutePath().toString(), null);
}
return uri;
}

private static void setNames(Path path, Object value) throws IOException {
String name = path.getFileName().toString();
if (getName(value) == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public Map<String, Object> setInputSecondaryFiles(Map<String, Object> inputs, SB

public Map<String, Object> setFileProperties(Map<String, Object> inputs) throws SBPortProcessorException {
try {
return portProcessor.processInputs(inputs, new SBFilePropertiesProcessorCallback(Paths.get(sbJob.getApp().getAppFileLocation())));
String appFileLocation = sbJob.getApp().getAppFileLocation();
return portProcessor.processInputs(inputs, new SBFilePropertiesProcessorCallback(Paths.get(appFileLocation == null ? "." : appFileLocation).toAbsolutePath().normalize()));
} catch (SBPortProcessorException e) {
throw new SBPortProcessorException("Failed to set inputs properties.", e);
}
Expand Down
15 changes: 15 additions & 0 deletions rabix-cli/src/main/java/org/rabix/cli/BackendCommandLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.rabix.bindings.Bindings;
import org.rabix.bindings.BindingsFactory;
import org.rabix.bindings.ProtocolType;
import org.rabix.bindings.helper.FileValueHelper;
import org.rabix.bindings.model.Application;
import org.rabix.bindings.model.ApplicationPort;
import org.rabix.bindings.model.DataType;
Expand Down Expand Up @@ -406,6 +407,20 @@ protected void configure() {
Object commonInputs = null;
try {
commonInputs = bindings.translateToCommon(inputs);
if (inputsFile != null) {
final Path finalInputs = inputsFile;
FileValueHelper.updateFileValues(commonInputs, (FileValue f) -> {
String path = f.getPath();
if (path != null && !Paths.get(path).isAbsolute()) {
f.setPath(finalInputs.resolveSibling(path).normalize().toString());
}
String location = f.getLocation();
if (location != null && URI.create(location).getScheme() == null) {
f.setLocation(finalInputs.resolveSibling(location).normalize().toString());
}
return f;
});
}
} catch (BindingException e1) {
VerboseLogger.log("Failed to translate inputs to the common Rabix format");
System.exit(10);
Expand Down

0 comments on commit bb84fb3

Please sign in to comment.