Search in sources :

Example 11 with CommandLineException

use of org.codehaus.plexus.util.cli.CommandLineException in project maven-scm by apache.

the class PerforceCheckOutCommand method executeCheckOutCommand.

/**
 * Check out the depot code at <code>repo.getPath()</code> into the target
 * directory at <code>files.getBasedir</code>. Perforce does not support
 * arbitrary checkout of versioned source so we need to set up a well-known
 * clientspec which will hold the required info.
 * <p/>
 * 1) A clientspec will be created or updated which holds a temporary
 * mapping from the repo path to the target directory.
 * 2) This clientspec is sync'd to pull all the files onto the client
 * <p/>
 * {@inheritDoc}
 */
protected CheckOutScmResult executeCheckOutCommand(ScmProviderRepository repo, ScmFileSet files, ScmVersion version, boolean recursive, boolean shallow) throws ScmException {
    PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
    File workingDirectory = new File(files.getBasedir().getAbsolutePath());
    actualLocation = PerforceScmProvider.getRepoPath(getLogger(), prepo, files.getBasedir());
    String specname = PerforceScmProvider.getClientspecName(getLogger(), prepo, workingDirectory);
    PerforceCheckOutConsumer consumer = new PerforceCheckOutConsumer(specname, actualLocation);
    if (getLogger().isInfoEnabled()) {
        getLogger().info("Checkout working directory: " + workingDirectory);
    }
    Commandline cl = null;
    // Create or update a clientspec so we can checkout the code to a particular location
    try {
        // Ahhh, glorious Perforce.  Create and update of clientspecs is the exact
        // same operation so we don't need to distinguish between the two modes.
        cl = PerforceScmProvider.createP4Command(prepo, workingDirectory);
        cl.createArg().setValue("client");
        cl.createArg().setValue("-i");
        if (getLogger().isInfoEnabled()) {
            getLogger().info("Executing: " + PerforceScmProvider.clean(cl.toString()));
        }
        String client = PerforceScmProvider.createClientspec(getLogger(), prepo, workingDirectory, actualLocation);
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Updating clientspec:\n" + client);
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        int exitCode = CommandLineUtils.executeCommandLine(cl, new ByteArrayInputStream(client.getBytes()), consumer, err);
        if (exitCode != 0) {
            String cmdLine = CommandLineUtils.toString(cl.getCommandline());
            StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
            msg.append('\n');
            msg.append("Command line was:" + cmdLine);
            throw new CommandLineException(msg.toString());
        }
    } catch (CommandLineException e) {
        if (getLogger().isErrorEnabled()) {
            getLogger().error("CommandLineException " + e.getMessage(), e);
        }
    }
    boolean clientspecExists = consumer.isSuccess();
    // Perform the actual checkout using that clientspec
    try {
        if (clientspecExists) {
            try {
                getLastChangelist(prepo, workingDirectory, specname);
                cl = createCommandLine(prepo, workingDirectory, version, specname);
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Executing: " + PerforceScmProvider.clean(cl.toString()));
                }
                Process proc = cl.execute();
                BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("Consuming: " + line);
                    }
                    consumer.consumeLine(line);
                }
                CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
                int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, err);
                if (exitCode != 0) {
                    String cmdLine = CommandLineUtils.toString(cl.getCommandline());
                    StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
                    msg.append('\n');
                    msg.append("Command line was:" + cmdLine);
                    throw new CommandLineException(msg.toString());
                }
                if (getLogger().isDebugEnabled()) {
                    getLogger().debug("Perforce sync complete.");
                }
            } catch (CommandLineException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("CommandLineException " + e.getMessage(), e);
                }
            } catch (IOException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("IOException " + e.getMessage(), e);
                }
            }
        }
        if (consumer.isSuccess()) {
            return new CheckOutScmResult(cl.toString(), consumer.getCheckedout());
        } else {
            return new CheckOutScmResult(cl.toString(), "Unable to sync.  Are you logged in?", consumer.getOutput(), consumer.isSuccess());
        }
    } finally {
        // Support transient clientspecs as we don't want to create 1000s of permanent clientspecs
        if (clientspecExists && !prepo.isPersistCheckout()) {
            // Delete the clientspec
            InputStreamReader isReader = null;
            InputStreamReader isReaderErr = null;
            try {
                cl = PerforceScmProvider.createP4Command(prepo, workingDirectory);
                cl.createArg().setValue("client");
                cl.createArg().setValue("-d");
                cl.createArg().setValue(specname);
                if (getLogger().isInfoEnabled()) {
                    getLogger().info("Executing: " + PerforceScmProvider.clean(cl.toString()));
                }
                Process proc = cl.execute();
                isReader = new InputStreamReader(proc.getInputStream());
                BufferedReader br = new BufferedReader(isReader);
                String line;
                while ((line = br.readLine()) != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("Consuming: " + line);
                    }
                    consumer.consumeLine(line);
                }
                br.close();
                // Read errors from STDERR
                isReaderErr = new InputStreamReader(proc.getErrorStream());
                BufferedReader brErr = new BufferedReader(isReaderErr);
                while ((line = brErr.readLine()) != null) {
                    if (getLogger().isDebugEnabled()) {
                        getLogger().debug("Consuming stderr: " + line);
                    }
                    consumer.consumeLine(line);
                }
                brErr.close();
            } catch (CommandLineException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("CommandLineException " + e.getMessage(), e);
                }
            } catch (IOException e) {
                if (getLogger().isErrorEnabled()) {
                    getLogger().error("IOException " + e.getMessage(), e);
                }
            } finally {
                IOUtil.close(isReader);
                IOUtil.close(isReaderErr);
            }
        } else if (clientspecExists) {
            // SCM-165 Save clientspec in memory so we can reuse it with further commands in this VM.
            System.setProperty(PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY, specname);
        }
    }
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) ByteArrayInputStream(java.io.ByteArrayInputStream) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) BufferedReader(java.io.BufferedReader) CheckOutScmResult(org.apache.maven.scm.command.checkout.CheckOutScmResult) PerforceScmProviderRepository(org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository) File(java.io.File)

