/**
  * Gets the validity information for the cache.
  *
  * If $cleanCache is true and the cache is not valid, it will be wiped.
  */
 public function getValidity($cleanCache)
 {
     // Things that could make the cache invalid:
     // - changing the version of PieCrust
     // - changing the configuration
     $hashArray = $this->pieCrust->getConfig()->get();
     $hashArray['_version'] = PieCrustDefaults::VERSION;
     $hashString = json_encode($hashArray);
     $hash = hash('sha1', $hashString);
     $isCacheValid = false;
     $cacheInfoFileName = $this->pieCrust->getCacheDir() . PieCrustDefaults::CACHE_INFO_FILENAME;
     if (file_exists($cacheInfoFileName)) {
         $previousHash = file_get_contents($cacheInfoFileName);
         $isCacheValid = $previousHash == $hash;
     }
     $cacheValidity = array('is_valid' => $isCacheValid, 'path' => $cacheInfoFileName, 'hash' => $hash, 'was_cleaned' => false);
     if ($cleanCache && !$isCacheValid) {
         // Clean the cache!
         PathHelper::deleteDirectoryContents($this->pieCrust->getCacheDir(), $this->cacheCleaningSkipPatterns);
         file_put_contents($cacheInfoFileName, $hash);
         $cacheValidity['is_valid'] = true;
         $cacheValidity['was_cleaned'] = true;
     }
     return $cacheValidity;
 }
Beispiel #2
0
function ensure_cache($cacheDir, $ensureClean = true)
{
    if ($cacheDir == null or $cacheDir == '') {
        throw new Exception('Need a valid cache directory.');
    }
    if ($ensureClean and is_dir($cacheDir)) {
        PathHelper::deleteDirectoryContents($cacheDir);
    }
    PathHelper::ensureDirectory($cacheDir);
}
 public function run(ChefContext $context)
 {
     $cacheDir = $context->getApp()->getCacheDir();
     if (!$cacheDir) {
         throw new PieCrustException("The website seems to have caching disabled.");
     }
     if (!is_dir($cacheDir)) {
         throw new PieCrustException("The cache directory doesn't exist: {$cacheDir}");
     }
     $context->getLog()->info("Purging cache: {$cacheDir}");
     PathHelper::deleteDirectoryContents($cacheDir);
 }
 protected function copySubDirectory($context, $destination, $subDir, $cleanFirst = false)
 {
     $log = $context->getLog();
     if ($cleanFirst) {
         if (is_dir($destination)) {
             $log->info("Deleting existing files...");
             PathHelper::deleteDirectoryContents($destination);
         }
     }
     $log->info("Copying files...");
     $log->debug("{$subDir} -> {$destination}");
     PathHelper::copyDirectory($subDir, $destination, ",\\.hg|\\.git|\\.svn,");
 }
Beispiel #5
0
 public static function tearDownAfterClass()
 {
     $mockDir = PIECRUST_UNITTESTS_MOCK_DIR;
     if (is_dir($mockDir)) {
         // On Windows, it looks like the file-system is a bit "slow".
         // And by "slow", I mean "retarded".
         $tries = 3;
         while ($tries > 0) {
             try {
                 PathHelper::deleteDirectoryContents($mockDir);
                 rmdir($mockDir);
                 $tries = 0;
             } catch (\Exception $e) {
                 $tries--;
             }
         }
     }
 }
 protected function cleanCacheIfNeeded(array $cacheValidity)
 {
     $cleanCache = $this->parameters['clean_cache'];
     $cleanCacheReason = "ordered to";
     if (!$cleanCache) {
         if (!$cacheValidity['is_valid']) {
             $cleanCache = true;
             $cleanCacheReason = "not valid anymore";
         }
     }
     if (!$cleanCache) {
         if ($this->bakeRecord->shouldDoFullBake()) {
             $cleanCache = true;
             $cleanCacheReason = "need bake info regen";
         }
     }
     // If any template file changed since last time, we also need to re-bake everything
     // (there's no way to know what weird conditional template inheritance/inclusion
     //  could be in use...).
     if (!$cleanCache) {
         $maxMTime = 0;
         foreach ($this->pieCrust->getTemplatesDirs() as $dir) {
             $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST);
             foreach ($iterator as $path) {
                 if ($path->isFile()) {
                     $maxMTime = max($maxMTime, $path->getMTime());
                 }
             }
         }
         if ($maxMTime >= $this->bakeRecord->getLast('time')) {
             $cleanCache = true;
             $cleanCacheReason = "templates modified";
         }
     }
     if ($cleanCache) {
         $start = microtime(true);
         PathHelper::deleteDirectoryContents($this->pieCrust->getCacheDir());
         file_put_contents($cacheValidity['path'], $cacheValidity['hash']);
         $this->logger->info(self::formatTimed($start, 'cleaned cache (reason: ' . $cleanCacheReason . ')'));
         $this->parameters['smart'] = false;
     }
 }
