You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by ke...@apache.org on 2007/06/03 07:35:34 UTC
svn commit: r543843 - in /ant/sandbox/antlibs/debian/trunk/src:
main/org/apache/ant/debian/ tests/antunit/
Author: kevj
Date: Sat Jun 2 22:35:33 2007
New Revision: 543843
URL: http://svn.apache.org/viewvc?view=rev&rev=543843
Log:
-add checksum & remove tasks
Added:
ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/ChecksumTask.java
ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/RemoveDpkg.java
ant/sandbox/antlibs/debian/trunk/src/tests/antunit/checksum-test.xml
Modified:
ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/BuildDpkg.java
ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/antlib.xml
Modified: ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/BuildDpkg.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/BuildDpkg.java?view=diff&rev=543843&r1=543842&r2=543843
==============================================================================
--- ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/BuildDpkg.java (original)
+++ ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/BuildDpkg.java Sat Jun 2 22:35:33 2007
@@ -54,7 +54,11 @@
if (null == packageName || packageName.length() == 0) {
log("You haven't specified a package name, the file will be created under ["+dir+"].");
}
- //TODO add checks for missing control file, and package name checks
+ File controlFile = new File(dir.getAbsolutePath()+File.separator+"DEBIAN"+File.separator+"control");
+ if (!controlFile.canRead()) {
+ throw new BuildException("Cannot read control file ["+controlFile+"].");
+ }
+ //TODO add checks for missing control file, and package name checks, and MD5 checks
}
public File getDir() {
Added: ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/ChecksumTask.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/ChecksumTask.java?view=auto&rev=543843
==============================================================================
--- ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/ChecksumTask.java (added)
+++ ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/ChecksumTask.java Sat Jun 2 22:35:33 2007
@@ -0,0 +1,175 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ant.debian;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileInputStream;
+import java.util.Iterator;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+import org.apache.tools.ant.Task;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.util.StringUtils;
+import org.apache.tools.ant.util.FileUtils;
+import org.apache.tools.ant.types.FileSet;
+import org.apache.tools.ant.types.ResourceCollection;
+import org.apache.tools.ant.types.resources.Union;
+import org.apache.tools.ant.types.resources.Restrict;
+import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.types.resources.selectors.Type;
+
+/**
+ * Creates a single file with the checksums (MD5 by default)
+ * of the files specified. Note different from the core
+ * <checksum> task.
+ */
+public class ChecksumTask extends Task {
+
+ private String algorithm = "MD5";
+
+ private File output;
+
+ /**
+ * Resource Collection.
+ */
+ private FileUnion resources = null;
+
+ private MessageDigest messageDigest;
+
+ public void execute() throws BuildException {
+ validate();
+ try {
+ messageDigest = MessageDigest.getInstance(algorithm);
+ } catch(NoSuchAlgorithmException e) {
+ throw new BuildException(e);
+ }
+ if (messageDigest == null) {
+ throw new BuildException("Unable to create Message Digest", getLocation());
+ }
+ createChecksums();
+ }
+
+ private void validate() throws BuildException {
+ if(resources == null || resources.size() == 0) {
+ throw new BuildException("You must specify some resources to create the checksums for.");
+ }
+ if(!resources.isFilesystemOnly()) {
+ throw new BuildException("Cannot create a checksum for non file-based resources.");
+ }
+
+ if (output == null) {
+ throw new BuildException("You must specify an output file.");
+ }
+ //TODO add more checks
+ }
+
+ /**
+ * Creates the checksums and writes them to the output file
+ * This is an adaptation & simplyfied version of the standard
+ * Checksum task in Ant.
+ * @throws BuildException
+ */
+ private void createChecksums() throws BuildException {
+ FileOutputStream os = null;
+ FileInputStream is = null;
+ DigestInputStream dis = null;
+ byte[] buffer = new byte[8 * 1024];
+ try {
+ os = new FileOutputStream(output);
+ for(Iterator i = resources.iterator(); i.hasNext();) {
+ messageDigest.reset();
+ File src = ((FileResource)i.next()).getFile();
+ is = new FileInputStream(src);
+ dis = new DigestInputStream(is, messageDigest);
+ while (dis.read(buffer, 0, 8 * 1024) != -1) { /* do nothing */}
+ dis.close();
+ is.close();
+ is = null;
+ byte[] fileDigest = messageDigest.digest ();
+ String checksum = createDigestString(fileDigest);
+ os.write((checksum + " " + src.getName()).getBytes());
+ os.write(StringUtils.LINE_SEP.getBytes());
+ }
+ } catch (Exception e) {
+ throw new BuildException("Problem occured creating checksums",e);
+ } finally {
+ FileUtils.close(dis);
+ FileUtils.close(is);
+ FileUtils.close(os);
+ }
+ }
+
+ private String createDigestString(byte[] fileDigest) {
+ StringBuffer checksumSb = new StringBuffer();
+ for (int i = 0; i < fileDigest.length; i++) {
+ String hexStr = Integer.toHexString(0x00ff & fileDigest[i]);
+ if (hexStr.length() < 2) {
+ checksumSb.append("0");
+ }
+ checksumSb.append(hexStr);
+ }
+ return checksumSb.toString();
+ }
+
+ /**
+ * Sets the digest algorithm, by default it's MD5
+ * @param a algorithm to set
+ */
+ public void setAlgorithm(String a) {
+ algorithm = a;
+ }
+
+ public void setOutput(File o) {
+ output = o;
+ }
+
+ /**
+ * Files to generate checksums for.
+ * @param set a fileset of files to generate checksums for.
+ */
+ public void addFileset(FileSet set) {
+ add(set);
+ }
+
+ /**
+ * Add a resource collection.
+ * @param rc the ResourceCollection to add.
+ */
+ public void add(ResourceCollection rc) {
+ if (rc == null) {
+ return;
+ }
+ resources = (resources == null) ? new FileUnion() : resources;
+ resources.add(rc);
+ }
+
+ private static class FileUnion extends Restrict {
+ private Union u;
+ FileUnion() {
+ u = new Union();
+ super.add(u);
+ super.add(Type.FILE);
+ }
+ public void add(ResourceCollection rc) {
+ u.add(rc);
+ }
+ }
+}
\ No newline at end of file
Added: ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/RemoveDpkg.java
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/RemoveDpkg.java?view=auto&rev=543843
==============================================================================
--- ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/RemoveDpkg.java (added)
+++ ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/RemoveDpkg.java Sat Jun 2 22:35:33 2007
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+package org.apache.ant.debian;
+
+import org.apache.tools.ant.types.Commandline;
+import org.apache.tools.ant.BuildException;
+
+/**
+ * Remove a debian package
+ */
+public class RemoveDpkg extends AbstractDpkgTask {
+
+ /** the name of the .deb package to create */
+ private String packageName;
+
+ /** are we going to remove config files too ? */
+ private boolean purge = false;
+
+ public void execute() throws BuildException {
+ validate();
+ Commandline c = new Commandline();
+ c.setExecutable("dpkg");
+ String[] args;
+ if(purge) {
+ args = new String[] { "--purge", packageName };
+ } else {
+ args = new String[] { "--remove", packageName };
+ }
+ c.addArguments(args);
+ super.addConfiguredCommandline(c);
+ super.execute();
+ }
+
+ private void validate() throws BuildException {
+ if((packageName == null || packageName.length() == 0)) {
+ throw new BuildException("You must specify a packagename.");
+ }
+ }
+
+ /* setters */
+ public void setPackageName(final String p) {
+ packageName = p;
+ }
+
+ public void setPurge(final boolean p) {
+ purge = p;
+ }
+}
\ No newline at end of file
Modified: ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/antlib.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/antlib.xml?view=diff&rev=543843&r1=543842&r2=543843
==============================================================================
--- ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/antlib.xml (original)
+++ ant/sandbox/antlibs/debian/trunk/src/main/org/apache/ant/debian/antlib.xml Sat Jun 2 22:35:33 2007
@@ -28,4 +28,12 @@
name="install"
classname="org.apache.ant.debian.InstallDpkg"
/>
+ <taskdef
+ name="remove"
+ classname="org.apache.ant.debian.RemoveDpkg"
+ />
+ <taskdef
+ name="checksum"
+ classname="org.apache.ant.debian.ChecksumTask"
+ />
</antlib>
Added: ant/sandbox/antlibs/debian/trunk/src/tests/antunit/checksum-test.xml
URL: http://svn.apache.org/viewvc/ant/sandbox/antlibs/debian/trunk/src/tests/antunit/checksum-test.xml?view=auto&rev=543843
==============================================================================
--- ant/sandbox/antlibs/debian/trunk/src/tests/antunit/checksum-test.xml (added)
+++ ant/sandbox/antlibs/debian/trunk/src/tests/antunit/checksum-test.xml Sat Jun 2 22:35:33 2007
@@ -0,0 +1,28 @@
+<?xml version="1.0"?>
+<project default="all" basedir="../../../.."
+ xmlns:au="antlib:org.apache.ant.antunit"
+ xmlns:deb="antlib:org.apache.ant.debian">
+
+ <property name="output" value="${basedir}/ant-core-trunk/dist/md5sums"/>
+
+ <target name="tearDown">
+ <delete file="${output}"/>
+ </target>
+
+ <target name="test-checksum">
+ <au:assertFileDoesntExist file="${output}"/>
+ <deb:checksum output="${output}">
+ <fileset dir="${basedir}/ant-core-trunk/dist">
+ <include name="bin/*.*"/>
+ </fileset>
+ </deb:checksum>
+ <au:assertFileExists file="${output}"/>
+ </target>
+
+ <target name="all">
+ <au:antunit>
+ <fileset file="${ant.file}"/>
+ <au:plainlistener/>
+ </au:antunit>
+ </target>
+</project>
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org