<commit_before><?php
namespace Mage\Task\BuiltIn\Filesystem;

use Mage\Task\AbstractTask;
use Mage\Task\SkipException;

/**
 * Task for setting permissions on given paths. Change will be done on local or
 * remote host depending on the stage of the deployment.
 *
 * Usage :
 *   pre-deploy:
 *     - filesystem/permissions: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/cache, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775}
 *   on-deploy:
 *     - filesystem/permissions: {paths: app/cache:app/logs, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775}
 *
 * @author <NAME> <<EMAIL>>
 */
class PermissionsTask extends AbstractTask
{
    /**
     * Paths to change of permissions separated by PATH_SEPARATOR.
     *
     * If the stage is on local host you should give full paths. If on remote
     * you may give full or relative to the current release directory paths.
     *
     * @var string
     */
    private $paths;

    /**
     * If set to true, will check existance of given paths on the host and
     * throw SkipException if at least one does not exist.
     *
     * @var boolean
     */
    private $checkPathsExist = true;

    /**
     * Owner to set for the given paths (ex : "www-data")
     *
     * @var string
     */
    private $owner;

    /**
     * Group to set for the given paths (ex : "www-data")
     *
     * @var string
     */
    private $group;

    /**
     * Rights to set for the given paths (ex: "755")
     *
     * @var string
     */
    private $rights;

    /**
     * If set to true, will recursively change permissions on given paths.
     *
     * @var string
     */
    private $recursive = false;

    /**
     * Initialize parameters.
     *
     * @throws SkipException
     */
    public function init()
    {
        parent::init();

        if (! is_null($this->getParameter('checkPathsExist'))) {
            $this->setCheckPathsExist($this->getParameter('checkPathsExist'));
        }

        if (! $this->getParameter('paths')) {
            throw new SkipException('Param paths is mandatory');
        }
        $this->setPaths(explode(PATH_SEPARATOR, $this->getParameter('paths', '')));

        if (! is_null($this->getParameter('owner'))) {
            $this->setOwner($this->getParameter('owner'));
        }

        if (! is_null($this->getParameter('group'))) {
            $this->setGroup($this->getParameter('group'));
        }

        if (! is_null($this->getParameter('rights'))) {
            $this->setRights($this->getParameter('rights'));
        }

        if (! is_null($this->getParameter('recursive'))) {
            $this->setRecursive($this->getParameter('recursive'));
        }
    }

    /**
     * @return string
     */
    public function getName()
    {
        return "Change rights / owner / group for paths : " . $this->getPathsForCmd() . " [built-in]";
    }

    /**
     * @return boolean
     */
    public function run()
    {
        $command = '';
        $recursive = $this->recursive ? '-R' : '';

        if ($this->paths && $this->owner) {
            $command .= 'chown '. $recursive .' ' . $this->owner . ' ' . $this->getPathsForCmd() . ';';
        }

        if ($this->paths && $this->group) {
            $command .= 'chgrp '. $recursive .' ' . $this->group . ' ' . $this->getPathsForCmd() . ';';
        }

        if ($this->paths && $this->rights) {
            $command .= 'chmod '. $recursive .' ' . $this->rights . ' ' . $this->getPathsForCmd() . ';';
        }

        $result = $this->runCommand($command);

        return $result;
    }

    /**
     * Transforms paths array to a string separated by 1 space in order to use<commit_msg>More generic way to handle command line options<commit_after><?php
namespace Mage\Task\BuiltIn\Filesystem;

use Mage\Task\AbstractTask;
use Mage\Task\SkipException;

/**
 * Task for setting permissions on given paths. Change will be done on local or
 * remote host depending on the stage of the deployment.
 *
 * Usage :
 *   pre-deploy:
 *     - filesystem/permissions: {paths: /var/www/myapp/app/cache:/var/www/myapp/app/cache, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775}
 *   on-deploy:
 *     - filesystem/permissions: {paths: app/cache:app/logs, recursive: false, checkPathsExist: true, owner: www-data, group: www-data, rights: 775}
 *
 * @author <NAME> <<EMAIL>>
 */
class PermissionsTask extends AbstractTask
{
    /**
     * Paths to change of permissions separated by PATH_SEPARATOR.
     *
     * If the stage is on local host you should give full paths. If on remote
     * you may give full or relative to the current release directory paths.
     *
     * @var string
     */
    private $paths;

    /**
     * If set to true, will check existance of given paths on the host and
     * throw SkipException if at least one does not exist.
     *
     * @var boolean
     */
    private $checkPathsExist = true;

    /**
     * Owner to set for the given paths (ex : "www-data")
     *
     * @var string
     */
    private $owner;

    /**
     * Group to set for the given paths (ex : "www-data")
     *
     * @var string
     */
    private $group;

    /**
     * Rights to set for the given paths (ex: "755")
     *
     * @var string
     */
    private $rights;

    /**
     * If set to true, will recursively change permissions on given paths.
     *
     * @var string
     */
    private $recursive = false;

    /**
     * Initialize parameters.
     *
     * @throws SkipException
     */
    public function init()
    {
        parent::init();

        if (! is_null($this->getParameter('checkPathsExist'))) {
            $this->setCheckPathsExist($this->getParameter('checkPathsExist'));
        }

        if (! $this->getParameter('paths')) {
            throw new SkipException('Param paths is mandatory');
        }
        $this->setPaths(explode(PATH_SEPARATOR, $this->getParameter('paths', '')));

        if (! is_null($this->getParameter('owner'))) {
            $this->setOwner($this->getParameter('owner'));
        }

        if (! is_null($this->getParameter('group'))) {
            $this->setGroup($this->getParameter('group'));
        }

        if (! is_null($this->getParameter('rights'))) {
            $this->setRights($this->getParameter('rights'));
        }

        if (! is_null($this->getParameter('recursive'))) {
            $this->setRecursive($this->getParameter('recursive'));
        }
    }

    /**
     * @return string
     */
    public function getName()
    {
        return "Change rights / owner / group for paths : " . $this->getPathsForCmd() . " [built-in]";
    }

    /**
     * @return boolean
     */
    public function run()
    {
        $command = '';

        if ($this->paths && $this->owner) {
            $command .= 'chown '. $this->getOptionsForCmd() .' ' . $this->owner . ' ' . $this->getPathsForCmd() . ';';
        }

        if ($this->paths && $this->group) {
            $command .= 'chgrp '. $this->getOptionsForCmd()  .' ' . $this->group . ' ' . $this->getPathsForCmd() . ';';
        }

        if ($this->paths && $this->rights) {
            $command .= 'chmod '. $this->getOptionsForCmd()  .' ' . $this->rights . ' ' . $this->getPathsForCmd() . ';';
        }

        $result = $this->runCommand($command);

        return $result;
    }

    /**
     * Returns the options for the commands to run. Only supports -R for now.
     *
     * @return string
     */
    protected function getOptionsForCmd()
    {
        $optionsForCmd = '';
        $options = array(
            'R' => $this->recursive
        );

        foreach($options as $option => $apply) {
            if ($apply == true) {
                $optionsForCmd .= $option;
            }
        }

        return $optionsForCmd ? '-' . $optionsForCmd : '';
    }

    /**
     * Transforms paths array to a string separated by 1 space in order to use
{'fraction_non_alphanumeric': 0.15144320879670123, 'fraction_numerical': 0.0024990628514307135, 'mean_word_length': 2.0963249516441005, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 80, 'https://': 0, 'lorem ipsum': 0, 'www.': 16, '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': '5388232', 'n_tokens_mistral': 2457, 'n_tokens_neox': 2352, 'n_words': 610}