Beispiel #7
0
 protected function updatePlugins(ChefContext $context)
 {
     $app = $context->getApp();
     $log = $context->getLog();
     $result = $context->getResult();
     $pluginName = $result->command->command->args['name'];
     // Right now we do it the brute force way: update everything.
     // TODO: keep some metadata on the installed version so we don't overwrite with the exact same.
     $pluginLoader = $app->getPluginLoader();
     foreach ($pluginLoader->getPlugins() as $plugin) {
         $curName = $plugin->getName();
         if ($curName != '__builtin__' && ($curName == $pluginName || !$pluginName)) {
             $log->info("Updating {$curName}...");
             // First, rename the existing directory.
             $pluginMeta = $pluginLoader->getPluginMeta($curName);
             $pluginDir = $pluginMeta->directory;
             $pluginDirBackup = $pluginDir . '__backup';
             if (!rename($pluginDir, $pluginDirBackup)) {
                 throw new PieCrustException("Can't rename plugin directory: {$pluginDir}");
             }
             // Then, update.
             try {
                 $plugin = $this->installPlugin($curName, $context);
             } catch (\Exception $e) {
                 $log->debug("Error encountered, restoring backup directory.");
                 rename($pluginDirBackup, $pluginDir);
                 throw new PieCrustException("Error updating plugin '{$curName}'.", 0, $e);
             }
             // Last, cleanup backup directory.
             $log->debug("Cleaning up backup directory: {$pluginDirBackup}");
             PathHelper::deleteDirectoryContents($pluginDirBackup);
             rmdir($pluginDirBackup);
         }
     }
 }
 protected function extractRepository($context, $destination, $userName, $repoSlug, $rev = 'default')
 {
     $app = $context->getApp();
     $log = $context->getLog();
     $cacheDir = $app->getCacheDir();
     if (!$cacheDir) {
         // If the cache doesn't exist or the application is running
         // with caching disabled, we still need to create a cache directory
         // to download the archive somewhere.
         $cacheDir = $app->getRootDir() . PieCrustDefaults::CACHE_DIR;
         PathHelper::ensureDirectory($cacheDir, true);
     }
     $url = "https://bitbucket.org/{$userName}/{$repoSlug}/get/{$rev}.zip";
     $log->info("Downloading archive...");
     $log->debug("Fetching '{$url}'...");
     $contents = file_get_contents($url);
     $tempZip = $cacheDir . $repoSlug . '.zip';
     file_put_contents($tempZip, $contents);
     $log->info("Extracting...");
     $log->debug("Unzipping into: {$cacheDir}");
     ArchiveHelper::unzip($tempZip, $cacheDir, $log);
     $globbed = glob($cacheDir . $userName . '-' . $repoSlug . '-*', GLOB_ONLYDIR | GLOB_MARK);
     if (count($globbed) != 1) {
         throw new PieCrustException("Can't find extracted directory for downloaded archive!");
     }
     $archiveDir = $globbed[0];
     if (is_dir($destination)) {
         $log->debug("Cleaning destination: {$destination}");
         PathHelper::deleteDirectoryContents($destination);
     }
     if (!is_dir(dirname($destination))) {
         mkdir(dirname($destination));
     }
     $log->debug("Moving extracted files into: {$destination}");
     rename($archiveDir, $destination);
     $log->debug("Cleaning up...");
     unlink($tempZip);
 }