Search in sources :

Example 1 with FingerprintMap

use of hudson.model.FingerprintMap in project hudson-2.x by hudson.

the class Fingerprinter method record.

private void record(AbstractBuild<?, ?> build, BuildListener listener, Map<String, String> record, final String targets) throws IOException, InterruptedException {
    final class Record implements Serializable {

        final boolean produced;

        final String relativePath;

        final String fileName;

        final String md5sum;

        public Record(boolean produced, String relativePath, String fileName, String md5sum) {
            this.produced = produced;
            this.relativePath = relativePath;
            this.fileName = fileName;
            this.md5sum = md5sum;
        }

        Fingerprint addRecord(AbstractBuild build) throws IOException {
            FingerprintMap map = Hudson.getInstance().getFingerprintMap();
            return map.getOrCreate(produced ? build : null, fileName, md5sum);
        }

        private static final long serialVersionUID = 1L;
    }
    final long buildTimestamp = build.getTimeInMillis();
    FilePath ws = build.getWorkspace();
    if (ws == null) {
        listener.error(Messages.Fingerprinter_NoWorkspace());
        build.setResult(Result.FAILURE);
        return;
    }
    List<Record> records = ws.act(new FileCallable<List<Record>>() {

        public List<Record> invoke(File baseDir, VirtualChannel channel) throws IOException {
            List<Record> results = new ArrayList<Record>();
            FileSet src = Util.createFileSet(baseDir, targets);
            DirectoryScanner ds = src.getDirectoryScanner();
            for (String f : ds.getIncludedFiles()) {
                File file = new File(baseDir, f);
                // consider the file to be produced by this build only if the timestamp
                // is newer than when the build has started.
                // 2000ms is an error margin since since VFAT only retains timestamp at 2sec precision
                boolean produced = buildTimestamp <= file.lastModified() + 2000;
                try {
                    results.add(new Record(produced, f, file.getName(), new FilePath(file).digest()));
                } catch (IOException e) {
                    throw new IOException2(Messages.Fingerprinter_DigestFailed(file), e);
                } catch (InterruptedException e) {
                    throw new IOException2(Messages.Fingerprinter_Aborted(), e);
                }
            }
            return results;
        }
    });
    for (Record r : records) {
        Fingerprint fp = r.addRecord(build);
        if (fp == null) {
            listener.error(Messages.Fingerprinter_FailedFor(r.relativePath));
            continue;
        }
        fp.add(build);
        record.put(r.relativePath, fp.getHashString());
    }
}
Also used : FilePath(hudson.FilePath) Serializable(java.io.Serializable) Fingerprint(hudson.model.Fingerprint) AbstractBuild(hudson.model.AbstractBuild) FileSet(org.apache.tools.ant.types.FileSet) VirtualChannel(hudson.remoting.VirtualChannel) IOException(java.io.IOException) FingerprintMap(hudson.model.FingerprintMap) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) IOException2(hudson.util.IOException2)

Aggregations

FilePath (hudson.FilePath)1 AbstractBuild (hudson.model.AbstractBuild)1 Fingerprint (hudson.model.Fingerprint)1 FingerprintMap (hudson.model.FingerprintMap)1 VirtualChannel (hudson.remoting.VirtualChannel)1 IOException2 (hudson.util.IOException2)1 File (java.io.File)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)1 FileSet (org.apache.tools.ant.types.FileSet)1