<commit_before><?php

class S3Export_Cli extends CM_Cli_Runnable_Abstract implements CM_Service_ManagerAwareInterface {

    use CM_Service_ManagerAwareTrait;

    public function __construct(CM_InputStream_Interface $input = null, CM_OutputStream_Interface $output = null) {
        parent::__construct($input, $output);
        $this->setServiceManager(CM_Service_Manager::getInstance());
    }

    /**
     * @param string $devicePath
     * @param string $truecryptPassword
     */
    public function verifyDisk($devicePath, $truecryptPassword) {
    }

    /**
     * @param string $device
     * @param bool $confirm
     * @throws CM_Exception_Invalid
     */
    public function initDisk($manifestPath, $device, $confirm = false) {
        if (!preg_match('/\d+$/', $device)) {
            CM_Util::exec('sgdisk', ['-o', $device]);
            $startSector = CM_Util::exec('sgdisk', ['-F', $device]);
            $endSector = CM_Util::exec('sgdisk', ['-E', $device]);
            CM_Util::exec('sgdisk', ['-n', '1:' . $startSector . ':' . $endSector, $device]);
            $device = $device . '1';
        }
        CM_Util::exec('sudo mkfs', ['-t', 'ext4', '-m', '0', $device]);
        $mountpoint = $this->_getLocalFilesystemPath($this->_getFilesystemBackupEncrypted());
        CM_Util::exec('sudo mount', [$device, $mountpoint]);

        $file = new CM_File($manifestPath);
        $manifest = $file->read();
        if (!preg_match('/fileSystem:(.*)/', $manifest, $matches)) {
            throw new CM_Exception_Invalid('Manifest file has not fileSystem field');
            $this->_cleanup();
        }
        if (!$matches[1] == 'EXT4') {
            throw new CM_Exception_Invalid('Only file system EXT4 supported (manifest)');
            $this->_cleanup();
        }
        $apiResponse = $this->_createAWSJob($manifest, !$confirm);

        $signatureFile = new CM_File($mountpoint . '/SIGNATURE');
        $signatureFile->write($apiResponse->get('SignatureFileContents'));
        $this->_cleanup();

        print("\nCreate Job completed:\n");
        print("---------------------\n");
        print('JobID: ' . $apiResponse->get('JobId') . "\n");
        print("\n\n");
    }

    /**
     * @param string $jobId
     */
    public function cancelJob($jobId) {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        print_r($client->cancelJob(array(
            'JobId' => $jobId,
        )));
    }

    public function listJobs() {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        print_r($client->listJobs());
    }

    /**
     * @param string $jobId
     */
    public function getStatus($jobId) {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        print_r($client->getStatus(array(
            'JobId' => $jobId,
        )));
    }

    /**
     *
     * @param string $manifest
     * @param bool $dryRun
     * @return String awsJobId
     */
    private function _createAWSJob($manifest, $dryRun = true) {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        $apiResponse = $client->createJob(array(
            'JobType' => 'Export',
            'Manifest' => $manifest,
            'ValidateOnly' => $dryRun,
        ));
        return $apiResponse;
    }

    private function _getAWSClient($credentials) {
        return Aws\ImportExport\ImportExportClient::factory($credentials);
    }

    /**
     * @return CM_File_Filesystem
     * @throws CM_Exception_Invalid
     */
    private function _getFilesystemBackupEncrypted() {
        return $this->getServiceManager()->get('s3export-filesystem-backup-encrypted', 'CM_File_Filesystem');
    }

    /**<commit_msg>Introduce --no-format arg, rename to --dry-run<commit_after><?php

class S3Export_Cli extends CM_Cli_Runnable_Abstract implements CM_Service_ManagerAwareInterface {

    use CM_Service_ManagerAwareTrait;

    public function __construct(CM_InputStream_Interface $input = null, CM_OutputStream_Interface $output = null) {
        parent::__construct($input, $output);
        $this->setServiceManager(CM_Service_Manager::getInstance());
    }

    /**
     * @param string $devicePath
     * @param string $truecryptPassword
     */
    public function verifyDisk($devicePath, $truecryptPassword) {
    }


    /**
     * @param string $device
     * @param bool $confirm
     * @throws CM_Exception_Invalid
     */
    public function initDisk($manifestPath, $device, $noFormat = false, $dryRun = false) {
        if ($noFormat !== true) {
            if (!preg_match('/\d+$/', $device)) {
                CM_Util::exec('sgdisk', ['-o', $device]);
                $startSector = CM_Util::exec('sgdisk', ['-F', $device]);
                $endSector = CM_Util::exec('sgdisk', ['-E', $device]);
                CM_Util::exec('sgdisk', ['-n', '1:' . $startSector . ':' . $endSector, $device]);
                $device = $device . '1';
            }
            CM_Util::exec('sudo mkfs', ['-t', 'ext4', '-m', '0', $device]);
        }
        $mountpoint = $this->_getLocalFilesystemPath($this->_getFilesystemBackupEncrypted());
        CM_Util::exec('sudo mount', [$device, $mountpoint]);

        $file = new CM_File($manifestPath);
        if ($file->isDirectory()) {
            $this->_cleanup();
            throw new CM_Exception_Invalid('Manifest file expected, path to directory given');
        }
        $manifest = $file->read();
        if (!preg_match('/fileSystem:(.*)/', $manifest, $matches)) {
            $this->_cleanup();
            throw new CM_Exception_Invalid('Manifest file has not fileSystem field');
        }
        if (!$matches[1] == 'EXT4') {
            $this->_cleanup();
            throw new CM_Exception_Invalid('Only file system EXT4 supported (manifest)');
        }
        $apiResponse = $this->_createAWSJob($manifest, $dryRun);

        $signatureFile = new CM_File($mountpoint . '/SIGNATURE');
        $signatureFile->write($apiResponse->get('SignatureFileContents'));
        $this->_cleanup();

        print("\nCreate Job completed:\n");
        print("---------------------\n");
        print('JobID: ' . $apiResponse->get('JobId') . "\n");
        print("\n\n");
    }

    /**
     * @param string $jobId
     */
    public function cancelJob($jobId) {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        print_r($client->cancelJob(array(
            'JobId' => $jobId,
        )));
    }

    public function listJobs() {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        print_r($client->listJobs());
    }

    /**
     * @param string $jobId
     */
    public function getStatus($jobId) {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        print_r($client->getStatus(array(
            'JobId' => $jobId,
        )));
    }

    /**
     *
     * @param string $manifest
     * @param bool $dryRun
     * @return \Guzzle\Service\Resource\Model
     */
    private function _createAWSJob($manifest, $dryRun = true) {
        $client = $this->_getAWSClient(CM_Config::get()->awsCredentials);
        $apiResponse = $client->createJob(array(
            'JobType' => 'Export',
            'Manifest' => $manifest,
            'ValidateOnly' => $dryRun,
        ));
        return $apiResponse;
    }

    /**
     * @param array $credentials
     * @return \Aws\ImportExport\ImportExportClient
     */
    private function _getAWSClient($credentials) {
        return Aws\ImportExport\ImportExportClient::factory($credentials);
    }

    /**
     * @return CM_File_Filesystem
     * @throws CM_Exception_Invalid
     */
    private function _getFilesystemBackupEncrypted() {
        return $this->getServiceManager()->get('s3export-filesystem-backup-encrypted', 'CM_File_Filesystem');
    }

    /**
{'fraction_non_alphanumeric': 0.1650571791613723, 'fraction_numerical': 0.002287166454891995, 'mean_word_length': 2.5327648114901256, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '6876037', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2304, 'n_words': 418}