Subversion Repositories DevTools

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1615 mhunt 1
package com.erggroup.mass.ant.taskdefs;
2
 
3
import com.erggroup.mass.ant.Utils;
4
import java.io.*;
5
import net.sourceforge.clearcase.api.IClearcase;
6
import net.sourceforge.clearcase.simple.ClearcaseCLI;
7
import org.apache.tools.ant.BuildException;
8
import org.apache.tools.ant.Project;
9
import org.apache.tools.ant.Task;
10
 
11
 
12
/**
13
 *
14
 */
15
public class CCLabel extends Task
16
{
17
	public static final String COMPUTERNAME = "${env.COMPUTERNAME}";
18
	public static final String USERNAME     = "${env.USERNAME}";
19
 
20
	/**
21
	 * The name of the package being labelled
22
	 */
23
	private String packageName;
24
 
25
	/**
26
	 * Indicates if a view should be created based on the created label
27
	 */
28
	private String mkview;
29
 
30
	/**
31
	 * Indicates if a view should be created based on the created label
32
	 */
33
	private String mapview;
34
 
35
	/**
36
	 * Provides a variable with which to populate the created view path
37
	 */
38
	private String lbl;
39
 
40
	/**
41
	 * Find checkouts (optional) - looks for checkouts and aborts if any are
42
	 * found.
43
	 */
44
	private String findCheckouts;
45
 
46
	/**
47
	 * The fully qualified path of the view as created (if mkview is true)
48
	 */
49
	private String viewpath;
50
 
51
	/**
52
	 * View config spec base filename (optional, if mkview is true)
53
	 */
54
	private String viewCS;
55
 
56
	/**
57
	 * The name of the view as created (if mkview is true)
58
	 */
59
	private String viewnameProperty;
60
 
61
	/**
62
	 * The version to apply a label against.
63
	 */
64
	private String version;
65
 
66
	/**
67
	 */
68
	public void execute()
69
	{
70
		if ( packageName == null )
71
		{
72
			throw new BuildException("Attribute 'packageName' is mandatory.");
73
		}
74
 
75
		if ( version == null )
76
		{
77
			throw new BuildException("Attribute 'version' is mandatory.");
78
		}
79
 
80
		String labelname = packageName + "_" + project.replaceProperties(version);
81
		String projectPath = getPath( project.getBaseDir().getAbsolutePath() );
82
 
83
		IClearcase clearcase = new ClearcaseCLI();
84
 
85
		// Before proceeding, look for checkouts and abort if any are found
86
		if ( findCheckouts != null && (findCheckouts.equalsIgnoreCase("true") || findCheckouts.equalsIgnoreCase("yes")) )
87
		{
88
			project.log("Searching for checkouts in directory \'" + project.getBaseDir().getAbsolutePath() + "\'.",Project.MSG_INFO);
89
			IClearcase.Status lscheckout = clearcase.cleartool( "lscheckout -short -me -recurse -cview " + projectPath);
90
			if ( !lscheckout.status )
91
			{
92
				project.log(lscheckout.message,Project.MSG_ERR);
93
				throw new BuildException("Could not find checkouts in directory \'" + project.getBaseDir().getAbsolutePath() + "\'.");
94
			}
95
 
96
			if ( lscheckout.message != null && lscheckout.message.length() > 0 )
97
			{
98
				throw new BuildException("Build aborted.\nYou have the following files checked out:\n" + lscheckout.message );
99
			}
100
		}
101
 
102
		// Do the labelling here.
103
		Utils.setWorkingDirectory(clearcase,project);
104
		project.log("Creating label \'" + labelname + "\'.",Project.MSG_INFO);
105
		IClearcase.Status makelabel = clearcase.cleartool( "mklbtype -global -nc " + labelname );
106
		if ( ! makelabel.status )
107
		{
108
			project.log(makelabel.message,Project.MSG_ERR);
109
			throw new BuildException("Could not create label \'" + labelname + "\'.\nMake sure that the label does not already exist.");
110
		}
111
 
112
		// Label the release directory and all files within...
113
		project.log("Labelling project, please wait (this could take some time for large projects)...",Project.MSG_INFO);
114
		IClearcase.Status label = clearcase.cleartool( "mklabel -replace -recurse -follow " + labelname + " " + projectPath );
115
		if ( ! label.status )
116
		{
117
			project.log(label.message,Project.MSG_ERR);
118
			throw new BuildException("Could not label '" + projectPath + "' with label \'" + labelname + "\'.");
119
		}
120
 
121
		File parentDirectory = project.getBaseDir().getParentFile();
122
		while ( parentDirectory != null && parentDirectory.getParentFile() != null )
123
		{
124
			String directoryPath = getPath( parentDirectory.getAbsolutePath() );
125
			label = clearcase.cleartool( "mklabel -replace  -follow " + labelname + " " + directoryPath );
126
			if ( ! label.status )
127
			{
128
				project.log(label.message,Project.MSG_ERR);
129
				throw new BuildException("Could not label '" + directoryPath + "' with label \'" + labelname + "\'.");
130
			}
131
			parentDirectory = parentDirectory.getParentFile();
132
		}
133
 
134
		if ( lbl != null )
135
		{
136
			project.setNewProperty( lbl, labelname );
137
		}
138
		project.log("Successfully created label '" + labelname + "'.",Project.MSG_INFO);
139
 
140
		// Make a view (optional)
141
		if ( mkview != null && (mkview.equalsIgnoreCase("true") || mkview.equalsIgnoreCase("yes")) )
142
		{
143
			String viewhost = project.replaceProperties(COMPUTERNAME);
144
			String username = project.replaceProperties(USERNAME);
145
			if ( viewhost != null && username != null && !viewhost.equals(COMPUTERNAME) && !username.equals(USERNAME) )
146
			{
147
				String viewname = username + "_jats_build_" + labelname;
148
				project.log("Creating view '" + viewname + "'.",Project.MSG_INFO);
149
				try
150
				{
151
					IClearcase.Status mkview = clearcase.cleartool( "mkview -tag " + viewname + " \\\\" + viewhost + "\\Clearcase\\" + viewname );
152
					if ( ! mkview.status )
153
					{
154
						project.log(mkview.message,Project.MSG_ERR);
155
						throw new BuildException("Could not create view '" + viewname + "'.");
156
					}
157
					String viewspecFilename = "\\\\" + viewhost + "\\Clearcase\\" + viewname + "\\config_spec";
158
					File viewspec = new File(viewspecFilename);
159
					viewspec.delete();
160
					viewspec.createNewFile();
161
					FileOutputStream fileOut = new FileOutputStream(viewspec);
162
					PrintWriter viewspecOut = new PrintWriter(fileOut,true);
163
					if (viewCS != null)
164
					{
165
						try
166
						{
167
							File viewCSFile = new File(viewCS);
168
							if (viewCSFile.exists())
169
							{
170
								FileReader reader = new FileReader(viewCSFile);
171
								BufferedReader bufferedReader = new BufferedReader(reader);
172
								String line = bufferedReader.readLine();
173
								while ( line != null )
174
								{
175
									viewspecOut.println(line);
176
									line = bufferedReader.readLine();
177
								}
178
								bufferedReader.close();
179
								reader.close();
180
							}
181
						}
182
						catch ( Exception x )
183
						{
184
							project.log("Could not read config spec '" + viewCS + "'.",Project.MSG_ERR);
185
							throw new BuildException("Could not create view '" + viewname + "' - could not read config spec.");
186
						}
187
					}
188
 
189
					viewspecOut.println("element * " + labelname + " -nocheckout");
190
					viewspecOut.close();
191
					fileOut.close();
192
					mkview = clearcase.cleartool( "setcs -tag " + viewname + " -current" );
193
					if ( ! mkview.status )
194
					{
195
						project.log(mkview.message,Project.MSG_ERR);
196
						throw new BuildException("Could not create view '" + viewname + "' - could not set config spec.");
197
					}
198
 
199
					if ( viewnameProperty != null )
200
					{
201
						project.setNewProperty( viewnameProperty, viewname );
202
					}
203
 
204
					if ( viewpath != null )
205
					{
206
						String viewPathW32 = project.getBaseDir().getAbsolutePath();
207
						int cpos = viewPathW32.indexOf(':');
208
						viewPathW32 = "O:/" + viewname + viewPathW32.substring(cpos+1);
209
 
210
						try
211
						{
212
							if ( mapview != null )
213
							{
214
								project.log("Mapping view to network drive '" + mapview + "'.",Project.MSG_INFO);
215
								Process p = Runtime.getRuntime().exec("subst "+ mapview + " O:/" + viewname );
216
								int rv = p.waitFor();
217
								if ( rv == 0 )
218
								{
219
									viewPathW32 = project.getBaseDir().getAbsolutePath();
220
									viewPathW32 = mapview + "/" + viewPathW32.substring(cpos+1);
221
								}
222
							}
223
						}
224
						catch ( InterruptedException x )
225
						{
226
							project.log(x.getMessage(),Project.MSG_WARN);
227
							viewPathW32 = "O:/" + viewname + viewPathW32.substring(cpos+1);
228
						}
229
						catch ( IOException x )
230
						{
231
							project.log(x.getMessage(),Project.MSG_WARN);
232
							viewPathW32 = "O:/" + viewname + viewPathW32.substring(cpos+1);
233
						}
234
 
235
						File viewDirectory = new File(viewPathW32);
236
						project.setNewProperty( viewpath, viewDirectory.getAbsolutePath() );
237
					}
238
					project.log("Successfully created view '" + viewname + "'.",Project.MSG_INFO);
239
				}
240
				catch (Exception x)
241
				{
242
					project.log(x.getMessage(),Project.MSG_ERR);
243
					throw new BuildException("Could not create view.");
244
				}
245
			}
246
		}
247
	}
248
 
249
	public void setPackage(String value)
250
	{
251
		this.packageName = value;
252
	}
253
 
254
	public void setMkview(String value)
255
	{
256
		this.mkview = value;
257
	}
258
 
259
	public void setMapview(String value)
260
	{
261
		this.mapview = value;
262
	}
263
 
264
 
265
	public void setLabelname(String value)
266
	{
267
		this.lbl = value;
268
	}
269
 
270
	public void setViewpath(String value)
271
	{
272
		this.viewpath = value;
273
	}
274
 
275
	public void setViewname(String value)
276
	{
277
		this.viewnameProperty = value;
278
	}
279
 
280
	public void setViewcs(String value)
281
	{
282
		this.viewCS = value;
283
	}
284
 
285
	public void setFindCheckouts(String value)
286
	{
287
		findCheckouts = value;
288
	}
289
 
290
	public void setVersion(String value)
291
	{
292
		version = value;
293
	}
294
 
295
	private String getPath( String absolute )
296
	{
297
		int cindex = absolute.indexOf(' ');
298
		if ( cindex >= 0 )
299
		{
300
			if ( !absolute.startsWith("\"") )
301
			{
302
				absolute = "\"" + absolute;
303
			}
304
 
305
			if ( !absolute.endsWith("\"") )
306
			{
307
				absolute = absolute + "\"";
308
			}
309
		}
310
		return absolute;
311
	}
312
}