Search in sources :

Example 76 with FileSet

use of org.apache.tools.ant.types.FileSet in project hudson-2.x by hudson.

the class FilePath method glob.

/**
 * Runs Ant glob expansion.
 *
 * @return
 *      A set of relative file names from the base directory.
 */
private static String[] glob(File dir, String includes) throws IOException {
    if (isAbsolute(includes))
        throw new IOException("Expecting Ant GLOB pattern, but saw '" + includes + "'. See http://ant.apache.org/manual/Types/fileset.html for syntax");
    FileSet fs = Util.createFileSet(dir, includes);
    DirectoryScanner ds = fs.getDirectoryScanner(new Project());
    String[] files = ds.getIncludedFiles();
    return files;
}
Also used : Project(org.apache.tools.ant.Project) AbstractProject(hudson.model.AbstractProject) FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) IOException(java.io.IOException)

Example 77 with FileSet

use of org.apache.tools.ant.types.FileSet 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)

Example 78 with FileSet

use of org.apache.tools.ant.types.FileSet in project hudson-2.x by hudson.

the class AbstractItem method renameTo.

/**
 * Renames this item.
 * Not all the Items need to support this operation, but if you decide to do so,
 * you can use this method.
 */
protected void renameTo(String newName) throws IOException {
    // always synchronize from bigger objects first
    final ItemGroup parent = getParent();
    synchronized (parent) {
        synchronized (this) {
            // sanity check
            if (newName == null)
                throw new IllegalArgumentException("New name is not given");
            // noop?
            if (this.name.equals(newName))
                return;
            Item existing = parent.getItem(newName);
            if (existing != null && existing != this)
                // see http://www.nabble.com/error-on-renaming-project-tt18061629.html
                throw new IllegalArgumentException("Job " + newName + " already exists");
            String oldName = this.name;
            performBeforeItemRenaming(oldName, newName);
            File oldRoot = this.getRootDir();
            doSetName(newName);
            File newRoot = this.getRootDir();
            boolean success = false;
            try {
                // rename data files
                boolean interrupted = false;
                boolean renamed = false;
                // so retry few times before we fall back to copy.
                for (int retry = 0; retry < 5; retry++) {
                    if (oldRoot.renameTo(newRoot)) {
                        renamed = true;
                        // succeeded
                        break;
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        // process the interruption later
                        interrupted = true;
                    }
                }
                if (interrupted)
                    Thread.currentThread().interrupt();
                if (!renamed) {
                    // failed to rename. it must be that some lengthy
                    // process is going on
                    // to prevent a rename operation. So do a copy. Ideally
                    // we'd like to
                    // later delete the old copy, but we can't reliably do
                    // so, as before the VM
                    // shuts down there might be a new job created under the
                    // old name.
                    Copy cp = new Copy();
                    cp.setProject(new org.apache.tools.ant.Project());
                    cp.setTodir(newRoot);
                    FileSet src = new FileSet();
                    src.setDir(getRootDir());
                    cp.addFileset(src);
                    cp.setOverwrite(true);
                    cp.setPreserveLastModified(true);
                    // keep going even if
                    cp.setFailOnError(false);
                    // there's an error
                    cp.execute();
                    // try to delete as much as possible
                    try {
                        Util.deleteRecursive(oldRoot);
                    } catch (IOException e) {
                        // but ignore the error, since we expect that
                        e.printStackTrace();
                    }
                }
                success = true;
            } finally {
                // if failed, back out the rename.
                if (!success)
                    doSetName(oldName);
            }
            callOnRenamed(newName, parent, oldName);
            for (ItemListener l : ItemListener.all()) l.onRenamed(this, oldName, newName);
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) IOException(java.io.IOException) Copy(org.apache.tools.ant.taskdefs.Copy) ItemListener(hudson.model.listeners.ItemListener) XmlFile(hudson.XmlFile) File(java.io.File)

Example 79 with FileSet

use of org.apache.tools.ant.types.FileSet in project quasar by puniverse.

the class SuspendablesScanner method visitAntProject.

private void visitAntProject(Function<InputStream, Void> classFileVisitor) throws IOException {
    for (FileSet fs : filesets) {
        try {
            final DirectoryScanner ds = fs.getDirectoryScanner(getProject());
            final String[] includedFiles = ds.getIncludedFiles();
            for (String filename : includedFiles) {
                if (isClassFile(filename)) {
                    try {
                        File file = new File(fs.getDir(), filename);
                        if (file.isFile())
                            classFileVisitor.apply(new FileInputStream(file));
                        else
                            log("File not found: " + filename);
                    } catch (Exception e) {
                        throw new RuntimeException("Exception while processing " + filename, e);
                    }
                }
            }
        } catch (BuildException ex) {
            log(ex.getMessage(), ex, Project.MSG_WARN);
        }
    }
}
Also used : FileSet(org.apache.tools.ant.types.FileSet) DirectoryScanner(org.apache.tools.ant.DirectoryScanner) BuildException(org.apache.tools.ant.BuildException) ClassLoaderUtil.isClassFile(co.paralleluniverse.common.reflection.ClassLoaderUtil.isClassFile) File(java.io.File) FileInputStream(java.io.FileInputStream) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException)

Example 80 with FileSet

use of org.apache.tools.ant.types.FileSet in project quasar by puniverse.

the class SuspendablesScanner method run.

public void run() {
    try {
        // System.err.println("QQQQQQQQ: " + new SimpleSuspendableClassifier(supersFile).getSuspendableClasses());
        final List<URL> us = new ArrayList<>();
        if (ant) {
            final AntClassLoader acl = (AntClassLoader) getClass().getClassLoader();
            classpathToUrls(acl.getClasspath().split(System.getProperty("path.separator")), us);
            for (FileSet fs : filesets) us.add(fs.getDir().toURI().toURL());
        } else {
        // final URLClassLoader ucl = (URLClassLoader) getClass().getClassLoader();
        // us.addAll(Arrays.asList(ucl.getURLs()));
        }
        if (// only in tests
        this.urls != null)
            us.addAll(Arrays.asList(this.urls));
        setURLs(us);
        log("Classpath URLs: " + Arrays.toString(this.urls), Project.MSG_INFO);
        List<URL> pus = new ArrayList<>();
        if (ant) {
            for (FileSet fs : filesets) pus.add(fs.getDir().toURI().toURL());
        } else
            pus.add(projectDir.toUri().toURL());
        log("Project URLs: " + pus, Project.MSG_INFO);
        final long tStart = System.nanoTime();
        scanExternalSuspendables();
        final long tScanExternal = System.nanoTime();
        if (auto)
            log("Scanned external suspendables in " + (tScanExternal - tStart) / 1000000 + " ms", Project.MSG_INFO);
        // scan classes in filesets
        Function<InputStream, Void> fileVisitor = new Function<InputStream, Void>() {

            @Override
            public Void apply(InputStream is1) {
                try (InputStream is = is1) {
                    createGraph(is);
                    return null;
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        };
        if (ant)
            visitAntProject(fileVisitor);
        else
            visitProjectDir(fileVisitor);
        scanSuspendablesFile(fileVisitor);
        final long tBuildGraph = System.nanoTime();
        log("Built method graph in " + (tBuildGraph - tScanExternal) / 1000000 + " ms", Project.MSG_INFO);
        walkGraph();
        final long tWalkGraph = System.nanoTime();
        log("Walked method graph in " + (tWalkGraph - tBuildGraph) / 1000000 + " ms", Project.MSG_INFO);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : Function(com.google.common.base.Function) FileSet(org.apache.tools.ant.types.FileSet) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) AntClassLoader(org.apache.tools.ant.AntClassLoader) IOException(java.io.IOException) URL(java.net.URL)

Aggregations

FileSet (org.apache.tools.ant.types.FileSet)165 File (java.io.File)124 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)83 BuildException (org.apache.tools.ant.BuildException)49 Test (org.junit.Test)41 IOException (java.io.IOException)36 ArrayList (java.util.ArrayList)29 Project (org.apache.tools.ant.Project)22 Resource (org.apache.tools.ant.types.Resource)12 FileResource (org.apache.tools.ant.types.resources.FileResource)10 URL (java.net.URL)6 Hashtable (java.util.Hashtable)6 ArchiveFileSet (org.apache.tools.ant.types.ArchiveFileSet)6 Path (org.apache.tools.ant.types.Path)6 ResourceCollection (org.apache.tools.ant.types.ResourceCollection)6 PrintStream (java.io.PrintStream)5 HashMap (java.util.HashMap)5 Iterator (java.util.Iterator)5 List (java.util.List)5 ZipFileSet (org.apache.tools.ant.types.ZipFileSet)5