public function execute(Config $config) { switch ($config->command) { case 'doctor': $doctor = new Tasks\Doctor($this->gremlin, $this->config); $doctor->run($config); break; case 'init': $task = new Tasks\Initproject($this->gremlin, $this->config); $task->run($config); break; case 'anonymize': $task = new Tasks\Anonymize($this->gremlin, $this->config); $task->run($config); break; case 'files': $task = new Tasks\Files($this->gremlin, $this->config); $task->run($config); break; case 'load': $task = new Tasks\Load($this->gremlin, $this->config); $task->run($config); break; case 'stat': $task = new Tasks\Stat($this->gremlin, $this->config); $task->run($config); break; case 'analyze': $task = new Tasks\Analyze($this->gremlin, $this->config); $task->run($config); break; case 'results': $task = new Tasks\Results($this->gremlin, $this->config); $task->run($config); break; case 'export': $task = new Tasks\Export($this->gremlin, $this->config); $task->run($config); break; case 'report': $task = new Tasks\Report2($this->gremlin, $this->config); $task->run($config); break; case 'project': $task = new Tasks\Project($this->gremlin, $this->config); $task->run($config); break; case 'magicnumber': $task = new Tasks\Magicnumber($this->gremlin, $this->config); $task->run($config); break; case 'clean': $task = new Tasks\Clean($this->gremlin, $this->config); $task->run($config); break; case 'status': $task = new Tasks\Status($this->gremlin, $this->config); $task->run($config); break; case 'help': $task = new Tasks\Help($this->gremlin, $this->config); $task->run($config); break; case 'cleandb': $task = new Tasks\CleanDb($this->gremlin, $this->config); $task->run($config); break; case 'onepage': $task = new Tasks\OnePage($this->gremlin, $this->config); $task->run($config); break; case 'update': $task = new Tasks\Update($this->gremlin, $this->config); $task->run($config); break; case 'onepagereport': $task = new Tasks\OnepageReport($this->gremlin, $this->config); $task->run($config); break; case 'phploc': $task = new Tasks\Phploc($this->gremlin, $this->config); $task->run($config); break; case 'findextlib': $task = new Tasks\FindExternalLibraries($this->gremlin, $this->config); $task->run($config); break; case 'dump': $task = new Tasks\Dump($this->gremlin, $this->config); $task->run($config); break; case 'jobqueue': $task = new Tasks\Jobqueue($this->gremlin, $this->config); $task->run($config); break; case 'queue': $task = new Tasks\Queue($this->gremlin, $this->config); $task->run($config); break; case 'test': $task = new Tasks\Test($this->gremlin, $this->config); $task->run($config); break; case 'remove': $task = new Tasks\Remove($this->gremlin, $this->config); $task->run($config); break; case 'server': $task = new Tasks\Server($this->gremlin, $this->config); $task->run($config); break; case 'upgrade': $task = new Tasks\Upgrade($this->gremlin, $this->config); $task->run($config); break; case 'version': default: $version = self::VERSION; $build = self::BUILD; $date = date('r', filemtime(__FILE__)); echo "\n ________ __ _ \n|_ __ | [ | _ / |_ \n | |_ \\_| _ __ ,--. | | / ] ,--. `| |-' \n | _| _ [ \\ [ ]`'_\\ : | '' < `'_\\ : | | \n _| |__/ | > ' < // | |, | |`\\ \\ // | |,| |, \n|________|[__]`\\_]\\'-;__/[__| \\_]\\'-;__/\\__/ \n \n\nExakat : @ 2014-2016 Damien Seguy. \nVersion : ", $version, ' - Build ', $build, ' - ', $date, "\n"; break; } }
public function run() { $progress = 0; $begin = microtime(true); $this->project_dir = $this->config->projects_root . '/projects/onepage/'; // checking for installation if (!file_exists($this->project_dir)) { shell_exec($this->config->php . ' ' . $this->config->executable . ' init -p onepage '); mkdir($this->project_dir . '/code', 0755); shell_exec($this->config->php . ' ' . $this->config->executable . ' phploc -p onepage '); } $this->updateProgress($progress++); // todo : check that there is indeed this project or create it. if (!file_exists($this->config->filename)) { throw new NoSuchFile($this->config->filename); } // todo : check that there is indeed this project or create it. if (!is_file($this->config->filename) || !is_readable($this->config->filename)) { die("'{$this->config->filename}' must be a readable file. Aborting\n"); } $this->cleanLogForProject('onepage'); copy($this->config->filename, $this->config->projects_root . '/projects/onepage/code/onepage.php'); $this->updateProgress($progress++); $this->logTime('Start'); $datastorePath = $this->config->projects_root . '/projects/onepage/datastore.sqlite'; if (file_exists($datastorePath)) { unlink($datastorePath); } unset($this->datastore); $this->datastore = new Datastore($this->config, Datastore::CREATE); $audit_start = time(); $this->datastore->addRow('hash', array('audit_start' => $audit_start, 'exakat_version' => Exakat::VERSION, 'exakat_build' => Exakat::BUILD)); display("Cleaning DB\n"); $task = new CleanDb($this->gremlin, $this->config); $task->run(); $this->updateProgress($progress++); $this->logTime('CleanDb'); display("Running files\n"); $task = new Files($this->gremlin, $this->config); $task->run(); $this->updateProgress($progress++); $this->logTime('Files'); display("Running project 'onepage'\n"); $task = new Load($this->gremlin, $this->config); $task->run(); display("Project loaded\n"); $this->updateProgress($progress++); $this->logTime('Loading'); $task = new Analyze($this->gremlin, $this->config); $task->run(); rename($this->config->projects_root . '/projects/onepage/log/analyze.log', $this->config->projects_root . '/projects/onepage/log/analyze.onepage.log'); display("Project analyzed\n"); $this->updateProgress($progress++); $this->logTime('Analyze'); $b1 = microtime(true); $task = new Dump($this->gremlin, $this->config); $task->run(); display("Project dumped\n"); $e1 = microtime(true); $task = new Report2($this->gremlin, $this->config); $task->run(); display("Project reported\n"); $this->logTime('Report'); $this->updateProgress($progress++); display("Project reported\n"); $this->updateProgress($progress++); // Clean code unlink($this->config->projects_root . '/projects/onepage/code/onepage.php'); $audit_end = time(); $this->datastore->addRow('hash', array('audit_end' => $audit_end, 'audit_length' => $audit_end - $audit_start)); $this->logTime('Final'); display("End 2\n"); $end = microtime(true); $this->updateProgress($progress++); $this->logTime('Files'); // Back to 0 $this->updateProgress(0); }
public function run() { $progress = 0; $project = $this->config->project; $this->project_dir = $this->config->projects_root . '/projects/' . $project; if ($this->config->project == "default") { throw new ProjectNeeded(); } if (!file_exists($this->config->projects_root . '/projects/' . $project)) { throw new NoSuchProject($this->config->project); } // cleaning log directory (possibly logs) $logs = glob($this->config->projects_root . '/projects/' . $project . '/log/*'); foreach ($logs as $log) { unlink($log); } $this->logTime('Start'); $this->addSnitch(array('step' => 'Start', 'project' => $this->config->project)); // cleaning datastore $this->datastore = new Datastore($this->config, Datastore::CREATE); $audit_start = time(); $this->datastore->addRow('hash', array('audit_start' => $audit_start, 'exakat_version' => Exakat::VERSION, 'exakat_build' => Exakat::BUILD)); display("Running project '{$project}'\n"); display("Cleaning DB\n"); $analyze = new CleanDb($this->gremlin, $this->config, Tasks::IS_SUBTASK); $analyze->run(); unset($analyze); $this->logTime('CleanDb'); $this->addSnitch(array('step' => 'Clean DB', 'project' => $this->config->project)); display("Search for external libraries\n"); $args = array(1 => 'findextlib', 2 => '-p', 3 => $this->config->project, 4 => '-u'); $configThema = Config::push($args); $analyze = new FindExternalLibraries($this->gremlin, $configThema, Tasks::IS_SUBTASK); $analyze->run(); $this->addSnitch(array('step' => 'External lib', 'project' => $this->config->project)); Config::pop(); unset($analyze); display("Running files\n"); $analyze = new Files($this->gremlin, $this->config, Tasks::IS_SUBTASK); $analyze->run(); unset($analyze); $this->logTime('Files'); $this->addSnitch(array('step' => 'Files', 'project' => $this->config->project)); $this->checkTokenLimit(); $analyze = new Load($this->gremlin, $this->config, Tasks::IS_SUBTASK); $analyze->run(); unset($analyze); display("Project loaded\n"); $this->logTime('Loading'); // paralell running exec($this->config->php . ' ' . $this->config->executable . ' magicnumber -p ' . $this->config->project . ' > /dev/null &'); $this->addSnitch(array('step' => 'Magic Numbers', 'project' => $this->config->project)); // Dump is a child process shell_exec($this->config->php . ' ' . $this->config->executable . ' dump -p ' . $this->config->project); foreach ($this->themes as $theme) { $this->addSnitch(array('step' => 'Analyze : ' . $theme, 'project' => $this->config->project)); $themeForFile = strtolower(str_replace(' ', '_', trim($theme, '"'))); $args = array(1 => 'analyze', 2 => '-p', 3 => $this->config->project, 4 => '-T', 5 => trim($theme, '"'), 6 => '-norefresh', 7 => '-u'); try { $configThema = Config::push($args); $analyze = new Analyze($this->gremlin, $configThema, Tasks::IS_SUBTASK); $analyze->run(); unset($analyze); rename($this->config->projects_root . '/projects/' . $project . '/log/analyze.log', $this->config->projects_root . '/projects/' . $project . '/log/analyze.' . $themeForFile . '.log'); Config::pop(); $args = array(1 => 'dump', 2 => '-p', 3 => $this->config->project, 4 => '-T', 5 => trim($theme, '"'), 6 => '-u'); $configThema = Config::push($args); $dump = new Dump($this->gremlin, $configThema, Tasks::IS_SUBTASK); $dump->run(); unset($dump); Config::pop(); } catch (\Exception $e) { echo "Error while running the Analyze {$theme} \n", $e->getMessage(), "\nTrying next analysis\n"; file_put_contents($this->config->projects_root . '/projects/' . $project . '/log/analyze.' . $themeForFile . '.final.log', $e->getMessage()); } } display("Analyzed project\n"); $this->logTime('Analyze'); $this->addSnitch(array('step' => 'Analyzed', 'project' => $this->config->project)); $this->logTime('Analyze'); $oldConfig = Config::factory(); foreach ($this->reports as $reportName => $formats) { foreach ($formats as $format => $fileName) { display("Reporting {$reportName} in {$format}\n"); $this->addSnitch(array('step' => 'Report : ' . $format, 'project' => $this->config->project)); $args = array(1 => 'report', 2 => '-p', 3 => $this->config->project, 4 => '-file', 5 => $fileName, 6 => '-format', 7 => $format); $this->config = Config::factory($args); try { $report = new Report2($this->gremlin, $this->config, Tasks::IS_SUBTASK); $report->run(); unset($report); } catch (\Exception $e) { echo "Error while building {$reportName} in {$format} \n", $e->getMessage(), "\nTrying next report\n"; } } } Config::factory($oldConfig); display("Reported project\n"); $audit_end = time(); // measure Neo4j's final size $res = shell_exec('du -sh ' . $this->config->neo4j_folder . ' 2>/dev/null'); $neo4jSize = trim(str_replace(basename($this->config->neo4j_folder), '', $res)); $this->datastore->addRow('hash', array('audit_end' => $audit_end, 'audit_length' => $audit_end - $audit_start, 'neo4jSize' => $neo4jSize)); $query = <<<GREMLIN g.V().where( __.sideEffect{x = []; }.in('ANALYZED').sideEffect{ x.add(it.get().value('analyzer')); }.barrier().sideEffect{ y = x.groupBy().findAll{ i,j -> j.size() > 1;};} ) .filter{ y.size() > 0; } .map{ y; }; GREMLIN; $res = $this->gremlin->query($query); if (!empty($res)) { file_put_contents($this->config->projects_root . '/projects/' . $project . '/log/doublons.log', var_export($res, true)); } $this->logTime('Final'); $this->removeSnitch(); display("End\n"); }
private function processDir($dir) { if (!file_exists($dir)) { return array('files' => -1, 'tokens' => -1); } $files = array(); $ignoredFiles = array(); if (substr($dir, -1) === '/') { $dir = substr($dir, 0, -1); } Files::findFiles($dir, $files, $ignoredFiles); $this->atoms = array($this->id0 => $this->atoms[$this->id0]); $this->links = array(); $nbTokens = 0; foreach ($files as $file) { if ($r = $this->processFile($dir . $file)) { $nbTokens += $r; $this->saveFiles(); } } $this->saveDefinitions(); return array('files' => count($files), 'tokens' => $nbTokens); }
private function init_project($project, $repositoryURL) { if (!file_exists($this->config->projects_root . '/projects/' . $project)) { mkdir($this->config->projects_root . '/projects/' . $project, 0755); } else { display($this->config->projects_root . '/projects/' . $project . ' already exists. Reusing' . "\n"); } if (!file_exists($this->config->projects_root . '/projects/' . $project . '/log/')) { mkdir($this->config->projects_root . '/projects/' . $project . '/log/', 0755); } else { display($this->config->projects_root . '/projects/' . $project . '/log/ already exists. Ignoring' . "\n"); return null; } $this->datastore = new Datastore(Config::factory(), Datastore::CREATE); if (!file_exists($this->config->projects_root . '/projects/' . $project . '/config.ini')) { if ($this->config->symlink === true) { $vcs = 'symlink'; } elseif ($this->config->svn === true) { $vcs = 'svn'; } elseif ($this->config->git === true) { $vcs = 'git'; } elseif ($this->config->copy === true) { $vcs = 'copy'; } elseif ($this->config->bzr === true) { $vcs = 'bzr'; } elseif ($this->config->hg === true) { $vcs = 'hg'; } elseif ($this->config->composer === true) { $vcs = 'composer'; } else { $vcs = 'git'; } // default initial config. Found in test project. $configIni = <<<INI phpversion = 7.1 ignore_dirs[] = /test ignore_dirs[] = /tests ignore_dirs[] = /Tests ignore_dirs[] = /Test ignore_dirs[] = /example ignore_dirs[] = /examples ignore_dirs[] = /docs ignore_dirs[] = /doc ignore_dirs[] = /tmp ignore_dirs[] = /version ignore_dirs[] = /vendor ignore_dirs[] = /js ignore_dirs[] = /lang ignore_dirs[] = /data ignore_dirs[] = /css ignore_dirs[] = /cache ignore_dirs[] = /vendor ignore_dirs[] = /assets ignore_dirs[] = /spec ignore_dirs[] = /sql file_extensions = project_name = "{$project}"; project_url = "{$repositoryURL}"; project_vcs = "{$vcs}"; project_description = ""; project_packagist = ""; INI; file_put_contents($this->config->projects_root . '/projects/' . $project . '/config.ini', $configIni); } else { display($this->config->projects_root . '/projects/' . $project . '/config.ini already exists. Ignoring' . "\n"); } shell_exec('chmod -R g+w ' . $this->config->projects_root . '/projects/' . $project); $repositoryDetails = parse_url($repositoryURL); $skipFiles = false; if (!file_exists($this->config->projects_root . '/projects/' . $project . '/code/')) { switch (true) { // Symlink case $this->config->symlink === true: display('Symlink initialization : ' . realpath($repositoryURL)); symlink(realpath($repositoryURL), $this->config->projects_root . '/projects/' . $project . '/code'); break 1; // Empty initialization // Empty initialization case $this->config->copy === true: display('Copy initialization'); $total = copyDir(realpath($repositoryURL), $this->config->projects_root . '/projects/' . $project . '/code'); display($total . ' files were copied'); break 1; // Empty initialization // Empty initialization case $repositoryURL === '' || $repositoryURL === false: display('Empty initialization'); break 1; // composer archive (early in the list, as this won't have 'scheme' // composer archive (early in the list, as this won't have 'scheme' case $this->config->composer === true: display('Initialization with composer'); $res = shell_exec('composer --version'); if (strpos($res, 'Composer') === false) { throw new HelperException('Composer'); } // composer install $composer = new \stdClass(); $composer->require = new \stdClass(); $composer->require->{$repositoryURL} = 'dev-master'; $json = json_encode($composer); mkdir($this->config->projects_root . '/projects/' . $project . '/code', 0755); file_put_contents($this->config->projects_root . '/projects/' . $project . '/code/composer.json', $json); shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '/code; composer -q install'); break 1; // SVN // SVN case isset($repositoryDetails['scheme']) && $repositoryDetails['scheme'] == 'svn' || $this->config->svn === true: $res = shell_exec('svn --version'); if (strpos($res, 'svn') === false) { throw new HelperException('SVN'); } display('SVN initialization'); shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; svn checkout ' . escapeshellarg($repositoryURL) . ' code'); break 1; // Bazaar // Bazaar case $this->config->bzr === true: $res = shell_exec('bzr --version'); if (strpos($res, 'Bazaar') === false) { throw new HelperException('Bazar'); } display('Bazaar initialization'); shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; bzr branch ' . escapeshellarg($repositoryURL) . ' code'); break 1; // HG // HG case $this->config->hg === true: $res = shell_exec('hg --version'); if (strpos($res, 'Mercurial') === false) { throw new HelperException('Mercurial'); } display('Mercurial initialization'); shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; hg clone ' . escapeshellarg($repositoryURL) . ' code'); break 1; // Tbz archive // Tbz archive case $this->config->tbz === true: display('Download the tar.bz2'); $binary = file_get_contents($repositoryURL); display('Saving'); $archiveFile = tempnam(sys_get_temp_dir(), 'archiveTgz') . '.tgz'; file_put_contents($archiveFile, $binary); display('Unarchive'); shell_exec('tar -jxf ' . $archiveFile . ' --directory ' . $this->config->projects_root . '/projects/' . $project . '/code/'); display('Cleanup'); unlink($archiveFile); break 1; // tgz archive // tgz archive case $this->config->tgz === true: display('Download the tar.gz'); $binary = file_get_contents($repositoryURL); display('Saving'); $archiveFile = tempnam(sys_get_temp_dir(), 'archiveTgz') . '.tgz'; file_put_contents($archiveFile, $binary); display('Unarchive'); mkdir($this->config->projects_root . '/projects/' . $project . '/code/'); shell_exec('tar -zxf ' . $archiveFile . ' -C ' . $this->config->projects_root . '/projects/' . $project . '/code/'); display('Cleanup'); unlink($archiveFile); break 1; // zip archive // zip archive case $this->config->zip === true: $res = shell_exec('zip --version'); if (strpos($res, 'Zip') === false) { throw new HelperException('zip'); } display('Download the zip'); $binary = file_get_contents($repositoryURL); display('Saving'); $archiveFile = tempnam(sys_get_temp_dir(), 'archiveZip') . '.zip'; file_put_contents($archiveFile, $binary); display('Unzip'); shell_exec('unzip ' . $archiveFile . ' -d ' . $this->config->projects_root . '/projects/' . $project . '/code/'); display('Cleanup'); unlink($archiveFile); break 1; // Git // Git is last, as it will act as a default // Git // Git is last, as it will act as a default case isset($repositoryDetails['scheme']) && $repositoryDetails['scheme'] == 'git' || $this->config->git === true: $res = shell_exec('git --version'); if (strpos($res, 'git') === false) { throw new HelperException('git'); } display('Git initialization'); $res = shell_exec('cd ' . $this->config->projects_root . '/projects/' . $project . '; git clone -q ' . $repositoryURL . ' code 2>&1 '); if (($offset = strpos($res, 'fatal: ')) !== false) { $this->datastore->addRow('hash', array('init error' => trim(substr($res, $offset + 7)))); display("An error prevented code initialization : " . trim(substr($res, $offset + 7)) . "\nNo code was loaded.\n"); $skipFiles = true; } break 1; default: display('No Initialization'); } } elseif (file_exists($this->config->projects_root . '/projects/' . $project . '/code/')) { display("Code folder is already there. Leaving it intact.\n"); } display("Counting files\n"); $this->datastore->addRow('hash', array('status' => 'Initproject')); if (!$skipFiles) { display("Running files\n"); $analyze = new Files($this->gremlin, $this->config); $analyze->run(); unset($analyze); } }