<commit_before><?php

namespace Heyday\Component\Beam\DeploymentProvider;

use Heyday\Component\Beam\DeploymentProvider\DeploymentResult;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Process\Process;

/**
 * Class Rsync
 * @package Heyday\Component\Beam\DeploymentProvider
 */
class Rsync extends Deployment implements DeploymentProvider
{
    /**
     * @var array
     */
    protected $options;
    /**
     * @param array $options
     */
    public function __construct(array $options)
    {
        $resolver = new OptionsResolver();
        $resolver->setOptional(
            array(
                'checksum',
                'delete',
                'archive',
                'compress',
                'delay-updates'
            )
        );
        $resolver->setAllowedTypes(
            array(
                'checksum'      => 'bool',
                'delete'        => 'bool',
                'archive'       => 'bool',
                'compress'      => 'bool',
                'delay-updates' => 'bool'
            )
        );
        $resolver->setDefaults(
            array(
                'checksum'      => true,
                'delete'        => false,
                'archive'       => true,
                'compress'      => true,
                'delay-updates' => true
            )
        );
        $this->options = $resolver->resolve($options);
    }
    /**
     * @{inheritDoc}
     */
    public function up(\Closure $output = null, $dryrun = false, DeploymentResult $deploymentResult = null)
    {
        return $this->deploy(
            $this->buildCommand(
                $this->beam->getLocalPath(),
                $this->getTargetPath(),
                $dryrun
            ),
            $output
        );
    }
    /**
     * @{inheritDoc}
     */
    public function down(\Closure $output = null, $dryrun = false, DeploymentResult $deploymentResult = null)
    {
        return $this->deploy(
            $this->buildCommand(
                $this->getTargetPath(),
                $this->beam->getLocalPath(),
                $dryrun
            ),
            $output
        );
    }
    /**
     * @param                    $command
     * @param  callable          $output
     * @return DeploymentResult
     * @throws \RuntimeException
     */
    protected function deploy($command, \Closure $output = null)
    {
        $this->generateExcludesFile();
        $process = $this->getProcess($command);
        $process->run($output);
        if (!$process->isSuccessful()) {
            throw new \RuntimeException($process->getErrorOutput());
        }
        $output = $process->getOutput();

        return new DeploymentResult($this->formatOutput($output));
    }
    /**
     * Builds the rsync command based of current options
     * @param         $fromPath
     * @param         $toPath
     * @param  bool   $dryrun
     * @return string
     */
    protected function buildCommand($fromPath, $toPath, $dryrun = false)
    {

        $flags = 'rlpD'; // recursion, links, perms, devices, specials

        $command = array(
            array(
                'rsync %s/ %s',
                $fromPath,
                $toPath
            ),
            '-' . $flags,
            '--itemize-changes'
        );

        if ($this->beam->hasPath()) {
            $paths = $this->beam->getOption('path');
            $excludes = array();
            $includes = array();

            foreach ($paths as $path) {
                $steps = $this->parsePathSteps($path);
                $last = count($steps) - 1;

                foreach ($steps as $index => $step) {
                    $includes[] = $step;

                    if ($index != $last) {
                        $excludes[] = "$step/*";
                    }
                }
            }

            // Exclude everything else
            $excludes[] = '/*';

            foreach ($includes as $include) {
                $command[] = array(
                    '--include="%s"',
                    $include
                );
            }

            foreach ($excludes as $exclude) {
                $command[] = array(
                    '--exclude="%s"',
                    $exclude
                );
            }
        }

        if ($dryrun) {
            $command[] = '--dry-run';
        }
        if ($this->options['checksum']) {
            $command[] = '--checksum';
        }
        if ($this->options['delete']) {
            $command[] = '--delete';
        }
        if ($this->options['compress']) {
            $command[] = '--compress';
        }
        if ($this->options['delay-updates']) {
            $command[] = '--delay-updates';
        }

        $command[] = array(
            '--exclude-from="%s"',
            $this->getExcludesPath()
        );

        foreach ($command as $key => $part) {
            if (is_array($part)) {
                $command[$key] = call_user_func_array('sprintf', $part);
            }
        }

        echo implode(' ', $command)."\n";

        return implode(' ', $command);
    }

