Blame | Last modification | View Log | RSS feed
package com.erggroup.mass.ant.taskdefs;import com.erggroup.mass.ant.Utils;import java.io.*;import net.sourceforge.clearcase.api.IClearcase;import net.sourceforge.clearcase.simple.ClearcaseCLI;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Project;import org.apache.tools.ant.Task;/****/public class CCLabel extends Task{public static final String COMPUTERNAME = "${env.COMPUTERNAME}";public static final String USERNAME = "${env.USERNAME}";/*** The name of the package being labelled*/private String packageName;/*** Indicates if a view should be created based on the created label*/private String mkview;/*** Indicates if a view should be created based on the created label*/private String mapview;/*** Provides a variable with which to populate the created view path*/private String lbl;/*** Find checkouts (optional) - looks for checkouts and aborts if any are* found.*/private String findCheckouts;/*** The fully qualified path of the view as created (if mkview is true)*/private String viewpath;/*** View config spec base filename (optional, if mkview is true)*/private String viewCS;/*** The name of the view as created (if mkview is true)*/private String viewnameProperty;/*** The version to apply a label against.*/private String version;/***/public void execute(){if ( packageName == null ){throw new BuildException("Attribute 'packageName' is mandatory.");}if ( version == null ){throw new BuildException("Attribute 'version' is mandatory.");}String labelname = packageName + "_" + project.replaceProperties(version);String projectPath = getPath( project.getBaseDir().getAbsolutePath() );IClearcase clearcase = new ClearcaseCLI();// Before proceeding, look for checkouts and abort if any are foundif ( findCheckouts != null && (findCheckouts.equalsIgnoreCase("true") || findCheckouts.equalsIgnoreCase("yes")) ){project.log("Searching for checkouts in directory \'" + project.getBaseDir().getAbsolutePath() + "\'.",Project.MSG_INFO);IClearcase.Status lscheckout = clearcase.cleartool( "lscheckout -short -me -recurse -cview " + projectPath);if ( !lscheckout.status ){project.log(lscheckout.message,Project.MSG_ERR);throw new BuildException("Could not find checkouts in directory \'" + project.getBaseDir().getAbsolutePath() + "\'.");}if ( lscheckout.message != null && lscheckout.message.length() > 0 ){throw new BuildException("Build aborted.\nYou have the following files checked out:\n" + lscheckout.message );}}// Do the labelling here.Utils.setWorkingDirectory(clearcase,project);project.log("Creating label \'" + labelname + "\'.",Project.MSG_INFO);IClearcase.Status makelabel = clearcase.cleartool( "mklbtype -global -nc " + labelname );if ( ! makelabel.status ){project.log(makelabel.message,Project.MSG_ERR);throw new BuildException("Could not create label \'" + labelname + "\'.\nMake sure that the label does not already exist.");}// Label the release directory and all files within...project.log("Labelling project, please wait (this could take some time for large projects)...",Project.MSG_INFO);IClearcase.Status label = clearcase.cleartool( "mklabel -replace -recurse -follow " + labelname + " " + projectPath );if ( ! label.status ){project.log(label.message,Project.MSG_ERR);throw new BuildException("Could not label '" + projectPath + "' with label \'" + labelname + "\'.");}File parentDirectory = project.getBaseDir().getParentFile();while ( parentDirectory != null && parentDirectory.getParentFile() != null ){String directoryPath = getPath( parentDirectory.getAbsolutePath() );label = clearcase.cleartool( "mklabel -replace -follow " + labelname + " " + directoryPath );if ( ! label.status ){project.log(label.message,Project.MSG_ERR);throw new BuildException("Could not label '" + directoryPath + "' with label \'" + labelname + "\'.");}parentDirectory = parentDirectory.getParentFile();}if ( lbl != null ){project.setNewProperty( lbl, labelname );}project.log("Successfully created label '" + labelname + "'.",Project.MSG_INFO);// Make a view (optional)if ( mkview != null && (mkview.equalsIgnoreCase("true") || mkview.equalsIgnoreCase("yes")) ){String viewhost = project.replaceProperties(COMPUTERNAME);String username = project.replaceProperties(USERNAME);if ( viewhost != null && username != null && !viewhost.equals(COMPUTERNAME) && !username.equals(USERNAME) ){String viewname = username + "_jats_build_" + labelname;project.log("Creating view '" + viewname + "'.",Project.MSG_INFO);try{IClearcase.Status mkview = clearcase.cleartool( "mkview -tag " + viewname + " \\\\" + viewhost + "\\Clearcase\\" + viewname );if ( ! mkview.status ){project.log(mkview.message,Project.MSG_ERR);throw new BuildException("Could not create view '" + viewname + "'.");}String viewspecFilename = "\\\\" + viewhost + "\\Clearcase\\" + viewname + "\\config_spec";File viewspec = new File(viewspecFilename);viewspec.delete();viewspec.createNewFile();FileOutputStream fileOut = new FileOutputStream(viewspec);PrintWriter viewspecOut = new PrintWriter(fileOut,true);if (viewCS != null){try{File viewCSFile = new File(viewCS);if (viewCSFile.exists()){FileReader reader = new FileReader(viewCSFile);BufferedReader bufferedReader = new BufferedReader(reader);String line = bufferedReader.readLine();while ( line != null ){viewspecOut.println(line);line = bufferedReader.readLine();}bufferedReader.close();reader.close();}}catch ( Exception x ){project.log("Could not read config spec '" + viewCS + "'.",Project.MSG_ERR);throw new BuildException("Could not create view '" + viewname + "' - could not read config spec.");}}viewspecOut.println("element * " + labelname + " -nocheckout");viewspecOut.close();fileOut.close();mkview = clearcase.cleartool( "setcs -tag " + viewname + " -current" );if ( ! mkview.status ){project.log(mkview.message,Project.MSG_ERR);throw new BuildException("Could not create view '" + viewname + "' - could not set config spec.");}if ( viewnameProperty != null ){project.setNewProperty( viewnameProperty, viewname );}if ( viewpath != null ){String viewPathW32 = project.getBaseDir().getAbsolutePath();int cpos = viewPathW32.indexOf(':');viewPathW32 = "O:/" + viewname + viewPathW32.substring(cpos+1);try{if ( mapview != null ){project.log("Mapping view to network drive '" + mapview + "'.",Project.MSG_INFO);Process p = Runtime.getRuntime().exec("subst "+ mapview + " O:/" + viewname );int rv = p.waitFor();if ( rv == 0 ){viewPathW32 = project.getBaseDir().getAbsolutePath();viewPathW32 = mapview + "/" + viewPathW32.substring(cpos+1);}}}catch ( InterruptedException x ){project.log(x.getMessage(),Project.MSG_WARN);viewPathW32 = "O:/" + viewname + viewPathW32.substring(cpos+1);}catch ( IOException x ){project.log(x.getMessage(),Project.MSG_WARN);viewPathW32 = "O:/" + viewname + viewPathW32.substring(cpos+1);}File viewDirectory = new File(viewPathW32);project.setNewProperty( viewpath, viewDirectory.getAbsolutePath() );}project.log("Successfully created view '" + viewname + "'.",Project.MSG_INFO);}catch (Exception x){project.log(x.getMessage(),Project.MSG_ERR);throw new BuildException("Could not create view.");}}}}public void setPackage(String value){this.packageName = value;}public void setMkview(String value){this.mkview = value;}public void setMapview(String value){this.mapview = value;}public void setLabelname(String value){this.lbl = value;}public void setViewpath(String value){this.viewpath = value;}public void setViewname(String value){this.viewnameProperty = value;}public void setViewcs(String value){this.viewCS = value;}public void setFindCheckouts(String value){findCheckouts = value;}public void setVersion(String value){version = value;}private String getPath( String absolute ){int cindex = absolute.indexOf(' ');if ( cindex >= 0 ){if ( !absolute.startsWith("\"") ){absolute = "\"" + absolute;}if ( !absolute.endsWith("\"") ){absolute = absolute + "\"";}}return absolute;}}