01: package de.masters_of_disaster.ant.tasks.calculatesize;
02:
03: import java.io.File;
04: import java.util.Enumeration;
05: import java.util.Vector;
06: import org.apache.tools.ant.BuildException;
07: import org.apache.tools.ant.taskdefs.MatchingTask;
08: import org.apache.tools.ant.types.FileSet;
09:
10: /**
11: * Calculates the "Installed-Size" of a deb package for the "control"-file.
12: *
13: * @ant.task category="packaging"
14: */
15: public class CalculateSize extends MatchingTask {
16: String realSizeProperty = null;
17: String diskSizeProperty = null;
18: Vector fileSets = new Vector();
19: File baseDir;
20:
21: /**
22: * Add a new fileset
23: *
24: * @return the fileset to be used as the nested element.
25: */
26: public FileSet createFileSet() {
27: FileSet fileSet = new FileSet();
28: fileSets.addElement(fileSet);
29: return fileSet;
30: }
31:
32: /**
33: * This is the base directory to look in for things to include.
34: *
35: * @param baseDir the base directory.
36: */
37: public void setBaseDir(File baseDir) {
38: this .baseDir = baseDir;
39: fileset.setDir(baseDir);
40: }
41:
42: /**
43: * This is the property to set to the real size.
44: *
45: * @param realSizeProperty The property to set to the real size
46: */
47: public void setRealSizeProperty(String realSizeProperty) {
48: this .realSizeProperty = realSizeProperty;
49: }
50:
51: /**
52: * This is the property to set to the disk size.
53: *
54: * @param diskSizeProperty The property to set to the disk size
55: */
56: public void setDiskSizeProperty(String diskSizeProperty) {
57: this .diskSizeProperty = diskSizeProperty;
58: }
59:
60: /**
61: * do the business
62: *
63: * @throws BuildException on error
64: */
65: public void execute() throws BuildException {
66: if ((null == realSizeProperty) && (null == diskSizeProperty)) {
67: throw new BuildException(
68: "realSizeProperty or diskSizeProperty must be set for <CalculateSize>");
69: }
70:
71: if (null != baseDir) {
72: // add the main fileset to the list of filesets to process.
73: fileSets.addElement(fileset);
74: }
75:
76: long realSize = 0;
77: long diskSize = 0;
78: for (Enumeration e = fileSets.elements(); e.hasMoreElements();) {
79: FileSet fileSet = (FileSet) e.nextElement();
80: String[] files = fileSet.getDirectoryScanner(getProject())
81: .getIncludedFiles();
82: File fileSetDir = fileSet.getDir(getProject());
83: for (int i = 0, c = files.length; i < c; i++) {
84: long fileLength = new File(fileSetDir, files[i])
85: .length();
86: realSize += fileLength / 1024;
87: diskSize += (fileLength / 4096 + 1) * 4;
88: }
89: }
90: if (null != realSizeProperty) {
91: getProject().setNewProperty(realSizeProperty,
92: Long.toString(realSize));
93: }
94: if (null != diskSizeProperty) {
95: getProject().setNewProperty(diskSizeProperty,
96: Long.toString(diskSize));
97: }
98: }
99: }
|