def _load_package(self, pkg_loader, name): try: parts = name.rsplit('/') if len(parts) == 2: name, pkg_version = parts version_spec = helpers.parse_version_spec(pkg_version) else: version_spec = helpers.parse_version_spec('*') package = pkg_loader.load_package(name, version_spec) except exceptions.NoPackageFound: if not CONF.engine.load_packages_from: msg = _('Local package is not found since "load-packages-from"' ' engine parameter is not provided and specified ' 'packages is not loaded to murano-api') else: msg = _('Specified package is not found: {0} were scanned ' 'together with murano database').format(','.join( CONF.engine.load_packages_from)) LOG.error(msg) self.error(msg, show_help=False) except exc.CommunicationError: msg = ('Murano API is not running. ' 'Check configuration parameters.') LOG.error(msg) self.error(msg, show_help=False) return package
def _load_package(self, pkg_loader, name): try: parts = name.rsplit('/') if len(parts) == 2: name, pkg_version = parts version_spec = helpers.parse_version_spec(pkg_version) else: version_spec = helpers.parse_version_spec('*') package = pkg_loader.load_package(name, version_spec) except exceptions.NoPackageFound: if not CONF.packages_opts.load_packages_from: msg = _('Local package is not found since "load-packages-from"' ' engine parameter is not provided and specified ' 'packages is not loaded to murano-api') else: msg = _('Specified package is not found: {0} were scanned ' 'together with murano database' ).format(','.join( CONF.packages_opts.load_packages_from)) LOG.error(msg) self.error(msg, show_help=False) except exc.CommunicationError: msg = ('Murano API is not running. ' 'Check configuration parameters.') LOG.error(msg) self.error(msg, show_help=False) return package
def _process_item(self, obj, owner_id, path): if isinstance(obj, dict) and '?' in obj: obj_rule = self._create_object_rule(obj, owner_id) self._rules.append(obj_rule) # the environment has 'services' relationships # to all its top-level applications # traversal path is used to test whether # we are at the right place within the tree if path == ('applications', ): self._rules.append( RelationshipRule(self._env_id, obj_rule.obj_id, "services")) self._rules.extend(self._create_propety_rules( obj_rule.obj_id, obj)) cls = obj['?']['type'] if 'classVersion' in obj['?']: version_spec = helpers.parse_version_spec( semantic_version.Version(obj['?']['classVersion'])) else: version_spec = semantic_version.Spec('*') types = self._get_parent_types(cls, self._package_loader, version_spec) self._rules.extend( self._create_parent_type_rules(obj['?']['id'], types)) # current object will be the owner for its subtree return obj_rule.obj_id
def generate_schema(pkg_loader, context_manager, class_name, method_names=None, class_version=None, package_name=None): """Generate JSON schema JSON Schema is generated either for the class with all model builders or for specified model builders only. The return value is a dictionary with keys being model builder names and the values are JSON schemas for them. The class itself is represented by an empty string key. """ if method_names and not isinstance(method_names, (list, tuple)): method_names = (method_names,) version = helpers.parse_version_spec(class_version) if package_name: package = pkg_loader.load_package(package_name, version) else: package = pkg_loader.load_class_package(class_name, version) cls = package.find_class(class_name, search_requirements=False) exc = executor.MuranoDslExecutor(pkg_loader, context_manager) with helpers.with_object_store(exc.object_store): context = exc.create_object_context(cls) model_builders = set(list_model_builders(cls, context)) method_names = model_builders.intersection( method_names or model_builders) result = { name: generate_entity_schema( get_entity(cls, name), context, cls, get_meta(cls, name, context)) for name in method_names } return result
def load_package(self, package_name, version_spec): fixed_versions = self._fixations[package_name] version = version_spec.select(fixed_versions) if version: version_spec = helpers.parse_version_spec(version) packages = self._package_cache.get(package_name) if packages: version = version_spec.select(packages.keys()) if version: return packages[version] filter_opts = {'fqn': package_name, 'version': helpers.breakdown_spec_to_query( version_spec)} try: package_definition = self._get_definition(filter_opts) self._lock_usage(package_definition) except LookupError: exc_info = sys.exc_info() six.reraise(exceptions.NoPackageFound, exceptions.NoPackageFound(package_name), exc_info[2]) else: package = self._get_package_by_definition(package_definition) self._fixations[package_name].add(package.version) self._new_fixations[package_name].add(package.version) return self._to_dsl_package(package)
def __init__(self, package_loader, name, version=None, runtime_version=None, requirements=None, meta=None): super(MuranoPackage, self).__init__() self._package_loader = weakref.proxy(package_loader) self._name = name self._version = helpers.parse_version(version) self._runtime_version = helpers.parse_version(runtime_version) self._requirements = { name: semantic_version.Spec('==' + str(self._version.major)) } if name != constants.CORE_LIBRARY: self._requirements[constants.CORE_LIBRARY] = \ semantic_version.Spec('==0') self._classes = {} self._imported_types = {object, murano_object.MuranoObject} for key, value in six.iteritems(requirements or {}): self._requirements[key] = helpers.parse_version_spec(value) self._load_queue = {} self._native_load_queue = {} if self.name == constants.CORE_LIBRARY: principal_objects.register(self) self._package_class = self._create_package_class() self._meta = lambda: dslmeta.MetaData(meta, dsl_types.MetaTargets. Package, self._package_class)
def __init__(self, package_loader, name, version=None, runtime_version=None, requirements=None, meta=None): super(MuranoPackage, self).__init__() self._package_loader = weakref.proxy(package_loader) self._name = name self._version = helpers.parse_version(version) self._runtime_version = helpers.parse_version(runtime_version) self._requirements = { name: semantic_version.Spec('==' + str(self._version.major)) } if name != constants.CORE_LIBRARY: self._requirements[constants.CORE_LIBRARY] = \ semantic_version.Spec('==0') self._classes = {} self._imported_types = {object, murano_object.MuranoObject} for key, value in six.iteritems(requirements or {}): self._requirements[key] = helpers.parse_version_spec(value) self._load_queue = {} self._native_load_queue = {} if self.name == constants.CORE_LIBRARY: principal_objects.register(self) self._package_class = self._create_package_class() self._meta = lambda: dslmeta.MetaData( meta, dsl_types.MetaTargets.Package, self._package_class)
def load_package(self, package_name, version_spec): fixed_versions = self._fixations[package_name] version = version_spec.select(fixed_versions) if version: version_spec = helpers.parse_version_spec(version) packages = self._package_cache.get(package_name) if packages: version = version_spec.select(six.iterkeys(packages)) if version: return packages[version] filter_opts = {'fqn': package_name, 'version': helpers.breakdown_spec_to_query( version_spec)} try: package_definition = self._get_definition(filter_opts) self._lock_usage(package_definition) except LookupError: exc_info = sys.exc_info() six.reraise(exceptions.NoPackageFound(package_name), None, exc_info[2]) else: package = self._get_package_by_definition(package_definition) self._fixations[package_name].add(package.version) self._new_fixations[package_name].add(package.version) return self._to_dsl_package(package)
def find_class(self, name, search_requirements=True): payload = self._native_load_queue.pop(name, None) if payload is not None: return self._register_native_class(payload, name) payload = self._load_queue.pop(name, None) if payload is not None: result = self._register_mpl_classes(payload, name) if result: return result result = self._classes.get(name) if result: return result if search_requirements: pkgs_for_search = [] for package_name, version_spec in six.iteritems( self._requirements): if package_name == self.name: continue referenced_package = self._package_loader.load_package( package_name, version_spec) try: return referenced_package.find_class(name, False) except exceptions.NoClassFound: pkgs_for_search.append(referenced_package) continue return self._package_loader.load_class_package(name, helpers.parse_version_spec(None)).find_class(name, False) #raise exceptions.NoClassFound( # name, packages=pkgs_for_search + [self]) raise exceptions.NoClassFound(name, packages=[self])
def _process_item(self, obj, owner_id, path): if isinstance(obj, dict) and '?' in obj: obj_rule = self._create_object_rule(obj, owner_id) self._rules.append(obj_rule) # the environment has 'services' relationships # to all its top-level applications # traversal path is used to test whether # we are at the right place within the tree if path == ('applications',): self._rules.append(RelationshipRule(self._env_id, obj_rule.obj_id, "services")) self._rules.extend( self._create_propety_rules(obj_rule.obj_id, obj)) cls = obj['?']['type'] if 'classVersion' in obj['?']: version_spec = helpers.parse_version_spec( semantic_version.Version(obj['?']['classVersion'])) else: version_spec = semantic_version.Spec('*') types = self._get_parent_types( cls, self._package_loader, version_spec) self._rules.extend(self._create_parent_type_rules(obj['?']['id'], types)) # current object will be the owner for its subtree return obj_rule.obj_id
def _get_parent_types(type_name, package_loader, version_spec): type_name, version_spec, _ = helpers.parse_type_string( type_name, version_spec, None) version_spec = helpers.parse_version_spec(version_spec) result = {type_name} if package_loader: pkg = package_loader.load_class_package(type_name, version_spec) cls = pkg.find_class(type_name, False) if cls: result.update(t.name for t in cls.ancestors()) return result
def _load_package(pkg_loader, name): try: parts = name.rsplit('/') if len(parts) == 2: name, pkg_version = parts version_spec = helpers.parse_version_spec(pkg_version) else: version_spec = helpers.parse_version_spec('*') package = pkg_loader.load_package(name, version_spec) except exceptions.NoPackageFound: if not CONF.engine.load_packages_from: msg = _('Local package is not found since "load-packages-from" ' 'engine parameter is not provided and specified packages ' 'is not loaded to murano-api') else: msg = _('Specified package is not found: {0} were scanned together' ' with murano database' ).format(','.join(CONF.engine.load_packages_from)) LOG.error(msg) sys.stderr.write("ERROR: {msg}".format(msg=msg)) sys.exit(1) return package
def load_package(self, package_name, version_spec): fixed_versions = self._fixations[package_name] version = version_spec.select(fixed_versions) if version: version_spec = helpers.parse_version_spec(version) packages = self._packages_by_name.get(package_name) if not packages: raise exceptions.NoPackageFound(package_name) version = version_spec.select(six.iterkeys(packages)) if not version: raise exceptions.NoPackageFound(package_name) self._fixations[package_name].add(version) self._new_fixations[package_name].add(version) return packages[version]
def load(self, value, owner, context=None): if value is None: return None if '?' not in value or 'type' not in value['?']: raise ValueError() system_key = value['?'] object_id = system_key['id'] obj_type = system_key['type'] version_spec = helpers.parse_version_spec( system_key.get('classVersion')) if 'package' not in system_key: package = self.executor.package_loader.load_class_package( obj_type, version_spec) else: package = self.executor.package_loader.load_package( system_key['package'], version_spec) class_obj = package.find_class(obj_type, False) try: if owner is None: self._initializing = True if object_id in self._store: factory = self._store[object_id] if isinstance(factory, dsl_types.MuranoObject): return factory else: factory = class_obj.new(owner, self, self.executor, name=system_key.get('name'), object_id=object_id) self._store[object_id] = factory system_value = ObjectStore._get_designer_attributes(system_key) self._designer_attributes_store[object_id] = system_value init_context = self.executor.create_object_context( factory.object, context) obj = factory(init_context, **value) if not self._initializing: self._store[object_id] = obj if owner is None: self._initializing = False self._store[object_id] = factory(init_context, **value) finally: if owner is None: self._initializing = False return factory.object
def load_package(self, package_name, version_spec): fixed_versions = self._fixations[package_name] version = version_spec.select(fixed_versions) if version: version_spec = helpers.parse_version_spec(version) packages = self._packages_by_name.get(package_name) if not packages: raise exceptions.NoPackageFound(package_name) version = version_spec.select(packages.keys()) if not version: raise exceptions.NoPackageFound(package_name) self._fixations[package_name].add(version) self._new_fixations[package_name].add(version) return packages[version]
def load(self, value, owner, context=None, defaults=None): if value is None: return None if '?' not in value or 'type' not in value['?']: raise ValueError() system_key = value['?'] object_id = system_key['id'] obj_type = system_key['type'] version_spec = helpers.parse_version_spec( system_key.get('classVersion')) if 'package' not in system_key: package = self.executor.package_loader.load_class_package( obj_type, version_spec) else: package = self.executor.package_loader.load_package( system_key['package'], version_spec) class_obj = package.find_class(obj_type, False) try: if owner is None: self._initializing = True if object_id in self._store: factory = self._store[object_id] if isinstance(factory, dsl_types.MuranoObject): return factory else: factory = class_obj.new( owner, self, name=system_key.get('name'), object_id=object_id, defaults=defaults) self._store[object_id] = factory system_value = ObjectStore._get_designer_attributes(system_key) self._designer_attributes_store[object_id] = system_value init_context = self.executor.create_object_context( factory.object, context) obj = factory(init_context, **value) if not self._initializing: self._store[object_id] = obj if owner is None: self._initializing = False self._store[object_id] = factory(init_context, **value) finally: if owner is None: self._initializing = False return factory.object
def _invoke(self, mpl_executor): class_name = self.action['class_name'] pkg_name = self.action['pkg_name'] class_version = self.action['class_version'] version_spec = helpers.parse_version_spec(class_version) if pkg_name: package = mpl_executor.package_loader.load_package( pkg_name, version_spec) else: package = mpl_executor.package_loader.load_class_package( class_name, version_spec) cls = package.find_class(class_name, search_requirements=False) method_name, kwargs = self.action['method'], self.action['args'] return mpl_executor.run(cls, method_name, None, (), kwargs)
def test_execute(self, mock_package_loader, mock_serializer): mock_class = mock.Mock() mock_package = mock.Mock(spec=murano_package.MuranoPackage) mock_package.find_class.return_value = mock_class mock_package_loader.load_package.return_value = mock_package version_spec = helpers.parse_version_spec(self.action['class_version']) self.task_executor.execute() mock_package_loader.load_package.assert_called_once_with( 'TestPackage', version_spec) mock_package.find_class.assert_called_once_with( self.action['class_name'], search_requirements=False) mock_class.invoke.assert_called_once_with('TestAction', None, (), {'name': 'foo'}) self.assertTrue(mock_serializer.serialize.called)
def check(self, expected, version_spec): self.assertEqual(expected, helpers.parse_version_spec(version_spec))
def on_class(self, class_name): cls = self.executor.package_loader.load_class_package( class_name, helpers.parse_version_spec(None)).find_class(class_name, False) return Runner.DslObjectWrapper(cls, self)