<commit_before>/*
 * Copyright (c) 2005 Your Corporation. All Rights Reserved.
 */
package org.apache.maven.doxia;

import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Site;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.observers.Debug;
import org.apache.maven.wagon.providers.ssh.SshCommandExecutor;
import org.apache.maven.wagon.repository.Repository;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


/**
 * Deploys website using scp protocol.
 * First website files are packaged into zip archive,
 * then archive is transfred to remote host, nextly it is un-archived.
 * This method of deployment should normally be much faster
 * then making file by file copy.
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 * @goal deploy
 */
public class ScpSiteDeployMojo
    extends AbstractMojo
{
    /**
     * @parameter alias="siteDirectory" expression="${project.build.directory}/site"
     * @required
     */
    private String inputDirectory;

    /**
     * @parameter expression="${project.build.directory}"
     * @required
     */
    private String workingDirectory;

    /**
     * @parameter
     */
    private String unzipCommand = "unzip -o";

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * @parameter expression="${component.org.apache.maven.artifact.manager.WagonManager}"
     * @required
     * @readonly
     */
    private WagonManager wagonManager;

    public void execute()
        throws MojoExecutionException
    {
        File baseDir = new File( inputDirectory );

        File zipFile;

        try
        {
            zipFile = File.createTempFile( "site", ".zip", new File( workingDirectory ) );
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Cannot create site archive!", e );
        }

        SshCommandExecutor commandExecutor = null;
        try
        {
            DistributionManagement distributionManagement = project.getDistributionManagement();

            if ( distributionManagement == null )
            {
                throw new MojoExecutionException( "Missing distribution management information in the project" );
            }

            Site site = distributionManagement.getSite();

            if ( site == null )
            {
                throw new MojoExecutionException( "Missing site information in the distribution management element in the project.." );
            }

            String url = site.getUrl();

            String id = site.getId();

            if ( url == null )
            {
                throw new MojoExecutionException( "The URL to the site is missing in the project descriptor." );
            }

            Repository repository = new Repository( id, url );

            if ( !"scp".equals( repository.getProtocol() ) )
            {
                throw new MojoExecutionException( "The deploy mojo currently only supports site deployment using the 'scp' protocol." );
            }

            commandExecutor = (SshCommandExecutor) wagonManager.getWagon( "scp" );

            commandExecutor.connect( repository, wagonManager.getAuthenticationInfo( id ) );

            String basedir = repository.getBasedir();

            List files = FileUtils.getFileNames( baseDir, "**/**", "", false );

            createZip( files, zipFile, baseDir );

            Debug debug = new Debug();

            commandExecutor.addSessionListener( debug );

            commandExecutor.addTransferListener( debug );

            String cmd = " mkdir -p " + basedir;

            commandExecutor.executeCommand( cmd );

            commandExecutor.put( zipFile, zipFile.getName() );

            // TODO: cat to file is temporary until the ssh executor is fixed to deal with output
            cmd = " cd " + basedir + ";" + unzipCommand + " " + zipFile.getName() + " >scpdeploymojo.log";

            commandExecutor.executeCommand( cmd );

            if ( !basedir.endsWith( "/" ) )
            {
                basedir = basedir + "/";
            }

            commandExecutor.executeCommand( "rm -f " + basedir + zipFile.getName() + " scpdeploymojo.log" );
        }
        catch ( Exception e )
        {
            throw new MojoExecutionException( "Error transfering site archive!", e );
        }
        finally
        {
            if ( commandExecutor != null )
            {
                try
                {
                    commandExecutor.disconnect();
                }
                catch ( ConnectionException e )
                {
                    //what to to here?
                }
            }

            if ( !zipFile.delete() )
            {

                zipFile.deleteOnExit();
            }
        }
    }


    public void createZip( List files, File zipName, File basedir )
        throws Exception
    {
        ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( zipName ) );

        try
        {
            for ( int i = 0; i < files.size(); i++ )
            {
                String file = (String) files.get( i );

                writeZipEntry( zos, new File( basedir, file ), file );
            }
        }
        finally
        {
            zos.close();
        }
    }

    private void writeZipEntry( ZipOutputStream jar, File source, String entryName )
        throws Exception
    {
        byte[] buffer = new byte[1024];

        int bytesRead;

        FileInputStream is = new FileInputStream( source );

        try
        {
            ZipEntry entry = new ZipEntry( entryName );

            jar.putNextEntry( entry );

            while ( ( bytesRead = is.read( buffer ) ) != -1 )
            {
                jar.write( buffer, 0, bytesRead );
            }
        }

        finally
        {
            is.close();
        }
    }
}
<commit_msg>o store temp file in temp directory, not target which may not exist o error out if site doesn't exist to copy. o clean up<commit_after>package org.apache.maven.doxia;

