Skip to content

Commit

Permalink
improve robustness of version control
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Sep 25, 2024
1 parent e655b82 commit 10ede3f
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/main/java/io/bioimage/modelrunner/apposed/appose/Mamba.java
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,14 @@ public List<String> checkUninstalledDependenciesInEnv(String envName, List<Stri
public boolean checkDependencyInEnv(String envName, String dependency) throws MambaInstallException {
checkMambaInstalled();
if (!installed) throw new MambaInstallException("Micromamba is not installed");
if (dependency.contains("=<"))
throw new IllegalArgumentException("=< is not valid, use <=");
else if (dependency.contains("=>"))
throw new IllegalArgumentException("=> is not valid, use >=");
else if (dependency.contains(">") && dependency.contains("<") && !dependency.contains(","))
throw new IllegalArgumentException("Invalid dependency format. To specify both a minimum and maximum version, "
+ "separate the conditions with a comma. For example: 'torch>2.0.0, torch<2.5.0'.");

if (dependency.contains("==")) {
int ind = dependency.indexOf("==");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), dependency.substring(ind + 2).trim());
Expand All @@ -1375,6 +1383,9 @@ public boolean checkDependencyInEnv(String envName, String dependency) throws Ma
String packName = dependency.substring(0, minInd).trim();
String maxV = dependency.substring(lowInd + 2, lowInd < highInd ? commaInd : dependency.length());
String minV = dependency.substring(highInd + 2, lowInd < highInd ? dependency.length() : commaInd);
if (maxV.equals("") || minV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch>=2.0.0, torch<=2.5.0'.");
return checkDependencyInEnv(envName, packName, minV, maxV, false);
} else if (dependency.contains(">=") && dependency.contains("<") && dependency.contains(",")) {
int commaInd = dependency.indexOf(",");
Expand All @@ -1384,6 +1395,9 @@ public boolean checkDependencyInEnv(String envName, String dependency) throws Ma
String packName = dependency.substring(0, minInd).trim();
String maxV = dependency.substring(lowInd + 1, lowInd < highInd ? commaInd : dependency.length());
String minV = dependency.substring(highInd + 2, lowInd < highInd ? dependency.length() : commaInd);
if (maxV.equals("") || minV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch>=2.0.0, torch<2.5.0'.");
return checkDependencyInEnv(envName, packName, minV, null, false) && checkDependencyInEnv(envName, packName, null, maxV, true);
} else if (dependency.contains(">") && dependency.contains("<=") && dependency.contains(",")) {
int commaInd = dependency.indexOf(",");
Expand All @@ -1393,6 +1407,9 @@ public boolean checkDependencyInEnv(String envName, String dependency) throws Ma
String packName = dependency.substring(0, minInd).trim();
String maxV = dependency.substring(lowInd + 2, lowInd < highInd ? commaInd : dependency.length());
String minV = dependency.substring(highInd + 1, lowInd < highInd ? dependency.length() : commaInd);
if (maxV.equals("") || minV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch>2.0.0, torch<=2.5.0'.");
return checkDependencyInEnv(envName, packName, minV, null, true) && checkDependencyInEnv(envName, packName, null, maxV, false);
} else if (dependency.contains(">") && dependency.contains("<") && dependency.contains(",")) {
int commaInd = dependency.indexOf(",");
Expand All @@ -1402,19 +1419,38 @@ public boolean checkDependencyInEnv(String envName, String dependency) throws Ma
String packName = dependency.substring(0, minInd).trim();
String maxV = dependency.substring(lowInd + 1, lowInd < highInd ? commaInd : dependency.length());
String minV = dependency.substring(highInd + 1, lowInd < highInd ? dependency.length() : commaInd);
if (maxV.equals("") || minV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch>2.0.0, torch<2.5.0'.");
return checkDependencyInEnv(envName, packName, minV, maxV, true);
} else if (dependency.contains(">=")) {
int ind = dependency.indexOf(">=");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), dependency.substring(ind + 2).trim(), null, false);
String maxV = dependency.substring(ind + 2).trim();
if (maxV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch>=2.0.0'.");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), maxV, null, false);
} else if (dependency.contains(">")) {
int ind = dependency.indexOf(">");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), dependency.substring(ind + 1).trim(), null, true);
String maxV = dependency.substring(ind + 1).trim();
if (maxV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch>2.0.0'.");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), maxV, null, true);
} else if (dependency.contains("<=")) {
int ind = dependency.indexOf("<=");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), null, dependency.substring(ind + 2).trim(), false);
String maxV = dependency.substring(ind + 2).trim();
if (maxV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch<=2.0.0'.");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), null, maxV, false);
} else if (dependency.contains("<")) {
int ind = dependency.indexOf("<");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), null, dependency.substring(ind + 1).trim(), true);
String maxV = dependency.substring(ind + 1).trim();
if (maxV.equals(""))
throw new IllegalArgumentException("Conditions must always begin with either '<' or '>' signs and then "
+ "the version number. For example: 'torch<2.0.0'.");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), null, maxV, true);
} else if (dependency.contains("=")) {
int ind = dependency.indexOf("=");
return checkDependencyInEnv(envName, dependency.substring(0, ind).trim(), dependency.substring(ind + 1).trim());
Expand Down Expand Up @@ -1670,7 +1706,8 @@ private static String surroundWithQuotes(List<String> args) {
public static void main(String[] args) throws IOException, InterruptedException, MambaInstallException {

Mamba m = new Mamba("/home/carlos/git/SAMJ-IJ/appose_x86_64");
m.checkDependencyInEnv("sam2", "torch> 2.0.0, torch<2.5.0");
boolean aa = m.checkDependencyInEnv("sam2", "torch >=2.3.9, <=3.0.0");
System.out.println(aa);
}

/**
Expand Down

0 comments on commit 10ede3f

Please sign in to comment.