diff options
author | Michael Bien <[email protected]> | 2009-11-04 16:46:08 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2009-11-04 16:46:08 +0100 |
commit | 0eebae54517e816aaeb36495dfdbdc1b6d03bfba (patch) | |
tree | 65004c89b1d713f277de1acd15301563f089fbc5 /etc/src/com/mbien | |
parent | b2dfcb34a4cdc9c45d5ce20a2b1559b4bf3ebb8c (diff) |
added automatic OpenCL header downloader to build script.
Diffstat (limited to 'etc/src/com/mbien')
-rw-r--r-- | etc/src/com/mbien/ant/HeaderFileDownloader.java | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/etc/src/com/mbien/ant/HeaderFileDownloader.java b/etc/src/com/mbien/ant/HeaderFileDownloader.java new file mode 100644 index 00000000..5177b905 --- /dev/null +++ b/etc/src/com/mbien/ant/HeaderFileDownloader.java @@ -0,0 +1,100 @@ +package com.mbien.ant; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.URL; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.Task; + +/** + * Keeps downloaded headers up2date. + * @author Michael Bien + */ +public class HeaderFileDownloader extends Task { + + private String filePath; + private String urlStr; + + /*example: $Revision: 9283 $ on $Date: 2009-10-14 10:18:57 -0700 (Wed, 14 Oct 2009) $ */ + private final Pattern revisionPattern = Pattern.compile("\\$Revision:\\s(\\d+)"); + + @Override + public void execute() throws BuildException { + + if(filePath == null) + throw new IllegalArgumentException("file must be set"); + if(urlStr == null) + throw new IllegalArgumentException("update url must be set"); + + try { + URL url = new URL(urlStr); + int remoteRevision = readRevision(url.openStream()); + int localRevision = readRevision(new FileInputStream(new File(filePath))); + + if(remoteRevision != localRevision) { + + System.out.println("updating header: "+filePath); + System.out.println("from revision "+localRevision +" to revision "+remoteRevision); + + BufferedInputStream in = new BufferedInputStream(url.openStream()); + BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(filePath)); + + int val; + while((val=in.read()) != -1) { + out.write(val); + } + in.close(); + + out.flush(); + out.close(); + }else{ + System.out.println("header "+filePath+" is up to date"); + } + + } catch (IOException ex) { + throw new BuildException(ex); + }finally{ + } + + } + + private int readRevision(InputStream is) throws IOException { + + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + + String line = null; + try { + while ((line = reader.readLine()) != null) { + Matcher matcher = revisionPattern.matcher(line); + if(matcher.find()) { + System.out.println(line); + return Integer.parseInt(matcher.group(1)); + } + } + } finally { + reader.close(); + } + + return 0; + } + + + public void setURL(String url) { + this.urlStr = url; + } + + public void setHeader(String filePath) { + this.filePath = filePath; + } + + +} |