/*
 * Copyright 2001-2005 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.model.DistributionManagement;
import org.apache.maven.model.Site;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.observers.Debug;
import org.apache.maven.wagon.providers.ssh.SshCommandExecutor;
import org.apache.maven.wagon.repository.Repository;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * Deploys website using scp protocol.
 * First website files are packaged into zip archive,
 * then archive is transfred to remote host, nextly it is un-archived.
 * This method of deployment should normally be much faster
 * then making file by file copy.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Id$
 * @goal deploy
 */
public class ScpSiteDeployMojo
    extends AbstractMojo
{
    /**
     * @parameter alias="siteDirectory" expression="${project.build.directory}/site"
     * @required
     */
    private File inputDirectory;

    /**
     * @parameter
     */
    private String unzipCommand = "unzip -o";

    /**
     * @parameter expression="${project}"
     * @required
     * @readonly
     */
    private MavenProject project;

    /**
     * @parameter expression="${component.org.apache.maven.artifact.manager.WagonManager}"
     * @required
     * @readonly
     */
    private WagonManager wagonManager;

    public void execute()
        throws MojoExecutionException
    {
        if ( !inputDirectory.exists() )
        {
            throw new MojoExecutionException( "The site does not exist, please run site:site first" );
        }

        File zipFile;

        try
        {
            zipFile = File.createTempFile( "site", ".zip" );
        }
        catch ( IOException e )
        {
            throw new MojoExecutionException( "Cannot create site archive!", e );
        }

        DistributionManagement distributionManagement = project.getDistributionManagement();

        if ( distributionManagement == null )
        {
            throw new MojoExecutionException( "Missing distribution management information in the project" );
        }

        Site site = distributionManagement.getSite();

        if ( site == null )
        {
            throw new MojoExecutionException(
                "Missing site information in the distribution management element in the project.." );
        }

        String url = site.getUrl();

        String id = site.getId();

        if ( url == null )
        {
            throw new MojoExecutionException( "The URL to the site is missing in the project descriptor." );
        }

        Repository repository = new Repository( id, url );

        if ( !"scp".equals( repository.getProtocol() ) )
        {
            throw new MojoExecutionException(
                "The deploy mojo currently only supports site deployment using the 'scp' protocol." );
        }

        SshCommandExecutor commandExecutor = null;

        try
        {
            commandExecutor = (SshCommandExecutor) wagonManager.getWagon( "scp" );

            commandExecutor.connect( repository, wagonManager.getAuthenticationInfo( id ) );

            String basedir = repository.getBasedir();

            List files = FileUtils.getFileNames( inputDirectory, "**/**", "", false );

            createZip( files, zipFile, inputDirectory );

            Debug debug = new Debug();

            commandExecutor.addSessionListener( debug );

            commandExecutor.addTransferListener( debug );

            String cmd = " mkdir -p " + basedir;

            commandExecutor.executeCommand( cmd );

            commandExecutor.put( zipFile, zipFile.getName() );

            // TODO: cat to file is temporary until the ssh executor is fixed to deal with output
            cmd = " cd " + basedir + ";" + unzipCommand + " " + zipFile.getName() + " >scpdeploymojo.log";

            commandExecutor.executeCommand( cmd );

            if ( !basedir.endsWith( "/" ) )
            {
                basedir = basedir + "/";
            }

            commandExecutor.executeCommand( "rm -f " + basedir + zipFile.getName() + " scpdeploymojo.log" );
        }
        catch ( Exception e )
        {
            throw new MojoExecutionException( "Error transfering site archive!", e );
        }
        finally
        {
            if ( commandExecutor != null )
            {
                try
                {
                    commandExecutor.disconnect();
                }
                catch ( ConnectionException e )
                {
                    //what to to here?
                }
            }

            if ( !zipFile.delete() )
            {
                zipFile.deleteOnExit();
            }
        }
    }


    public void createZip( List files, File zipName, File basedir )
        throws Exception
    {
        ZipOutputStream zos = new ZipOutputStream( new FileOutputStream( zipName ) );

        try
        {
            for ( int i = 0; i < files.size(); i++ )
            {
                String file = (String) files.get( i );

                writeZipEntry( zos, new File( basedir, file ), file );
            }
        }
        finally
        {
            zos.close();
        }
    }

    private void writeZipEntry( ZipOutputStream jar, File source, String entryName )
        throws Exception
    {
        byte[] buffer = new byte[1024];

        int bytesRead;

        FileInputStream is = new FileInputStream( source );

        try
        {
            ZipEntry entry = new ZipEntry( entryName );

            jar.putNextEntry( entry );

            while ( ( bytesRead = is.read( buffer ) ) != -1 )
            {
                jar.write( buffer, 0, bytesRead );
            }
        }

        finally
        {
            is.close();
        }
    }
}

{'fraction_non_alphanumeric': 0.09731921603964858, 'fraction_numerical': 0.0022527596305474207, 'mean_word_length': 1.9399558498896248, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5398713', 'n_tokens_mistral': 3542, 'n_tokens_neox': 3330, 'n_words': 959}