Skip to content
Permalink
Browse files

[JENKINS-7618] implemented function for getting the perforce response…

… directly from the p4 process as a raw byte array
  • Loading branch information...
Rob Petti
Rob Petti committed Jun 24, 2011
1 parent d15efe6 commit 6f25b91e0461cde44a7aaec7f4824b16c4be041b
@@ -466,7 +466,79 @@ protected StringBuilder getPerforceResponse(String cmd[], ResponseFilter filter)
return lines;
}

/**
/**
* Used by calls that make use of p4.exe's python dictionary output format.
* @param cmd
* @return
* @throws PerforceException
*/

protected byte[] getRawPerforceResponseBytes(String cmd[]) throws PerforceException {
List<Byte> bytes = new ArrayList<Byte>(1024);

Executor p4 = depot.getExecFactory().newExecutor();
String debugCmd = "";
// get entire cmd to execute
cmd = getExtraParams(cmd);

// setup information for logging...
for(String cm : cmd) {
debugCmd += cm + " ";
}

// Perform execution and IO
p4.exec(cmd);

try
{
char[] cbuf = new char[1024];
BufferedReader reader = p4.getReader();
p4.getWriter().close();
int readCount = -1;
while((readCount = reader.read(cbuf, 0, 1024)) != -1) {
for(int i=0; i<readCount; i++){
bytes.add(new Byte((byte)(cbuf[i]&0xff)));
}
}
}
catch(IOException ioe)
{
//this is generally not anything to worry about. The underlying
//perforce process terminated and that causes java to be angry.

// TODO Given the above comment, should we bother to log a warning?
// See this blog for a discussion of IOException with message "Write end dead" from pipes:
// http://techtavern.wordpress.com/2008/07/16/whats-this-ioexception-write-end-dead/

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
ioe.printStackTrace(pw);
pw.flush();
sw.flush();
getLogger().warn("IOException reading from Perforce process (may just be EOF)");
getLogger().warn(sw.toString());
}
finally{
try{
p4.getWriter().close();
} catch (IOException e) {
getLogger().warn("Write pipe failed to close.");
}
try{
p4.getReader().close();
} catch (IOException e) {
getLogger().warn("Read pipe failed to close.");
}
p4.close();
}
byte[] byteArray = new byte[bytes.size()];
for(int i=0; i<bytes.size(); i++){
byteArray[i] = bytes.get(i).byteValue();
}
return byteArray;
}

/**
* Tries to perform a p4 login if the security level on the server is set to level 3 and no ticket was set via
* depot.setP4Ticket().
* <p>
@@ -74,8 +74,8 @@ public Changelist getChangelist(int number) throws PerforceException {
*/
private void calculateWorkspacePaths(Changelist change) throws PerforceException{
for(Changelist.FileEntry file :change.getFiles()){
StringBuilder response = getPerforceResponse(new String[]{getP4Exe(),"-G","where",file.getFilename()});
PerforceSCMHelper.WhereMapping map = PerforceSCMHelper.parseWhereMapping(response.toString().getBytes());
byte[] bytes = getRawPerforceResponseBytes(new String[]{getP4Exe(),"-G","where",file.getFilename()});
PerforceSCMHelper.WhereMapping map = PerforceSCMHelper.parseWhereMapping(bytes);
file.setWorkspacePath(map.getWorkspacePath().replaceAll("^//\\S+?/",""));
}
}

0 comments on commit 6f25b91

Please sign in to comment.
You can’t perform that action at this time.