Skip to content

Commit

Permalink
Merge pull request #342 from cmu-phil/latest_version_checking
Browse files Browse the repository at this point in the history
Latest version checking
  • Loading branch information
kvb2univpitt authored Oct 28, 2016
2 parents 7ba47fb + ac5fabe commit 4262099
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 49 deletions.
2 changes: 1 addition & 1 deletion causal-cmd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>edu.cmu</groupId>
<artifactId>tetrad</artifactId>
<version>6.0-alpha-SNAPSHOT</version>
<version>6.0.0-SNAPSHOT</version>
</parent>
<artifactId>causal-cmd</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public void run() {

if (!skipLatest) {
LatestClient latestClient = LatestClient.getInstance();
latestClient.checkLatest("causal-cmd", AppTool.jarVersion());
String version = AppTool.jarVersion();
if (version == null) version = "DEVELOPMENT";
latestClient.checkLatest("causal-cmd", version);
System.out.println(latestClient.getLatestResult());
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>edu.cmu</groupId>
<artifactId>tetrad</artifactId>
<version>6.0-alpha-SNAPSHOT</version>
<version>6.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Tetrad Project</name>
Expand Down
19 changes: 18 additions & 1 deletion tetrad-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@
<parent>
<groupId>edu.cmu</groupId>
<artifactId>tetrad</artifactId>
<version>6.0-alpha-SNAPSHOT</version>
<version>6.0.0-SNAPSHOT</version>
</parent>

<artifactId>tetrad-gui</artifactId>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>resources/version</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>resources/version</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -37,6 +52,8 @@
<manifestEntries>
<Main-Class>edu.cmu.tetradapp.Tetrad</Main-Class>
<Permissions>all-permissions</Permissions>
<Implementation-Title>${project.name}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
</manifestEntries>
</transformer>
</transformers>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

package edu.cmu.tetradapp.app;

import edu.cmu.tetrad.latest.LatestClient;
import edu.cmu.tetrad.util.JOptionUtils;
import edu.cmu.tetrad.util.Version;
import edu.cmu.tetradapp.util.LicenseUtils;
Expand Down Expand Up @@ -56,6 +57,9 @@ public void actionPerformed(ActionEvent e) {
String copyright = LicenseUtils.copyright();
copyright = copyright.replaceAll("\n", "<br>");

String latestVersion = LatestClient.getInstance().getLatestResult(60);
latestVersion = latestVersion.replaceAll("\n","<br>");


JLabel label = new JLabel();
label.setText("<html>" + "<b>Tetrad " + currentVersion + "</b>" +
Expand All @@ -65,7 +69,10 @@ public void actionPerformed(ActionEvent e) {
"<br>Carnegie Mellon University" + "<br>" +
"<br>Project Direction: Clark Glymour, Richard Scheines, Peter Spirtes" +
"<br>Lead Developer: Joseph Ramsey" +
"<br>" + "<br>" + copyright + "</html>");
"<br>" + copyright +
"<br>" + latestVersion + "</html>"

);
label.setBackground(Color.LIGHT_GRAY);
label.setFont(new Font("Dialog", Font.PLAIN, 12));
label.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY),
Expand Down
21 changes: 18 additions & 3 deletions tetrad-gui/src/main/java/edu/cmu/tetradapp/util/SplashScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package edu.cmu.tetradapp.util;

import edu.cmu.tetrad.latest.LatestClient;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
Expand Down Expand Up @@ -54,8 +56,7 @@ public static void hide() {

try {
Thread.sleep(2000);
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
// Ignore.
}

Expand Down Expand Up @@ -104,7 +105,21 @@ private static class SplashWindow extends Window {
b1.add(Box.createHorizontalGlue());
b.add(b1);

JTextArea textArea = new JTextArea(LicenseUtils.copyright());
String text = LicenseUtils.copyright();


// check if we are running latest version
LatestClient latestClient = LatestClient.getInstance();
String version = this.getClass().getPackage().getImplementationVersion();

// if no version it means we are not running a jar so probably development
if (version == null) version = "DEVELOPMENT";
latestClient.checkLatest("tetrad", version);
StringBuilder latestResult = new StringBuilder(latestClient.getLatestResult(60));
text = text + "\n" + latestResult.toString();


JTextArea textArea = new JTextArea(text);
textArea.setBorder(new EmptyBorder(5, 5, 5, 5));
b.add(textArea);

Expand Down
8 changes: 8 additions & 0 deletions tetrad-gui/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
log4j.rootLogger=INFO,file

log4j.appender.file.File=causal-cmd.log
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n
7 changes: 3 additions & 4 deletions tetrad-gui/src/main/resources/resources/Readme
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ start Tetrad. The 'license' file contains the text of the GNU GPL license,
as posted at http://www.gnu.org/licenses/gpl.txt.

The 'version' file contains the version number of the most recently
published version of Tetrad. It is updated automatically each time the Ant
upload task is executed.
published version of Tetrad. It is updated automatically each time Maven package
goal is executed.

The 'version' file normally contains a string of this form as
its first line:
Expand All @@ -28,8 +28,7 @@ minor subversion. For example:
No other string types are permitted; anything else will cause
the lifecycle package goal to fail.

This currently needs to be coordinated manually with the POM file.
Might have to give this up.
This is coordinated automatically with the POM file.

The configuration file that Tetrad uses is configuration.xml. The file
configuration-post contains the version that's used for posting;
Expand Down
2 changes: 1 addition & 1 deletion tetrad-gui/src/main/resources/resources/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.3.0
${project.version}
2 changes: 1 addition & 1 deletion tetrad-lib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>edu.cmu</groupId>
<artifactId>tetrad</artifactId>
<version>6.0-alpha-SNAPSHOT</version>
<version>6.0.0-SNAPSHOT</version>
</parent>

<artifactId>tetrad-lib</artifactId>
Expand Down
73 changes: 51 additions & 22 deletions tetrad-lib/src/main/java/edu/cmu/tetrad/latest/LatestClient.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package edu.cmu.tetrad.latest;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
* Author : Jeremy Espino MD Created 9/20/16 11:06 AM
* Author : Jeremy Espino MD
* Created 9/20/16 11:06 AM
*/
public class LatestClient {

Expand All @@ -35,11 +37,26 @@ public String getLatestResult() {

}

public String getLatestResult(int lineWidth) {
StringBuilder truncatedLatestResult = new StringBuilder();

if (latestResult != null) {
truncatedLatestResult.append(latestResult);
int i = 0;
while ((i = truncatedLatestResult.indexOf(" ", i + lineWidth)) != -1) {
truncatedLatestResult.replace(i, i + 1, "\n");
}
}

return truncatedLatestResult.toString();
}

public boolean checkLatest(String softwareName, String version) {
LOGGER.debug("running version: " + version);

LOGGER.debug("running version: " + version);

final Properties applicationProperties = new Properties();
try (InputStream inputStream = this.getClass().getResourceAsStream("/tetrad-lib.properties")) {
try (InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("tetrad-lib.properties")) {
if (inputStream != null) {
applicationProperties.load(inputStream);
}
Expand All @@ -49,32 +66,44 @@ public boolean checkLatest(String softwareName, String version) {

String baseUrl = applicationProperties.getProperty("latest.version.url");
if (baseUrl == null) {
latestResult = String.format("Running version %s but unable to contact version server.", version);
latestResult = String.format("Running version %s but unable to contact version server. To disable checking use the skip-latest option.", version);
return false;
}

try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
HttpGet request = new HttpGet(baseUrl + softwareName);
HttpGet request = new HttpGet(baseUrl + "/latest/version/latest?softwareName=" + softwareName + "&softwareVersion=" + version);
request.addHeader("content-type", "application/json");
HttpResponse result = httpClient.execute(request);
String json = EntityUtils.toString(result.getEntity(), "UTF-8");
LOGGER.debug("response from latest version server: " + json);

com.google.gson.Gson gson = new com.google.gson.Gson();
SoftwareVersion softwareVersion = gson.fromJson(json, SoftwareVersion.class);

if (softwareVersion.getSoftwareVersion().equalsIgnoreCase(version)) {
latestResult = String.format("Running version %s which is the latest version.", version);
return true;
} else {
latestResult = String.format("Running version %s but the latest version is %s available.", version, softwareVersion.getSoftwareVersion());
return false;

if (result != null) {
String json = EntityUtils.toString(result.getEntity(), "UTF-8");
LOGGER.debug("response from latest version server: " + json);

if (json != null) {
com.google.gson.Gson gson = new com.google.gson.Gson();
SoftwareVersion softwareVersion = gson.fromJson(json, SoftwareVersion.class);

if (softwareVersion.getSoftwareVersion().equalsIgnoreCase(version)) {
latestResult = String.format("Running version %s which is the latest version. To disable checking use the skip-latest option.", version);
return true;
} else {
latestResult = String.format("Running version %s but the latest version is %s. To disable checking use the skip-latest option.", version, softwareVersion.getSoftwareVersion());
return false;
}
}
}

latestResult = String.format("Unable to communicate with version server. Running version %s. To disable checking use the skip-latest option.", version);
return false;


} catch (Exception ex) {

LOGGER.error("Could not contact server for latest version", ex);
latestResult = String.format("Running version %s but unable to contact version server.", version);
latestResult = String.format("Running version %s but unable to contact latest version server. To disable checking use the skip-latest option.", version);
return false;

}

}
}
}
41 changes: 29 additions & 12 deletions tetrad-lib/src/main/java/edu/cmu/tetrad/util/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public class Version implements TetradSerializable {
* increases without bound until the next major version, minor version, or
* minor subversion release, at which point is goes back to zero. If it is
* zero, release a.b.c.dx may be referred to as a.b.c.
* <p/>
* With maven snapshots the incremental release number refers to the snapshot build
* date. If this field is SNAPSHOT it is set to max int
*
* @serial Range greater than or equal to 0.
*/
Expand All @@ -90,24 +93,38 @@ public Version(String spec) {
Pattern pattern2 = Pattern.compile("(\\d*)\\.(\\d*)\\.(\\d*)");
Matcher matcher2 = pattern2.matcher(spec);

Pattern pattern3 = Pattern.compile("(\\d*)\\.(\\d*)\\.(\\d*)-(\\d*)");
Matcher matcher3 = pattern3.matcher(spec);

Pattern pattern4 = Pattern.compile("(\\d*)\\.(\\d*)\\.(\\d*)-SNAPSHOT");
Matcher matcher4 = pattern4.matcher(spec);

Pattern pattern5 = Pattern.compile("(\\d*)\\.(\\d*)\\.(\\d*)-(\\d*)\\.(\\d*)");
Matcher matcher5 = pattern5.matcher(spec);

if (matcher2.matches()) {
this.majorVersion = Integer.parseInt(matcher2.group(1));
this.minorVersion = Integer.parseInt(matcher2.group(2));
this.minorSubversion = Integer.parseInt(matcher2.group(3));
this.incrementalRelease = 0;
} else if (matcher3.matches()) {
this.majorVersion = Integer.parseInt(matcher3.group(1));
this.minorVersion = Integer.parseInt(matcher3.group(2));
this.minorSubversion = Integer.parseInt(matcher3.group(3));
this.incrementalRelease = Integer.parseInt(matcher3.group(4));
} else if (matcher4.matches()) {
this.majorVersion = Integer.parseInt(matcher4.group(1));
this.minorVersion = Integer.parseInt(matcher4.group(2));
this.minorSubversion = Integer.parseInt(matcher4.group(3));
this.incrementalRelease = 0;
} else if (matcher5.matches()) {
this.majorVersion = Integer.parseInt(matcher5.group(1));
this.minorVersion = Integer.parseInt(matcher5.group(2));
this.minorSubversion = Integer.parseInt(matcher5.group(3));
this.incrementalRelease = Integer.parseInt(matcher5.group(4));
} else {
Pattern pattern3 = Pattern.compile("(\\d*)\\.(\\d*)\\.(\\d*)-(\\d*)");
Matcher matcher3 = pattern3.matcher(spec);

if (matcher3.matches()) {
this.majorVersion = Integer.parseInt(matcher3.group(1));
this.minorVersion = Integer.parseInt(matcher3.group(2));
this.minorSubversion = Integer.parseInt(matcher3.group(3));
this.incrementalRelease = Integer.parseInt(matcher3.group(4));
} else {
throw new IllegalArgumentException("Version should be either of the " +
"form a.b.c (Maven) or a.b.c-d (old): " + spec);
}
throw new IllegalArgumentException("Version should be either of the " +
"form a.b.c or a.b.c-d or a.b.c-SNAPSHOT or a.b.c-d.e " + spec);
}
}

Expand Down
1 change: 1 addition & 0 deletions tetrad-lib/src/main/resources/tetrad-lib.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
latest.version.url=https://ccd3.vm.bridges.psc.edu
1 change: 0 additions & 1 deletion tetrad-lib/src/resources/tetrad-lib.properties

This file was deleted.

0 comments on commit 4262099

Please sign in to comment.