1 18 package org.apache.tools.ant.taskdefs.optional.pvcs; 19 20 import java.io.BufferedReader ; 21 import java.io.BufferedWriter ; 22 import java.io.File ; 23 import java.io.FileNotFoundException ; 24 import java.io.FileOutputStream ; 25 import java.io.FileReader ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.text.MessageFormat ; 29 import java.text.ParseException ; 30 import java.util.Enumeration ; 31 import java.util.Random ; 32 import java.util.Vector ; 33 import org.apache.tools.ant.BuildException; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.taskdefs.Execute; 36 import org.apache.tools.ant.taskdefs.ExecuteStreamHandler; 37 import org.apache.tools.ant.taskdefs.LogOutputStream; 38 import org.apache.tools.ant.taskdefs.LogStreamHandler; 39 import org.apache.tools.ant.taskdefs.PumpStreamHandler; 40 import org.apache.tools.ant.types.Commandline; 41 42 72 public class Pvcs extends org.apache.tools.ant.Task { 73 private static final int POS_1 = 1; 76 private static final int POS_2 = 2; 77 private static final int POS_3 = 3; 78 79 private String pvcsbin; 80 private String repository; 81 private String pvcsProject; 82 private Vector pvcsProjects; 83 private String workspace; 84 private String force; 85 private String promotiongroup; 86 private String label; 87 private String revision; 88 private boolean ignorerc; 89 private boolean updateOnly; 90 private String filenameFormat; 91 private String lineStart; 92 private String userId; 93 private String config; 94 97 private static final String PCLI_EXE = "pcli"; 98 99 102 104 107 private static final String GET_EXE = "get"; 108 109 110 116 protected int runCmd(Commandline cmd, ExecuteStreamHandler out) { 117 try { 118 Project aProj = getProject(); 119 Execute exe = new Execute(out); 120 exe.setAntRun(aProj); 121 exe.setWorkingDirectory(aProj.getBaseDir()); 122 exe.setCommandline(cmd.getCommandline()); 123 return exe.execute(); 124 } catch (java.io.IOException e) { 125 String msg = "Failed executing: " + cmd.toString() 126 + ". Exception: " + e.getMessage(); 127 throw new BuildException(msg, getLocation()); 128 } 129 } 130 131 private String getExecutable(String exe) { 132 StringBuffer correctedExe = new StringBuffer (); 133 if (getPvcsbin() != null) { 134 if (pvcsbin.endsWith(File.separator)) { 135 correctedExe.append(pvcsbin); 136 } else { 137 correctedExe.append(pvcsbin).append(File.separator); 138 } 139 } 140 return correctedExe.append(exe).toString(); 141 } 142 143 146 public void execute() throws org.apache.tools.ant.BuildException { 147 int result = 0; 148 149 if (repository == null || repository.trim().equals("")) { 150 throw new BuildException("Required argument repository not specified"); 151 } 152 153 Commandline commandLine = new Commandline(); 158 commandLine.setExecutable(getExecutable(PCLI_EXE)); 159 160 commandLine.createArgument().setValue("lvf"); 161 commandLine.createArgument().setValue("-z"); 162 commandLine.createArgument().setValue("-aw"); 163 if (getWorkspace() != null) { 164 commandLine.createArgument().setValue("-sp" + getWorkspace()); 165 } 166 commandLine.createArgument().setValue("-pr" + getRepository()); 167 168 String uid = getUserId(); 169 170 if (uid != null) { 171 commandLine.createArgument().setValue("-id" + uid); 172 } 173 174 if (getPvcsproject() == null && getPvcsprojects().isEmpty()) { 176 pvcsProject = "/"; 177 } 178 179 if (getPvcsproject() != null) { 180 commandLine.createArgument().setValue(getPvcsproject()); 181 } 182 if (!getPvcsprojects().isEmpty()) { 183 Enumeration e = getPvcsprojects().elements(); 184 while (e.hasMoreElements()) { 185 String projectName = ((PvcsProject) e.nextElement()).getName(); 186 if (projectName == null || (projectName.trim()).equals("")) { 187 throw new BuildException("name is a required attribute " 188 + "of pvcsproject"); 189 } 190 commandLine.createArgument().setValue(projectName); 191 } 192 } 193 194 File tmp = null; 195 File tmp2 = null; 196 try { 197 Random rand = new Random (System.currentTimeMillis()); 198 tmp = new File ("pvcs_ant_" + rand.nextLong() + ".log"); 199 FileOutputStream fos = new FileOutputStream (tmp); 200 tmp2 = new File ("pvcs_ant_" + rand.nextLong() + ".log"); 201 log(commandLine.describeCommand(), Project.MSG_VERBOSE); 202 try { 203 result = runCmd(commandLine, 204 new PumpStreamHandler(fos, 205 new LogOutputStream(this, 206 Project.MSG_WARN))); 207 } finally { 208 fos.close(); 209 } 210 211 if (Execute.isFailure(result) && !ignorerc) { 212 String msg = "Failed executing: " + commandLine.toString(); 213 throw new BuildException(msg, getLocation()); 214 } 215 216 if (!tmp.exists()) { 217 throw new BuildException("Communication between ant and pvcs " 218 + "failed. No output generated from executing PVCS " 219 + "commandline interface \"pcli\" and \"get\""); 220 } 221 222 log("Creating folders", Project.MSG_INFO); 224 createFolders(tmp); 225 226 massagePCLI(tmp, tmp2); 228 229 commandLine.clearArgs(); 231 commandLine.setExecutable(getExecutable(GET_EXE)); 232 233 if (getConfig() != null && getConfig().length() > 0) { 234 commandLine.createArgument().setValue("-c" + getConfig()); 235 } 236 237 if (getForce() != null && getForce().equals("yes")) { 238 commandLine.createArgument().setValue("-Y"); 239 } else { 240 commandLine.createArgument().setValue("-N"); 241 } 242 243 if (getPromotiongroup() != null) { 244 commandLine.createArgument().setValue("-G" 245 + getPromotiongroup()); 246 } else { 247 if (getLabel() != null) { 248 commandLine.createArgument().setValue("-v" + getLabel()); 249 } else { 250 if (getRevision() != null) { 251 commandLine.createArgument().setValue("-r" 252 + getRevision()); 253 } 254 } 255 } 256 257 if (updateOnly) { 258 commandLine.createArgument().setValue("-U"); 259 } 260 261 commandLine.createArgument().setValue("@" + tmp2.getAbsolutePath()); 262 log("Getting files", Project.MSG_INFO); 263 log("Executing " + commandLine.toString(), Project.MSG_VERBOSE); 264 result = runCmd(commandLine, 265 new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN)); 266 if (result != 0 && !ignorerc) { 267 String msg = "Failed executing: " + commandLine.toString() 268 + ". Return code was " + result; 269 throw new BuildException(msg, getLocation()); 270 } 271 272 } catch (FileNotFoundException e) { 273 String msg = "Failed executing: " + commandLine.toString() 274 + ". Exception: " + e.getMessage(); 275 throw new BuildException(msg, getLocation()); 276 } catch (IOException e) { 277 String msg = "Failed executing: " + commandLine.toString() 278 + ". Exception: " + e.getMessage(); 279 throw new BuildException(msg, getLocation()); 280 } catch (ParseException e) { 281 String msg = "Failed executing: " + commandLine.toString() 282 + ". Exception: " + e.getMessage(); 283 throw new BuildException(msg, getLocation()); 284 } finally { 285 if (tmp != null) { 286 tmp.delete(); 287 } 288 if (tmp2 != null) { 289 tmp2.delete(); 290 } 291 } 292 } 293 294 297 private void createFolders(File file) throws IOException , ParseException { 298 BufferedReader in = null; 299 try { 300 in = new BufferedReader (new FileReader (file)); 301 MessageFormat mf = new MessageFormat (getFilenameFormat()); 302 String line = in.readLine(); 303 while (line != null) { 304 log("Considering \"" + line + "\"", Project.MSG_VERBOSE); 305 if (line.startsWith("\"\\") || line.startsWith("\"/") || (line.length() > POS_3 && line.startsWith("\"") 309 && Character.isLetter(line.charAt(POS_1)) 310 && String.valueOf(line.charAt(POS_2)).equals(":") 311 && String.valueOf(line.charAt(POS_3)).equals("\\"))) { 312 Object [] objs = mf.parse(line); 313 String f = (String ) objs[1]; 314 int index = f.lastIndexOf(File.separator); 316 if (index > -1) { 317 File dir = new File (f.substring(0, index)); 318 if (!dir.exists()) { 319 log("Creating " + dir.getAbsolutePath(), 320 Project.MSG_VERBOSE); 321 if (dir.mkdirs()) { 322 log("Created " + dir.getAbsolutePath(), 323 Project.MSG_INFO); 324 } else { 325 log("Failed to create " 326 + dir.getAbsolutePath(), 327 Project.MSG_INFO); 328 } 329 } else { 330 log(dir.getAbsolutePath() + " exists. Skipping", 331 Project.MSG_VERBOSE); 332 } 333 } else { 334 log("File separator problem with " + line, 335 Project.MSG_WARN); 336 } 337 } else { 338 log("Skipped \"" + line + "\"", Project.MSG_VERBOSE); 339 } 340 line = in.readLine(); 341 } 342 } finally { 343 if (in != null) { 344 in.close(); 345 } 346 } 347 } 348 349 350 355 private void massagePCLI(File in, File out) 356 throws IOException { 357 BufferedReader inReader = null; 358 BufferedWriter outWriter = null; 359 try { 360 inReader = new BufferedReader (new FileReader (in)); 361 outWriter = new BufferedWriter (new FileWriter (out)); 362 String s = null; 363 while ((s = inReader.readLine()) != null) { 364 String sNormal = s.replace('\\', '/'); 365 outWriter.write(sNormal); 366 outWriter.newLine(); 367 } 368 } finally { 369 if (inReader != null) { 370 inReader.close(); 371 } 372 if (outWriter != null) { 373 outWriter.close(); 374 } 375 } 376 } 377 378 382 public String getRepository() { 383 return repository; 384 } 385 386 393 public String getFilenameFormat() { 394 return filenameFormat; 395 } 396 397 405 public void setFilenameFormat(String f) { 406 filenameFormat = f; 407 } 408 409 418 public String getLineStart() { 419 return lineStart; 420 } 421 422 431 public void setLineStart(String l) { 432 lineStart = l; 433 } 434 435 439 public void setRepository(String repo) { 440 repository = repo; 441 } 442 443 447 public String getPvcsproject() { 448 return pvcsProject; 449 } 450 451 456 public void setPvcsproject(String prj) { 457 pvcsProject = prj; 458 } 459 460 464 public Vector getPvcsprojects() { 465 return pvcsProjects; 466 } 467 468 472 public String getWorkspace() { 473 return workspace; 474 } 475 476 485 public void setWorkspace(String ws) { 486 workspace = ws; 487 } 488 489 493 public String getPvcsbin() { 494 return pvcsbin; 495 } 496 497 507 public void setPvcsbin(String bin) { 508 pvcsbin = bin; 509 } 510 511 515 public String getForce() { 516 return force; 517 } 518 519 528 public void setForce(String f) { 529 if (f != null && f.equalsIgnoreCase("yes")) { 530 force = "yes"; 531 } else { 532 force = "no"; 533 } 534 } 535 536 540 public String getPromotiongroup() { 541 return promotiongroup; 542 } 543 544 548 public void setPromotiongroup(String w) { 549 promotiongroup = w; 550 } 551 552 556 public String getLabel() { 557 return label; 558 } 559 560 564 public void setLabel(String l) { 565 label = l; 566 } 567 568 572 public String getRevision() { 573 return revision; 574 } 575 576 580 public void setRevision(String r) { 581 revision = r; 582 } 583 584 588 public boolean getIgnoreReturnCode() { 589 return ignorerc; 590 } 591 592 597 public void setIgnoreReturnCode(boolean b) { 598 ignorerc = b; 599 } 600 601 605 public void addPvcsproject(PvcsProject p) { 606 pvcsProjects.addElement(p); 607 } 608 609 613 public boolean getUpdateOnly() { 614 return updateOnly; 615 } 616 617 622 public void setUpdateOnly(boolean l) { 623 updateOnly = l; 624 } 625 626 630 public String getConfig() { 631 return config; 632 } 633 634 639 public void setConfig(File f) { 640 config = f.toString(); 641 } 642 643 644 648 public String getUserId() { 649 return userId; 650 } 651 652 656 public void setUserId(String u) { 657 userId = u; 658 } 659 660 663 public Pvcs() { 664 super(); 665 pvcsProject = null; 666 pvcsProjects = new Vector (); 667 workspace = null; 668 repository = null; 669 pvcsbin = null; 670 force = null; 671 promotiongroup = null; 672 label = null; 673 ignorerc = false; 674 updateOnly = false; 675 lineStart = "\"P:"; 676 filenameFormat = "{0}-arc({1})"; 677 } 678 } 679 680 | Popular Tags |