Example 12 with CommandLineException

use of org.codehaus.plexus.util.cli.CommandLineException in project maven-scm by apache.

the class PerforceRemoveCommand method executeRemoveCommand.

/**
 * {@inheritDoc}
 */
protected ScmResult executeRemoveCommand(ScmProviderRepository repo, ScmFileSet files, String message) throws ScmException {
    Commandline cl = createCommandLine((PerforceScmProviderRepository) repo, files.getBasedir(), files);
    PerforceRemoveConsumer consumer = new PerforceRemoveConsumer();
    try {
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, err);
        if (exitCode != 0) {
            String cmdLine = CommandLineUtils.toString(cl.getCommandline());
            StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
            msg.append('\n');
            msg.append("Command line was:" + cmdLine);
            throw new CommandLineException(msg.toString());
        }
    } catch (CommandLineException e) {
        throw new ScmException("CommandLineException " + e.getMessage(), e);
    }
    return new RemoveScmResult(cl.toString(), consumer.getRemovals());
}
Also used : ScmException(org.apache.maven.scm.ScmException) Commandline(org.codehaus.plexus.util.cli.Commandline) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) RemoveScmResult(org.apache.maven.scm.command.remove.RemoveScmResult) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 13 with CommandLineException

use of org.codehaus.plexus.util.cli.CommandLineException in project maven-scm by apache.

the class PerforceStatusCommand method readOpened.

private Commandline readOpened(PerforceScmProviderRepository prepo, ScmFileSet files, PerforceStatusConsumer consumer) {
    Commandline cl = createOpenedCommandLine(prepo, files.getBasedir(), actualLocation);
    try {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(PerforceScmProvider.clean("Executing " + cl.toString()));
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, err);
        if (exitCode != 0) {
            String cmdLine = CommandLineUtils.toString(cl.getCommandline());
            StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
            msg.append('\n');
            msg.append("Command line was:" + cmdLine);
            throw new CommandLineException(msg.toString());
        }
    } catch (CommandLineException e) {
        if (getLogger().isErrorEnabled()) {
            getLogger().error("CommandLineException " + e.getMessage(), e);
        }
    }
    return cl;
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 14 with CommandLineException

use of org.codehaus.plexus.util.cli.CommandLineException in project maven-scm by apache.

the class PerforceTagCommand method createLabel.

