From 36dd7dfea5b7c42adab172aa9985c0cd479aab42 Mon Sep 17 00:00:00 2001 From: matt-whittom Date: Thu, 25 Jul 2013 15:25:09 -0500 Subject: [PATCH] Cleaned up perforce code Checks to see if already logged in to Perforce, and prompts for password if not and P4PASSWD is not set Checks server url with perforce call Checks for composer.json file, and returns the contents of the file already retrieved. --- .../Downloader/PerforceDownloader.php | 53 +------ .../Repository/Vcs/PerforceDriver.php | 101 +++---------- src/Composer/Util/Perforce.php | 140 ++++++++++++++---- 3 files changed, 138 insertions(+), 156 deletions(-) diff --git a/src/Composer/Downloader/PerforceDownloader.php b/src/Composer/Downloader/PerforceDownloader.php index b82abdc61..c8fcf442f 100644 --- a/src/Composer/Downloader/PerforceDownloader.php +++ b/src/Composer/Downloader/PerforceDownloader.php @@ -14,8 +14,6 @@ namespace Composer\Downloader; use Composer\Package\PackageInterface; use Composer\Util\Perforce; -#use Composer\Util\GitHub; -#use Composer\Util\Git as GitUtil; /** * @author Jordi Boggiano @@ -29,16 +27,11 @@ class PerforceDownloader extends VcsDownloader */ public function doDownload(PackageInterface $package, $path) { - print ("Perforce Downloader:doDownload - path:" . var_export($path, true) . "\n"); - $ref = $package->getSourceReference(); - $p4client = "composer_perforce_dl_" . str_replace("/", "_", str_replace("//", "", $ref)); - $clientSpec = "$path/$p4client.p4.spec"; - print ("PerforceDownloader:doDownload - clientSpec: $clientSpec, targetDir: $path, p4Client: $p4client\n\n"); - $perforce = new Perforce(); - $perforce->writeP4ClientSpec($clientSpec, $path, $p4client, $ref); - $perforce->syncCodeBase($clientSpec, $path, $p4client); + $perforce = new Perforce($ref, $package->getSourceUrl(), $path); + $perforce->writeP4ClientSpec(); + $perforce->syncCodeBase(); } /** @@ -47,26 +40,6 @@ class PerforceDownloader extends VcsDownloader public function doUpdate(PackageInterface $initial, PackageInterface $target, $path) { print("PerforceDownloader:doUpdate\n"); - -// $this->cleanEnv(); -// $path = $this->normalizePath($path); -// -// $ref = $target->getSourceReference(); -// $this->io->write(" Checking out ".$ref); -// $command = 'git remote set-url composer %s && git fetch composer && git fetch --tags composer'; -// -// // capture username/password from URL if there is one -// $this->process->execute('git remote -v', $output, $path); -// if (preg_match('{^(?:composer|origin)\s+https?://(.+):(.+)@([^/]+)}im', $output, $match)) { -// $this->io->setAuthentication($match[3], urldecode($match[1]), urldecode($match[2])); -// } -// -// $commandCallable = function($url) use ($command) { -// return sprintf($command, escapeshellarg($url)); -// }; -// -// $this->runCommand($commandCallable, $target->getSourceUrl(), $path); -// $this->updateToCommit($path, $ref, $target->getPrettyVersion(), $target->getReleaseDate()); } /** @@ -75,18 +48,6 @@ class PerforceDownloader extends VcsDownloader public function getLocalChanges($path) { print("PerforceDownloader:getLocalChanges\n"); -// $this->cleanEnv(); -// $path = $this->normalizePath($path); -// if (!is_dir($path.'/.git')) { -// return; -// } -// -// $command = 'git status --porcelain --untracked-files=no'; -// if (0 !== $this->process->execute($command, $output, $path)) { -// throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); -// } -// -// return trim($output) ?: null; } @@ -96,14 +57,6 @@ class PerforceDownloader extends VcsDownloader protected function getCommitLogs($fromReference, $toReference, $path) { print("PerforceDownloader:getCommitLogs\n"); -// $path = $this->normalizePath($path); -// $command = sprintf('git log %s..%s --pretty=format:"%%h - %%an: %%s"', $fromReference, $toReference); -// -// if (0 !== $this->process->execute($command, $output, $path)) { -// throw new \RuntimeException('Failed to execute ' . $command . "\n\n" . $this->process->getErrorOutput()); -// } -// -// return $output; } } diff --git a/src/Composer/Repository/Vcs/PerforceDriver.php b/src/Composer/Repository/Vcs/PerforceDriver.php index 6ecb47050..7d110c888 100644 --- a/src/Composer/Repository/Vcs/PerforceDriver.php +++ b/src/Composer/Repository/Vcs/PerforceDriver.php @@ -15,62 +15,39 @@ namespace Composer\Repository\Vcs; -#use Composer\Downloader\TransportException; -#use Composer\Json\JsonFile; -#use Composer\Cache; use Composer\IO\IOInterface; use Composer\Util\Filesystem; use Composer\Util\Perforce; -#use Composer\Util\RemoteFilesystem; -#use Composer\Util\GitHub; /** * @author matt-whittom <> */ class PerforceDriver extends VcsDriver { -// protected $cache; -// protected $owner; -// protected $repository; -// protected $tags; -// protected $branches; - protected $rootIdentifier = 'mainline'; - protected $repoDir; -// protected $hasIssues; -// protected $infoCache = array(); -// protected $isPrivate = false; + protected $rootIdentifier; protected $depot; - protected $p4client; + protected $perforce; /** * {@inheritDoc} */ public function initialize() { - print ("PerforceDriver:initialize\n"); + print ("\nPerforceDriver:initialize\n"); + $this->rootIdentifier = "mainline"; $this->depot = $this->repoConfig['depot']; - $this->p4client = "composer_perforce_$this->depot"; - $this->repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; - $clientSpec = $this->config->get('cache-dir') . "/perforce/$this->p4client.p4.spec"; - - $this->p4Login(); - - $fs = new Filesystem(); - $fs->ensureDirectoryExists($this->repoDir); $stream = "//$this->depot/$this->rootIdentifier"; - $perforce = new Perforce(); - $perforce->writeP4ClientSpec($clientSpec, $this->repoDir, $this->p4client, $stream); - $perforce->syncCodeBase($clientSpec, $this->repoDir, $this->p4client); + $repoDir = $this->config->get('cache-vcs-dir') . "/$this->depot"; + $this->perforce = new Perforce($stream, $this->getUrl(), $repoDir); + + $this->perforce->p4Login($this->io); + $this->perforce->writeP4ClientSpec(); + $this->perforce->syncCodeBase(); return true; } - protected function p4Login(){ - $password = trim(shell_exec('echo $P4PASSWD')); - $command = "echo $password | p4 login -a "; - shell_exec($command); - } @@ -79,31 +56,9 @@ class PerforceDriver extends VcsDriver */ public function getComposerInformation($identifier) { - print ("PerforceDriver:getComposerInformation: $identifier\n"); - $command = "p4 print $identifier/composer.json"; - $result = shell_exec($command); - $index = strpos($result, "{"); - if ($index === false){ - return; - } - if ($index >=0){ - $rawData = substr($result, $index); - $composer_info = json_decode($rawData, true); - print ("ComposerInfo is:".var_export($composer_info, true) . "\n"); - return $composer_info; - } - - -// Basically, read the composer.json file from the project. -// -// Git stuff: -// ..getComposerInfo is: array ( -// 'support' => -// array ( -// 'source' => 'http://github.com/composer/packagist', -// ), -// 'time' => '2012-09-10', -// ) + print ("PerforceDriver:getComposerInformation - identifier: $identifier\n"); + $composer_info =$this->perforce->getComposerInformation($identifier); + return $composer_info; } /** @@ -120,9 +75,6 @@ class PerforceDriver extends VcsDriver */ public function getBranches() { - //return $branch->$identifier - //getComposer($identifier) - //validate($branch) print ("PerforceDriver:getBranches\n"); $command = "p4 streams //$this->depot/..."; $result = shell_exec($command); @@ -137,7 +89,6 @@ class PerforceDriver extends VcsDriver } } $branches['master'] = $branches['mainline']; - print ("PerforceDriver:getBranches - returning branches:".var_export($branches, true)."\n"); return $branches; } @@ -155,7 +106,7 @@ class PerforceDriver extends VcsDriver */ public function getDist($identifier) { - print ("PerforceDriver:getDist: $identifier\n"); + print("\nPerforceDriver:getDist: identifier: $identifier\n"); return null; } @@ -164,7 +115,7 @@ class PerforceDriver extends VcsDriver */ public function getSource($identifier) { - print ("PerforceDriver:getSource: $identifier\n"); + print ("\nPerforceDriver:getSource - identifier: $identifier\n"); $source = array ( 'type' => 'perforce', @@ -180,7 +131,7 @@ class PerforceDriver extends VcsDriver public function getUrl() { print ("PerforceDriver:getUrl\n"); - + return $this->url; } /** @@ -188,10 +139,10 @@ class PerforceDriver extends VcsDriver */ public function hasComposerFile($identifier) { - print ("PerforceDriver:hasComposerFile: $identifier\n"); - - //Does the project have a composer file? - return true; + print ("\nPerforceDriver:hasComposerFile - identifier: $identifier\n"); + $composerFile = $this->perforce->getComposerFilePath($identifier); + print ("returning: " . var_export(file_exists($composerFile),true) . "\n"); + return file_exists($composerFile); } /** @@ -199,7 +150,8 @@ class PerforceDriver extends VcsDriver */ public function getContents($url) { - print("PerforceDriver:getContents - url: $url"); + print ("\nPerforceDriver:getContents - url: $url\n"); + return false; } /** @@ -207,12 +159,7 @@ class PerforceDriver extends VcsDriver */ public static function supports(IOInterface $io, $url, $deep = false) { - print ("PerforceDriver:supports\n"); - - print ("\nChecking url for support: $url\n\n"); - if (preg_match('#(^perforce)#', $url)) { - return true; - } - return false; + print ("PerforceDriver:supports - url: $url\n"); + return Perforce::checkServerExists($url); } } diff --git a/src/Composer/Util/Perforce.php b/src/Composer/Util/Perforce.php index 15f4b6ac5..dfd87b724 100644 --- a/src/Composer/Util/Perforce.php +++ b/src/Composer/Util/Perforce.php @@ -9,56 +9,93 @@ namespace Composer\Util; +use Composer\IO\IOInterface; + class Perforce { - public function syncCodeBase($clientSpec, $targetDir, $p4client){ - $p4CreateClientCommand = "p4 client -i < $clientSpec"; - print ("\nPerforceDriver create client: $p4CreateClientCommand\n"); + + protected $path; + protected $p4client; + protected $p4user; + protected $p4port; + protected $p4stream; + protected $p4clientSpec; + + final public function __construct($stream, $port, $path){ + $this->p4stream = $stream; + $this->p4port = $port; + $this->path = $path; + $fs = new Filesystem(); + $fs->ensureDirectoryExists($path); + } + + protected function getClient() + { + if (!isset($this->p4client)){ + $random_value = mt_rand(1000,9999); + $this->p4client = "composer_perforce_" . $random_value . "_".str_replace("/", "_", str_replace("//", "", $this->p4stream)); + } + return $this->p4client; + } + + protected function getUser() + { + if (!isset($this->p4user)){ + $this->p4user = trim(shell_exec('echo $P4USER')); + } + return $this->p4user; + } + + protected function getPath() + { + return $this->path; + } + protected function getPort() + { + return $this->p4port; + } + + protected function getStream() + { + return $this->p4stream; + } + protected function getP4ClientSpec() + { + $p4clientSpec = $this->path . "/" . $this->getClient() . ".p4.spec"; + return $p4clientSpec; + } + + public function syncCodeBase(){ + $p4CreateClientCommand = $this->generateP4Command( "client -i < " . $this->getP4ClientSpec()); $result = shell_exec($p4CreateClientCommand); - print ("result: $result\n\n"); $prevDir = getcwd(); - chdir($targetDir); + chdir($this->path); - //write p4 config file - $p4ConfigFileSpec = "$targetDir/p4config.config"; - $p4ConfigFile = fopen($p4ConfigFileSpec, 'w'); - fwrite($p4ConfigFile, "P4CLIENT=$p4client"); - fclose($p4ConfigFile); + $result = shell_exec("pwd"); - $testCommand = "pwd"; - print ("PerforceDriver test dir command: $testCommand\n"); - $result = shell_exec($testCommand); - print ("result: $result\n\n"); - - $p4SyncCommand = "p4 sync -f //$p4client/..."; - print ("PerforceDriver sync client: $p4SyncCommand\n"); + $p4SyncCommand = $this->generateP4Command( "sync -f //".$this->getClient()."/..."); $result = shell_exec($p4SyncCommand); - print ("result: $result\n\n"); chdir($prevDir); } - public function writeP4ClientSpec($clientSpec, $targetDir, $p4client, $stream){ - $fs = new Filesystem(); - $fs->ensureDirectoryExists(dirname($clientSpec)); + public function writeP4ClientSpec(){ - $p4user = trim(shell_exec('echo $P4USER')); - print ("PerforceDriver: writing to client spec: $clientSpec\n\n"); - $spec = fopen($clientSpec, 'w'); + $spec = fopen($this->getP4ClientSpec(), 'w'); try { - fwrite($spec, "Client: $p4client\n\n"); + fwrite($spec, "Client: " . $this->getClient() . "\n\n"); fwrite($spec, "Update: " . date("Y/m/d H:i:s") . "\n\n"); fwrite($spec, "Access: " . date("Y/m/d H:i:s") . "\n" ); - fwrite($spec, "Owner: $p4user\n\n" ); + fwrite($spec, "Owner: " . $this->getUser() . "\n\n" ); fwrite($spec, "Description:\n" ); - fwrite($spec, " Created by $p4user from composer.\n\n" ); - fwrite($spec, "Root: $targetDir\n\n" ); + fwrite($spec, " Created by " . $this->getUser() . " from composer.\n\n" ); + fwrite($spec, "Root: " .$this->getPath(). "\n\n" ); fwrite($spec, "Options: noallwrite noclobber nocompress unlocked modtime rmdir\n\n" ); fwrite($spec, "SubmitOptions: revertunchanged\n\n" ); fwrite($spec, "LineEnd: local\n\n" ); fwrite($spec, "Stream:\n" ); - fwrite($spec, " $stream\n" ); + fwrite($spec, " " . $this->getStream()."\n" ); } catch(Exception $e){ fclose($spec); throw $e; @@ -66,5 +103,50 @@ class Perforce { fclose($spec); } + public function getComposerFilePath($identifier) + { + $composerFilePath = $this->path . "/composer.json" ; + print ("\nPerforceUtility - getComposerPath: $composerFilePath\n\n"); + return $composerFilePath; + } + protected function generateP4Command($command) { + $p4Command = "p4 "; + $p4Command = $p4Command . "-u " . $this->getUser() . " "; + $p4Command = $p4Command . "-c " . $this->getClient() . " "; + $p4Command = $p4Command . "-p " . $this->getPort() . " "; + $p4Command = $p4Command . $command; + return $p4Command; + } + public function p4Login(IOInterface $io){ + $user = $this->getUser(); + $result = trim(shell_exec("p4 login -s")); + $index = strpos($result, $user); + if ($index === false){ + $password = trim(shell_exec('echo $P4PASSWD')); + if (strlen($password) <= 0){ + $password = $io->ask("Enter password for Perforce user " . $this->getUser() . ": " ); + } + $command = "echo $password | p4 login -a "; + shell_exec($command); + } + } + + public static function checkServerExists($url) + { + $result = shell_exec("p4 -p $url info -s"); + $index = strpos($result, "error"); + if ($index === false){ + return true; + } + return false; + } + + public function getComposerInformation($identifier) + { + $composerFilePath =$this->getComposerFilePath($identifier); + $contents = file_get_contents($composerFilePath); + $composer_info = json_decode($contents, true); + return $composer_info; + } } \ No newline at end of file