    /**
     * @param $path
     * @return array|string
     */
    protected function parsePathSteps($path)
    {
        $steps = array();
        $folders = explode('/', $path);
        $lastIndex = count($folders) - 1;

        $partialPath = '';
        foreach ($folders as $index => $component) {<commit_msg>MINOR: Remove left in debugging line<commit_after><?php

namespace Heyday\Component\Beam\DeploymentProvider;

use Heyday\Component\Beam\DeploymentProvider\DeploymentResult;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Process\Process;

/**
 * Class Rsync
 * @package Heyday\Component\Beam\DeploymentProvider
 */
class Rsync extends Deployment implements DeploymentProvider
{
    /**
     * @var array
     */
    protected $options;
    /**
     * @param array $options
     */
    public function __construct(array $options)
    {
        $resolver = new OptionsResolver();
        $resolver->setOptional(
            array(
                'checksum',
                'delete',
                'archive',
                'compress',
                'delay-updates'
            )
        );
        $resolver->setAllowedTypes(
            array(
                'checksum'      => 'bool',
                'delete'        => 'bool',
                'archive'       => 'bool',
                'compress'      => 'bool',
                'delay-updates' => 'bool'
            )
        );
        $resolver->setDefaults(
            array(
                'checksum'      => true,
                'delete'        => false,
                'archive'       => true,
                'compress'      => true,
                'delay-updates' => true
            )
        );
        $this->options = $resolver->resolve($options);
    }
    /**
     * @{inheritDoc}
     */
    public function up(\Closure $output = null, $dryrun = false, DeploymentResult $deploymentResult = null)
    {
        return $this->deploy(
            $this->buildCommand(
                $this->beam->getLocalPath(),
                $this->getTargetPath(),
                $dryrun
            ),
            $output
        );
    }
    /**
     * @{inheritDoc}
     */
    public function down(\Closure $output = null, $dryrun = false, DeploymentResult $deploymentResult = null)
    {
        return $this->deploy(
            $this->buildCommand(
                $this->getTargetPath(),
                $this->beam->getLocalPath(),
                $dryrun
            ),
            $output
        );
    }
    /**
     * @param                    $command
     * @param  callable          $output
     * @return DeploymentResult
     * @throws \RuntimeException
     */
    protected function deploy($command, \Closure $output = null)
    {
        $this->generateExcludesFile();
        $process = $this->getProcess($command);
        $process->run($output);
        if (!$process->isSuccessful()) {
            throw new \RuntimeException($process->getErrorOutput());
        }
        $output = $process->getOutput();

        return new DeploymentResult($this->formatOutput($output));
    }
    /**
     * Builds the rsync command based of current options
     * @param         $fromPath
     * @param         $toPath
     * @param  bool   $dryrun
     * @return string
     */
    protected function buildCommand($fromPath, $toPath, $dryrun = false)
    {

        $flags = 'rlpD'; // recursion, links, perms, devices, specials

        $command = array(
            array(
                'rsync %s/ %s',
                $fromPath,
                $toPath
            ),
            '-' . $flags,
            '--itemize-changes'
        );

        if ($this->beam->hasPath()) {
            $paths = $this->beam->getOption('path');
            $excludes = array();
            $includes = array();

            foreach ($paths as $path) {
                $steps = $this->parsePathSteps($path);
                $last = count($steps) - 1;

                foreach ($steps as $index => $step) {
                    $includes[] = $step;

                    if ($index != $last) {
                        $excludes[] = "$step/*";
                    }
                }
            }

            // Exclude everything else
            $excludes[] = '/*';

            foreach ($includes as $include) {
                $command[] = array(
                    '--include="%s"',
                    $include
                );
            }

            foreach ($excludes as $exclude) {
                $command[] = array(
                    '--exclude="%s"',
                    $exclude
                );
            }
        }

        if ($dryrun) {
            $command[] = '--dry-run';
        }
        if ($this->options['checksum']) {
            $command[] = '--checksum';
        }
        if ($this->options['delete']) {
            $command[] = '--delete';
        }
        if ($this->options['compress']) {
            $command[] = '--compress';
        }
        if ($this->options['delay-updates']) {
            $command[] = '--delay-updates';
        }

        $command[] = array(
            '--exclude-from="%s"',
            $this->getExcludesPath()
        );

        foreach ($command as $key => $part) {
            if (is_array($part)) {
                $command[$key] = call_user_func_array('sprintf', $part);
            }
        }

        return implode(' ', $command);
    }

    /**
     * @param $path
     * @return array|string
     */
    protected function parsePathSteps($path)
    {
        $steps = array();
        $folders = explode('/', $path);
        $lastIndex = count($folders) - 1;

        $partialPath = '';
        foreach ($folders as $index => $component) {
{'fraction_non_alphanumeric': 0.14264678814651205, 'fraction_numerical': 0.0003681207436039021, 'mean_word_length': 1.3067289322861388, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3135125', 'n_tokens_mistral': 3021, 'n_tokens_neox': 2862, 'n_words': 564}