private void createLabel(ScmProviderRepository repo, ScmFileSet files, String tag, PerforceTagConsumer consumer, boolean lock) {
    Commandline cl = createLabelCommandLine((PerforceScmProviderRepository) repo, files.getBasedir());
    DataOutputStream dos = null;
    InputStreamReader isReader = null;
    InputStreamReader isReaderErr = null;
    try {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(PerforceScmProvider.clean("Executing: " + cl.toString()));
        }
        Process proc = cl.execute();
        OutputStream out = proc.getOutputStream();
        dos = new DataOutputStream(out);
        String label = createLabelSpecification((PerforceScmProviderRepository) repo, tag, lock);
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("LabelSpec: " + NEWLINE + label);
        }
        dos.write(label.getBytes());
        dos.close();
        out.close();
        // TODO find & use a less naive InputStream multiplexer
        isReader = new InputStreamReader(proc.getInputStream());
        isReaderErr = new InputStreamReader(proc.getErrorStream());
        BufferedReader stdout = new BufferedReader(isReader);
        BufferedReader stderr = new BufferedReader(isReaderErr);
        String line;
        while ((line = stdout.readLine()) != null) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Consuming stdout: " + line);
            }
            consumer.consumeLine(line);
        }
        while ((line = stderr.readLine()) != null) {
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Consuming stderr: " + line);
            }
            consumer.consumeLine(line);
        }
        stderr.close();
        stdout.close();
    } catch (CommandLineException e) {
        if (getLogger().isErrorEnabled()) {
            getLogger().error("CommandLineException " + e.getMessage(), e);
        }
    } catch (IOException e) {
        if (getLogger().isErrorEnabled()) {
            getLogger().error("IOException " + e.getMessage(), e);
        }
    } finally {
        IOUtil.close(dos);
        IOUtil.close(isReader);
        IOUtil.close(isReaderErr);
    }
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) InputStreamReader(java.io.InputStreamReader) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) DataOutputStream(java.io.DataOutputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Example 15 with CommandLineException

use of org.codehaus.plexus.util.cli.CommandLineException in project maven-scm by apache.

the class PerforceTagCommand method syncLabel.

private void syncLabel(ScmProviderRepository repo, ScmFileSet files, String tag, PerforceTagConsumer consumer) {
    Commandline cl = createLabelsyncCommandLine((PerforceScmProviderRepository) repo, files.getBasedir(), files, tag);
    try {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug(PerforceScmProvider.clean("Executing: " + cl.toString()));
        }
        CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
        int exitCode = CommandLineUtils.executeCommandLine(cl, consumer, err);
        if (exitCode != 0) {
            String cmdLine = CommandLineUtils.toString(cl.getCommandline());
            StringBuilder msg = new StringBuilder("Exit code: " + exitCode + " - " + err.getOutput());
            msg.append('\n');
            msg.append("Command line was:" + cmdLine);
            throw new CommandLineException(msg.toString());
        }
    } catch (CommandLineException e) {
        if (getLogger().isErrorEnabled()) {
            getLogger().error("CommandLineException " + e.getMessage(), e);
        }
    }
}
Also used : Commandline(org.codehaus.plexus.util.cli.Commandline) CommandLineUtils(org.codehaus.plexus.util.cli.CommandLineUtils) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException)

Aggregations

CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)94 CommandLineUtils (org.codehaus.plexus.util.cli.CommandLineUtils)76 Commandline (org.codehaus.plexus.util.cli.Commandline)67 ScmException (org.apache.maven.scm.ScmException)56 IOException (java.io.IOException)20 SvnCommandLineUtils (org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils)18 File (java.io.File)14 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)13 BufferedReader (java.io.BufferedReader)6 BlameScmResult (org.apache.maven.scm.command.blame.BlameScmResult)6 StatusScmResult (org.apache.maven.scm.command.status.StatusScmResult)6 SvnScmProviderRepository (org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository)6 InputStreamReader (java.io.InputStreamReader)5 ChangeLogScmResult (org.apache.maven.scm.command.changelog.ChangeLogScmResult)5 ChangeLogSet (org.apache.maven.scm.command.changelog.ChangeLogSet)5 CheckOutScmResult (org.apache.maven.scm.command.checkout.CheckOutScmResult)5 StreamConsumer (org.codehaus.plexus.util.cli.StreamConsumer)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 StringTokenizer (java.util.StringTokenizer)4 CheckInScmResult (org.apache.maven.scm.command.checkin.CheckInScmResult)4