diff options
author | Bernhard Posselt <Raydiation@users.noreply.github.com> | 2014-12-15 12:34:46 +0100 |
---|---|---|
committer | Bernhard Posselt <Raydiation@users.noreply.github.com> | 2014-12-15 12:34:46 +0100 |
commit | be45366013c50d9a9d18e258c66e18798719d8df (patch) | |
tree | f9e897521e0e93e1f96755ae60752a9ea7c0bcda /lib | |
parent | b831914aa66ab9591dd4593233cc8f67ed6cf8eb (diff) | |
parent | e58b9d3eb70996a6ccccabf979125fc0797fbd85 (diff) | |
download | nextcloud-server-be45366013c50d9a9d18e258c66e18798719d8df.tar.gz nextcloud-server-be45366013c50d9a9d18e258c66e18798719d8df.zip |
Merge pull request #12625 from owncloud/app-dependencies-libs-and-commands
adding dependencies for command line tools and php libraries
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/app.php | 27 | ||||
-rw-r--r-- | lib/private/app/dependencyanalyzer.php | 210 | ||||
-rw-r--r-- | lib/private/app/platform.php | 47 | ||||
-rw-r--r-- | lib/private/app/platformrepository.php | 210 |
4 files changed, 447 insertions, 47 deletions
diff --git a/lib/private/app.php b/lib/private/app.php index 86db8fd9f55..ecdc8ca8320 100644 --- a/lib/private/app.php +++ b/lib/private/app.php @@ -25,6 +25,8 @@ * License along with this library. If not, see <http://www.gnu.org/licenses/>. * */ +use OC\App\DependencyAnalyzer; +use OC\App\Platform; /** * This class manages the apps. It allows them to register and integrate in the @@ -1082,6 +1084,7 @@ class OC_App { */ public static function installApp($app) { $l = \OC::$server->getL10N('core'); + $config = \OC::$server->getConfig(); $appData=OC_OCSClient::getApplication($app); // check if app is a shipped app or not. OCS apps have an integer as id, shipped apps use a string @@ -1106,13 +1109,25 @@ class OC_App { array($info['name']) ) ); - }else{ - OC_Appconfig::setValue( $app, 'enabled', 'yes' ); - if(isset($appData['id'])) { - OC_Appconfig::setValue( $app, 'ocsid', $appData['id'] ); - } - \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); } + + // check for required dependencies + $dependencyAnalyzer = new DependencyAnalyzer(new Platform($config), $l); + $missing = $dependencyAnalyzer->analyze($app); + if(!empty($missing)) { + $missingMsg = join(PHP_EOL, $missing); + throw new \Exception( + $l->t('App \"%s\" cannot be installed because the following dependencies are not fulfilled: %s', + array($info['name'], $missingMsg) + ) + ); + } + + $config->setAppValue($app, 'enabled', 'yes'); + if(isset($appData['id'])) { + $config->setAppValue($app, 'ocsid', $appData['id'] ); + } + \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); }else{ throw new \Exception($l->t("No app name specified")); } diff --git a/lib/private/app/dependencyanalyzer.php b/lib/private/app/dependencyanalyzer.php index fb4b3761656..ae40e8523fc 100644 --- a/lib/private/app/dependencyanalyzer.php +++ b/lib/private/app/dependencyanalyzer.php @@ -1,5 +1,5 @@ <?php - /** +/** * @author Thomas Müller * @copyright 2014 Thomas Müller deepdiver@owncloud.com * @@ -10,78 +10,206 @@ namespace OC\App; +use OCP\IL10N; + class DependencyAnalyzer { /** @var Platform */ - private $system; + private $platform; /** @var \OCP\IL10N */ private $l; - /** @var array */ - private $missing; - - /** @var array */ - private $dependencies; - /** - * @param array $app * @param Platform $platform * @param \OCP\IL10N $l */ - function __construct(array $app, $platform, $l) { - $this->system = $platform; + function __construct(Platform $platform, IL10N $l) { + $this->platform = $platform; $this->l = $l; - $this->missing = array(); - $this->dependencies = array(); - if (array_key_exists('dependencies', $app)) { - $this->dependencies = $app['dependencies']; - } } /** * @param array $app * @returns array of missing dependencies */ - public function analyze() { - $this->analysePhpVersion(); - $this->analyseSupportedDatabases(); - return $this->missing; + public function analyze($app) { + $this->appInfo = $app; + if (isset($app['dependencies'])) { + $dependencies = $app['dependencies']; + } else { + $dependencies = []; + } + + return array_merge( + $this->analyzePhpVersion($dependencies), + $this->analyzeDatabases($dependencies), + $this->analyzeCommands($dependencies), + $this->analyzeLibraries($dependencies), + $this->analyzeOS($dependencies), + $this->analyzeOC($dependencies, $app)); } - private function analysePhpVersion() { - if (isset($this->dependencies['php']['@attributes']['min-version'])) { - $minVersion = $this->dependencies['php']['@attributes']['min-version']; - if (version_compare($this->system->getPhpVersion(), $minVersion, '<')) { - $this->missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion); + private function analyzePhpVersion($dependencies) { + $missing = []; + if (isset($dependencies['php']['@attributes']['min-version'])) { + $minVersion = $dependencies['php']['@attributes']['min-version']; + if (version_compare($this->platform->getPhpVersion(), $minVersion, '<')) { + $missing[] = (string)$this->l->t('PHP %s or higher is required.', $minVersion); } } - if (isset($this->dependencies['php']['@attributes']['max-version'])) { - $maxVersion = $this->dependencies['php']['@attributes']['max-version']; - if (version_compare($this->system->getPhpVersion(), $maxVersion, '>')) { - $this->missing[] = (string)$this->l->t('PHP with a version less then %s is required.', $maxVersion); + if (isset($dependencies['php']['@attributes']['max-version'])) { + $maxVersion = $dependencies['php']['@attributes']['max-version']; + if (version_compare($this->platform->getPhpVersion(), $maxVersion, '>')) { + $missing[] = (string)$this->l->t('PHP with a version lower than %s is required.', $maxVersion); } } + return $missing; } - private function analyseSupportedDatabases() { - if (!isset($this->dependencies['database'])) { - return; + private function analyzeDatabases($dependencies) { + $missing = []; + if (!isset($dependencies['database'])) { + return $missing; } - $supportedDatabases = $this->dependencies['database']; + $supportedDatabases = $dependencies['database']; if (empty($supportedDatabases)) { - return; + return $missing; } - $supportedDatabases = array_map(function($db) { - if (isset($db['@value'])) { - return $db['@value']; - } - return $db; + if (!is_array($supportedDatabases)) { + $supportedDatabases = array($supportedDatabases); + } + $supportedDatabases = array_map(function ($db) { + return $this->getValue($db); }, $supportedDatabases); - $currentDatabase = $this->system->getDatabase(); + $currentDatabase = $this->platform->getDatabase(); if (!in_array($currentDatabase, $supportedDatabases)) { - $this->missing[] = (string)$this->l->t('Following databases are supported: %s', join(', ', $supportedDatabases)); + $missing[] = (string)$this->l->t('Following databases are supported: %s', join(', ', $supportedDatabases)); + } + return $missing; + } + + private function analyzeCommands($dependencies) { + $missing = []; + if (!isset($dependencies['command'])) { + return $missing; + } + + $commands = $dependencies['command']; + if (!is_array($commands)) { + $commands = array($commands); + } + $os = $this->platform->getOS(); + foreach ($commands as $command) { + if (isset($command['@attributes']['os']) && $command['@attributes']['os'] !== $os) { + continue; + } + $commandName = $this->getValue($command); + if (!$this->platform->isCommandKnown($commandName)) { + $missing[] = (string)$this->l->t('The command line tool %s could not be found', $commandName); + } + } + return $missing; + } + + private function analyzeLibraries($dependencies) { + $missing = []; + if (!isset($dependencies['lib'])) { + return $missing; + } + + $libs = $dependencies['lib']; + if (!is_array($libs)) { + $libs = array($libs); + } + foreach ($libs as $lib) { + $libName = $this->getValue($lib); + $libVersion = $this->platform->getLibraryVersion($libName); + if (is_null($libVersion)) { + $missing[] = (string)$this->l->t('The library %s is not available.', $libName); + continue; + } + + if (is_array($lib)) { + if (isset($lib['@attributes']['min-version'])) { + $minVersion = $lib['@attributes']['min-version']; + if (version_compare($libVersion, $minVersion, '<')) { + $missing[] = (string)$this->l->t('Library %s with a version higher than %s is required - available version %s.', + array($libName, $minVersion, $libVersion)); + } + } + if (isset($lib['@attributes']['max-version'])) { + $maxVersion = $lib['@attributes']['max-version']; + if (version_compare($libVersion, $maxVersion, '>')) { + $missing[] = (string)$this->l->t('Library %s with a version lower than %s is required - available version %s.', + array($libName, $maxVersion, $libVersion)); + } + } + } + } + return $missing; + } + + private function analyzeOS($dependencies) { + $missing = []; + if (!isset($dependencies['os'])) { + return $missing; } + + $oss = $dependencies['os']; + if (empty($oss)) { + return $missing; + } + if (is_array($oss)) { + $oss = array_map(function ($os) { + return $this->getValue($os); + }, $oss); + } else { + $oss = array($oss); + } + $currentOS = $this->platform->getOS(); + if (!in_array($currentOS, $oss)) { + $missing[] = (string)$this->l->t('Following platforms are supported: %s', join(', ', $oss)); + } + return $missing; + } + + private function analyzeOC($dependencies, $appInfo) { + $missing = []; + $minVersion = null; + if (isset($dependencies['owncloud']['@attributes']['min-version'])) { + $minVersion = $dependencies['owncloud']['@attributes']['min-version']; + } elseif (isset($appInfo['requiremin'])) { + $minVersion = $appInfo['requiremin']; + } + $maxVersion = null; + if (isset($dependencies['owncloud']['@attributes']['max-version'])) { + $maxVersion = $dependencies['owncloud']['@attributes']['max-version']; + } elseif (isset($appInfo['requiremax'])) { + $maxVersion = $appInfo['requiremax']; + } + + if (!is_null($minVersion)) { + if (version_compare($this->platform->getOcVersion(), $minVersion, '<')) { + $missing[] = (string)$this->l->t('ownCloud %s or higher is required.', $minVersion); + } + } + if (!is_null($maxVersion)) { + if (version_compare($this->platform->getOcVersion(), $maxVersion, '>')) { + $missing[] = (string)$this->l->t('ownCloud with a version lower than %s is required.', $maxVersion); + } + } + return $missing; + } + + /** + * @param $element + * @return mixed + */ + private function getValue($element) { + if (isset($element['@value'])) + return $element['@value']; + return (string)$element; } } diff --git a/lib/private/app/platform.php b/lib/private/app/platform.php index 39f8a2979f9..578f541e5d2 100644 --- a/lib/private/app/platform.php +++ b/lib/private/app/platform.php @@ -10,18 +10,43 @@ namespace OC\App; +use OC_Util; use OCP\IConfig; +/** + * Class Platform + * + * This class basically abstracts any kind of information which can be retrieved from the underlying system. + * + * @package OC\App + */ class Platform { + /** + * @param IConfig $config + */ function __construct(IConfig $config) { $this->config = $config; } + /** + * @return string + */ public function getPhpVersion() { return phpversion(); } + /** + * @return string + */ + public function getOcVersion() { + $v = OC_Util::getVersion(); + return join('.', $v); + } + + /** + * @return string + */ public function getDatabase() { $dbType = $this->config->getSystemValue('dbtype', 'sqlite'); if ($dbType === 'sqlite3') { @@ -30,4 +55,26 @@ class Platform { return $dbType; } + + /** + * @return string + */ + public function getOS() { + return php_uname('s'); + } + + /** + * @param $command + * @return bool + */ + public function isCommandKnown($command) { + $path = \OC_Helper::findBinaryPath($command); + return ($path !== null); + } + + public function getLibraryVersion($name) { + $repo = new PlatformRepository(); + $lib = $repo->findLibrary($name); + return $lib; + } } diff --git a/lib/private/app/platformrepository.php b/lib/private/app/platformrepository.php new file mode 100644 index 00000000000..96d04ec2e42 --- /dev/null +++ b/lib/private/app/platformrepository.php @@ -0,0 +1,210 @@ +<?php + +namespace OC\App; + +/** + * Class PlatformRepository + * + * Inspired by the composer project - licensed under MIT + * https://github.com/composer/composer/blob/master/src/Composer/Repository/PlatformRepository.php#L82 + * + * @package OC\App + */ +class PlatformRepository { + public function __construct() { + $this->packages = $this->initialize(); + } + + protected function initialize() { + $loadedExtensions = get_loaded_extensions(); + $packages = array(); + + // Extensions scanning + foreach ($loadedExtensions as $name) { + if (in_array($name, array('standard', 'Core'))) { + continue; + } + + $ext = new \ReflectionExtension($name); + try { + $prettyVersion = $ext->getVersion(); + } catch (\UnexpectedValueException $e) { + $prettyVersion = '0'; + } + try { + $prettyVersion = $this->normalizeVersion($prettyVersion); + } catch (\UnexpectedValueException $e) { + continue; + } + + $packages[$this->buildPackageName($name)] = $prettyVersion; + } + + foreach ($loadedExtensions as $name) { + $prettyVersion = null; + switch ($name) { + case 'curl': + $curlVersion = curl_version(); + $prettyVersion = $curlVersion['version']; + break; + + case 'iconv': + $prettyVersion = ICONV_VERSION; + break; + + case 'intl': + $name = 'ICU'; + if (defined('INTL_ICU_VERSION')) { + $prettyVersion = INTL_ICU_VERSION; + } else { + $reflector = new \ReflectionExtension('intl'); + + ob_start(); + $reflector->info(); + $output = ob_get_clean(); + + preg_match('/^ICU version => (.*)$/m', $output, $matches); + $prettyVersion = $matches[1]; + } + + break; + + case 'libxml': + $prettyVersion = LIBXML_DOTTED_VERSION; + break; + + case 'openssl': + $prettyVersion = preg_replace_callback('{^(?:OpenSSL\s*)?([0-9.]+)([a-z]?).*}', function ($match) { + return $match[1] . (empty($match[2]) ? '' : '.' . (ord($match[2]) - 96)); + }, OPENSSL_VERSION_TEXT); + break; + + case 'pcre': + $prettyVersion = preg_replace('{^(\S+).*}', '$1', PCRE_VERSION); + break; + + case 'uuid': + $prettyVersion = phpversion('uuid'); + break; + + case 'xsl': + $prettyVersion = LIBXSLT_DOTTED_VERSION; + break; + + default: + // None handled extensions have no special cases, skip + continue 2; + } + + try { + $prettyVersion = $this->normalizeVersion($prettyVersion); + } catch (\UnexpectedValueException $e) { + continue; + } + + $packages[$this->buildPackageName($name)] = $prettyVersion; + } + + return $packages; + } + + private function buildPackageName($name) { + return str_replace(' ', '-', $name); + } + + /** + * @param $name + * @return string + */ + public function findLibrary($name) { + $extName = $this->buildPackageName($name); + if (isset($this->packages[$extName])) { + return $this->packages[$extName]; + } + return null; + } + + private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?'; + + /** + * Normalizes a version string to be able to perform comparisons on it + * + * https://github.com/composer/composer/blob/master/src/Composer/Package/Version/VersionParser.php#L94 + * + * @param string $version + * @param string $fullVersion optional complete version string to give more context + * @throws \UnexpectedValueException + * @return string + */ + public function normalizeVersion($version, $fullVersion = null) { + $version = trim($version); + if (null === $fullVersion) { + $fullVersion = $version; + } + // ignore aliases and just assume the alias is required instead of the source + if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) { + $version = $match[1]; + } + // match master-like branches + if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) { + return '9999999-dev'; + } + if ('dev-' === strtolower(substr($version, 0, 4))) { + return 'dev-' . substr($version, 4); + } + // match classical versioning + if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) { + $version = $matches[1] + . (!empty($matches[2]) ? $matches[2] : '.0') + . (!empty($matches[3]) ? $matches[3] : '.0') + . (!empty($matches[4]) ? $matches[4] : '.0'); + $index = 5; + } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)' . self::$modifierRegex . '$}i', $version, $matches)) { // match date-based versioning + $version = preg_replace('{\D}', '-', $matches[1]); + $index = 2; + } elseif (preg_match('{^v?(\d{4,})(\.\d+)?(\.\d+)?(\.\d+)?' . self::$modifierRegex . '$}i', $version, $matches)) { + $version = $matches[1] + . (!empty($matches[2]) ? $matches[2] : '.0') + . (!empty($matches[3]) ? $matches[3] : '.0') + . (!empty($matches[4]) ? $matches[4] : '.0'); + $index = 5; + } + // add version modifiers if a version was matched + if (isset($index)) { + if (!empty($matches[$index])) { + if ('stable' === $matches[$index]) { + return $version; + } + $version .= '-' . $this->expandStability($matches[$index]) . (!empty($matches[$index + 1]) ? $matches[$index + 1] : ''); + } + if (!empty($matches[$index + 2])) { + $version .= '-dev'; + } + return $version; + } + $extraMessage = ''; + if (preg_match('{ +as +' . preg_quote($version) . '$}', $fullVersion)) { + $extraMessage = ' in "' . $fullVersion . '", the alias must be an exact version'; + } elseif (preg_match('{^' . preg_quote($version) . ' +as +}', $fullVersion)) { + $extraMessage = ' in "' . $fullVersion . '", the alias source must be an exact version, if it is a branch name you should prefix it with dev-'; + } + throw new \UnexpectedValueException('Invalid version string "' . $version . '"' . $extraMessage); + } + + private function expandStability($stability) { + $stability = strtolower($stability); + switch ($stability) { + case 'a': + return 'alpha'; + case 'b': + return 'beta'; + case 'p': + case 'pl': + return 'patch'; + case 'rc': + return 'RC'; + default: + return $stability; + } + } +} |