diff --git a/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java b/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java index 60c7764..7ccd29d 100644 --- a/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java +++ b/ant/src/org/apache/hadoop/hive/ant/QTestGenTask.java @@ -35,6 +35,7 @@ import com.google.common.base.Splitter; import com.google.common.collect.Sets; + import org.apache.commons.lang.StringUtils; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Task; @@ -312,6 +313,17 @@ public String getQueryFileRegex() { return queryFileRegex; } + private String createAlternativeFile(File file) throws Exception { + String fileParentDir = file.getParent(); + String fileName = file.getName(); + int dotIndex = fileName.lastIndexOf('.'); + String fileNameWithoutExtension = dotIndex == -1 ? fileName : fileName.substring(0, dotIndex); + String fileNameExtension = dotIndex == -1 ? "" : fileName.substring(dotIndex); + File alternativeFile = File.createTempFile(fileNameWithoutExtension, fileNameExtension, + new File(fileParentDir)); + return alternativeFile.getCanonicalPath(); + } + public void execute() throws BuildException { if (getTemplatePath().equals("")) { throw new BuildException("No templatePath attribute specified"); @@ -436,8 +448,10 @@ public void execute() throws BuildException { if (logFile != null) { File lf = new File(logFile); if (lf.exists()) { + System.out.println("Log file already exists: " + lf.getCanonicalPath()); if (!lf.delete()) { - throw new Exception("Could not delete log file " + lf.getCanonicalPath()); + System.out.println("Could not delete log file " + lf.getCanonicalPath()); + logFile = createAlternativeFile(lf); } } @@ -458,16 +472,17 @@ public void execute() throws BuildException { String qFileNamesFile = qFileNames.getCanonicalPath(); if (qFileNames.exists()) { + System.out.println("Query file names containing file already exists: " + qFileNamesFile); if (!qFileNames.delete()) { - throw new Exception("Could not delete old query file names containing file " + + System.out.println("Could not delete query file names containing file " + + qFileNames.getCanonicalPath()); + qFileNamesFile = createAlternativeFile(qFileNames); + } else if (!qFileNames.createNewFile()) { + System.out.println("Could not create query file names containing file " + qFileNamesFile); + qFileNamesFile = createAlternativeFile(qFileNames); } } - if (!qFileNames.createNewFile()) { - throw new Exception("Could not create query file names containing file " + - qFileNamesFile); - } - FileWriter fw = new FileWriter(qFileNames.getCanonicalFile()); BufferedWriter bw = new BufferedWriter(fw);