def _parse_plugin(self): with open(self.path) as fp: code = fp.read() try: module = ast.parse(code) # type: ast.Module assert isinstance(module, ast.Module), 'Module expected' except SyntaxError as e: raise exc.ContentLoadError( "Syntax error while parsing module {0}: Line {1}:{2} {3}". format(os.path.basename(self.path), e.lineno, e.offset, e.text)) for node in module.body: if not isinstance(node, ast.Assign): continue name = node.targets[0].id if name == 'DOCUMENTATION': try: self.documentation = ast_utils.parse_ast_doc(node) except ValueError as e: self.log.warning( 'Cannot parse "DOCUMENTATION": {}'.format(e)) break
def parse_dependencies(self): if not self.dependencies: return [] dependencies = [] for dep in self.dependencies: dep = ansible_req.RoleRequirement.role_yaml_parse(dep) name = dep.get('name') if not name: raise exc.ContentLoadError( 'Unable to determine name for dependency') if '.' not in name: raise exc.ContentLoadError( 'Expecting dependency name format to match ' '"username.role_name", got {0}'.format(dep['name'])) dependencies.append(models.DependencyInfo(*name.rsplit('.', 2))) return dependencies
def _load_metadata(self): with open(os.path.join(self.path, self.metadata_file)) as fp: metadata = yaml.safe_load(fp) if not isinstance(metadata, dict): raise exc.ContentLoadError( "Invalid 'apb.yml' file format, dict expected.") return metadata
def _get_dependencies(self, metadata): if 'dependencies' in metadata: dependencies = metadata['dependencies'] if not isinstance(dependencies, collections.Sequence): raise exc.ContentLoadError( "Expecting 'dependencies' in metadata to be a list") else: dependencies = [] return dependencies
def _get_galaxy_info(self, metadata): if 'galaxy_info' in metadata: galaxy_info = metadata['galaxy_info'] if not isinstance(galaxy_info, dict): raise exc.ContentLoadError( "Expecting 'galaxy_info' in metadata to be a dictionary " "or key:value mapping") else: galaxy_info = {} return galaxy_info
def _load_metadata(self): meta_file = self.meta_file if meta_file: meta_file = os.path.join(self.path, meta_file) else: meta_file = self._find_metadata() if not meta_file: raise exc.ContentLoadError( "Failed to find metadata file. Did you forget to add " "'meta/main.yml' or 'meta.yml'?") with open(meta_file) as fp: metadata = yaml.safe_load(fp) if not isinstance(metadata, dict): raise exc.ContentLoadError( "Invalid 'meta.yml' file format, dict expected.") return metadata
def validate_strings(self): string_defaults = [('author', 'your name', True), ('description', 'your description', True), ('company', 'your company', False), ('license', 'license', True)] for key, value, required in string_defaults: if key not in self.metadata and required: exc.ContentLoadError( "Missing required key {0} in metadata".format(key)) if key in self.metadata and value in self.metadata[key]: self.log.warning( "Value of {0} has not been set in metadata.".format(key))
def parse_platforms(self): meta_platforms = self.metadata.get('platforms') if not meta_platforms: return [] if not isinstance(meta_platforms, collections.Sequence): raise exc.ContentLoadError( 'Expected "platforms" in metadata to be a list.') platforms = [] for idx, platform in enumerate(meta_platforms): name = platform.get('name', None) if not name: continue versions = platform.get('versions', ['all']) # TODO: Validate versions platforms.append(models.PlatformInfo(name, versions)) return platforms
def parse_platforms(self): meta_platforms = self.metadata.get('platforms') if not meta_platforms: return [] if not isinstance(meta_platforms, collections.Sequence): raise exc.ContentLoadError( 'Expected "platforms" in metadata to be a list.') platforms = [] for idx, platform in enumerate(meta_platforms): try: name = platform['name'] except KeyError: self.log.warning( 'No name specified for platform [{0}], skipping' .format(idx)) continue versions = platform.get('versions', ['all']) # TODO: Validate versions platforms.append(models.PlatformInfo(name, versions)) return platforms
def _load_container_yml(self): container_yml = None container_yml_type = None container_meta_file = os.path.join(self.path, self.CONTAINER_META_FILE) if os.path.exists(container_meta_file): container_yml_type = self.CONTAINER_META_FILE with open(container_meta_file) as fp: container_yml = yaml.safe_load(fp) ansible_container_meta_file = os.path.join( self.path, self.ANSIBLE_CONTAINER_META_FILE) if os.path.exists(ansible_container_meta_file): if container_yml_type is not None: raise exc.ContentLoadError( 'Found container.yml and meta/container.yml. ' 'A role can have only one container.yml file.') container_yml_type = self.ANSIBLE_CONTAINER_META_FILE with open(ansible_container_meta_file) as fp: container_yml = yaml.safe_load(fp) return container_yml_type, container_yml
def load(self): branch = git.get_current_branch(directory=self.path) commit = git.get_commit_info(directory=self.path) finder, contents = self._find_contents() result = list(self._load_contents(contents)) readme = self._get_readme(finder.repository_format) if not all(v[1] for v in result): raise exc.ContentLoadError('Lint failed') name = None if (finder.repository_format in (constants.RepositoryFormat.ROLE, constants.RepositoryFormat.APB) and result[0][0].name): name = result[0][0].name return models.Repository(branch=branch, commit=commit, format=finder.repository_format, contents=[v[0] for v in result], readme=readme, name=name)
def validate_strings(self): required_keys = ['author', 'description', 'license'] for key in required_keys: if key not in self.metadata: exc.ContentLoadError( "Missing required key {0} in metadata".format(key))