Skip to content
Permalink
Browse files
[JENKINS-21467] Unable to copy the sloccount summary when the job has…
… been executed on a slave with a different OS

- Previous code change that tries to evaluate the path on master machine removed.
- The code now evaluates the path on slave which is much more robust. The master receives only two strings with file name and absolute path relative to slave machine.
  • Loading branch information
mixalturek committed Jan 24, 2014
1 parent 2e8cb21 commit e55f588ed9746f73c7cb1f82b54c6b55ef16c3be
Showing 2 changed files with 54 additions and 40 deletions.
@@ -9,6 +9,7 @@
import hudson.model.Result;
import hudson.plugins.sloccount.model.SloccountPublisherReport;
import hudson.plugins.sloccount.model.SloccountParser;
import hudson.plugins.sloccount.model.SloccountPublisherReport.SlaveFile;
import hudson.remoting.VirtualChannel;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Recorder;
@@ -102,39 +103,6 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
return true;
}

/**
* Let's try to find out the original file name.
* <p>
* If the OS we are running on the master and the slave are different,
* there are some back slash forward conversion to apply.
*
* @param source the file object representing the source file
* @return see description
*/
private String revertToOriginalFileName(final File source) {

String fileSeparator = System.getProperty("file.separator");
if (source.getPath().startsWith(fileSeparator)) {
// well, for sure the file comes from unix like OS
if (fileSeparator.equals("\\")) {
// but as we are running on windows, we must revert the mapping
return source.getPath().replaceAll("\\\\", "/");
} else {
// unix file but we are running on unix... no problem
return source.getAbsolutePath();
}
} else {
// starts with a drive letter...
if (source.getPath().startsWith(fileSeparator)) {
// and we are running on windows. Too easy !
return source.getAbsolutePath();
} else {
// hum... revert the back slashes
return source.getPath().replaceAll("/", "\\");
}
}
}

/**
* Copy files to a build results directory. The copy of a file will be
* stored in plugin's subdirectory and a hashcode of its absolute path will
@@ -152,7 +120,7 @@ private String revertToOriginalFileName(final File source) {
* @throws InterruptedException
* if something fails
*/
private void copyFilesToBuildDirectory(List<File> sourceFiles,
private void copyFilesToBuildDirectory(List<SlaveFile> sourceFiles,
File rootDir, VirtualChannel channel) throws IOException,
InterruptedException{
File destDir = new File(rootDir, BUILD_SUBDIR);
@@ -163,13 +131,13 @@ private void copyFilesToBuildDirectory(List<File> sourceFiles,
+ destDir.getAbsolutePath());
}

for(File sourceFile : sourceFiles){
for(SlaveFile sourceFile : sourceFiles){
File masterFile = new File(destDir, Integer.toHexString(sourceFile
.hashCode()) + "_" + sourceFile.getName());

if(!masterFile.exists()){
FileOutputStream outputStream = new FileOutputStream(masterFile);
new FilePath(channel, revertToOriginalFileName(sourceFile))
new FilePath(channel, sourceFile.getAbsolutePath())
.copyTo(outputStream);
}
}
@@ -16,13 +16,13 @@
public class SloccountPublisherReport implements Serializable,
SloccountReportInterface {
/** Serial version UID. */
private static final long serialVersionUID = 0L;
private static final long serialVersionUID = 1L;

/** The language statistics. */
private final Map<String, LanguageStatistics> statistics = new HashMap<String, LanguageStatistics>();

/** The list of files from which the original report was created. */
private final List<File> sourceFiles = new LinkedList<File>();
private final List<SlaveFile> sourceFiles = new LinkedList<SlaveFile>();

/**
* Get the statistics.
@@ -45,7 +45,7 @@ public SloccountReportStatistics getStatistics(){
*
* @return the source files
*/
public List<File> getSourceFiles(){
public List<SlaveFile> getSourceFiles(){
return Collections.unmodifiableList(sourceFiles);
}

@@ -56,7 +56,7 @@ public List<File> getSourceFiles(){
* the source file
*/
public void addSourceFile(File sourceFile){
sourceFiles.add(sourceFile);
sourceFiles.add(new SlaveFile(sourceFile));
}

public void add(String filePath, String languageName, int lineCount){
@@ -91,4 +91,50 @@ public static class LanguageStatistics implements Serializable {
/** The number of files. */
int numFiles = 0;
}

/**
* Helper class to store a file name and an absolute path relative to the
* slave machine.
*
* @author Michal Turek
*/
public static class SlaveFile implements Serializable {
/** Serial version UID. */
private static final long serialVersionUID = 0L;

/** The file name. */
private final String name;

/** The absolute path to the file. */
private final String absolutePath;

/**
* Constructor.
*
* @param file
* the file in the file system
*/
public SlaveFile(File file) {
this.name = file.getName();
this.absolutePath = file.getAbsolutePath();
}

/**
* Get the file name.
*
* @return the file name
*/
public String getName() {
return name;
}

/**
* Get the absolute path to the file.
*
* @return the absolute path
*/
public String getAbsolutePath() {
return absolutePath;
}
}
}

0 comments on commit e55f588

Please sign in to comment.