def getFileContent(self, shell, rootDirectory): """read file content and store it in self.__file_path""" if self.filePath and not self.__file_content: loaded, self.__file_content = self.__loadFile(shell, self.filePath) if loaded and self.__file_content is None: logger.warn('Included file not found: %s' % self.filePath) # if file not found, find that filename in the fileDirectory fileDirectory, fileName = getPathOperation(shell).split( self.filePath) locations = findFile(shell, fileName, fileDirectory, False) # if still not found and contains environment variable, find that filename in the rootDirectory # if still not found, find that filename in the rootDirectory with sub folders if '$' in fileDirectory: locations = locations or \ findFile(shell, fileName, rootDirectory, False) or \ findFile(shell, fileName, rootDirectory, True) if locations: contents = [] for location in locations: logger.debug( 'Find included file in rootDirectory: %s' % location) loaded, content = self.__loadFile(shell, location) if loaded and content is not None: contents.append(content) self.__file_content = '\n'.join(contents) return self.__file_content
def postParse(self, parser, item, path_prefix=''): """ Read the included file, add it to parser :param parser: apache_config_parser :param item: IncludeProperty item :param path_prefix: the path of file which has this include sentense """ logger.debug('Load included file: %s' % item.filePath) # append file path if the include filePath is not absolute path_operation = getPathOperation(parser.shell) # use server_root for relative path path_prefix = getattr(item.root, 'server_root', path_prefix) if not path_operation.isabs(item.filePath): item.filePath = path_operation.join(path_prefix, item.filePath) logger.debug('Load included file using absolute path: %s' % item.filePath) content = item.getFileContent(parser.shell, path_prefix) # append include content to parse iterator if content: path, name = path_operation.split(item.filePath) parser.contentsIterator.addContent(parser.shell, content, path, name) logger.debug('Successful loading included file: %s' % item.filePath) else: logger.debug('Failed loading included file: %s' % item.filePath)
def findVariables(self, shell, items, content, variableResolver): """parse apache websphere plugin and return plugin-cfg xml path""" wascfgfile = items.filter(cls=Property, name='WebSpherePluginConfig').first_value('value', flat=True) if wascfgfile: if wascfgfile[0] in ("'", '"'): wascfgfile = wascfgfile[1:] if wascfgfile[-1] in ("'", '"'): wascfgfile = wascfgfile[:-1] wascfgpath, _ = getPathOperation(shell).split(wascfgfile) logger.debug('found websphere plugin-cfg.xml path: %s' % wascfgpath) return {'pluginPath': (wascfgpath, )} return {}
def parseApacheConf(self, content, contentPath="", contentName=""): """ Parse Apache Config :param content: content of Apache config :param contentPath: the file path of the given content :param contentName: the file name of the given content """ if content is None: return self.rootBlock = Block('', is_root=True) self.items = ConfigItems() self.contentsIterator = self.ContentsIterator() currentBlock = self.rootBlock self.contentsIterator.addContent(self.shell, content, contentPath, contentName) for iterator in self.contentsIterator: line, lineNumber, path, name = iterator if not line: continue line = line.strip() if not line or line[0] == '#': continue try: parserRule = getApacheConfParserRule(line) if parserRule: item = parserRule.parse(line) if item and isinstance(item, ConfigItem): currentBlock.appendChild(item) self.items.append(item) currentBlock = parserRule.getNextBlock(item, currentBlock) parserRule.postParse(self, item, path) else: logger.warn( "cannot find parser rule for config: %s. (%s Line %s)" % (line, getPathOperation(self.shell).join( path, name), lineNumber)) except ApacheConfigParserException, e: raise ApacheConfigParserException( '%s: %s Line %s' % (e.message, getPathOperation( self.shell).join(path, name), lineNumber))
def parseApacheConf(self, content, contentPath="", contentName=""): """ Parse Apache Config :param content: content of Apache config :param contentPath: the file path of the given content :param contentName: the file name of the given content """ if content is None: return self.rootBlock = Block('', is_root=True) self.items = ConfigItems() self.contentsIterator = self.ContentsIterator() currentBlock = self.rootBlock self.contentsIterator.addContent(self.shell, content, contentPath, contentName) for iterator in self.contentsIterator: line, lineNumber, path, name = iterator if not line: continue line = line.strip() if not line or line[0] == '#': continue try: parserRule = getApacheConfParserRule(line) if parserRule: item = parserRule.parse(line) if item and isinstance(item, ConfigItem): currentBlock.appendChild(item) self.items.append(item) currentBlock = parserRule.getNextBlock(item, currentBlock) parserRule.postParse(self, item, path) else: logger.warn("cannot find parser rule for config: %s. (%s Line %s)" % ( line, getPathOperation(self.shell).join(path, name), lineNumber)) except ApacheConfigParserException, e: raise ApacheConfigParserException('%s: %s Line %s' % ( e.message, getPathOperation(self.shell).join(path, name), lineNumber))
def findVariables(self, shell, items, content, variableResolver): """parse apache websphere plugin and return plugin-cfg xml path""" wascfgfile = items.filter( cls=Property, name='WebSpherePluginConfig').first_value('value', flat=True) if wascfgfile: if wascfgfile[0] in ("'", '"'): wascfgfile = wascfgfile[1:] if wascfgfile[-1] in ("'", '"'): wascfgfile = wascfgfile[:-1] wascfgpath, _ = getPathOperation(shell).split(wascfgfile) logger.debug('found websphere plugin-cfg.xml path: %s' % wascfgpath) return {'pluginPath': (wascfgpath, )} return {}
def getFileContent(self, shell, rootDirectory): """read file content and store it in self.__file_path""" if self.filePath and not self.__file_content: loaded, self.__file_content = self.__loadFile(shell, self.filePath) if loaded and self.__file_content is None: logger.warn('Included file not found: %s' % self.filePath) # if file not found, find that filename in the fileDirectory fileDirectory, fileName = getPathOperation(shell).split(self.filePath) locations = findFile(shell, fileName, fileDirectory, False) # if still not found and contains environment variable, find that filename in the rootDirectory # if still not found, find that filename in the rootDirectory with sub folders if '$' in fileDirectory: locations = locations or \ findFile(shell, fileName, rootDirectory, False) or \ findFile(shell, fileName, rootDirectory, True) if locations: contents = [] for location in locations: logger.debug('Find included file in rootDirectory: %s' % location) loaded, content = self.__loadFile(shell, location) if loaded and content is not None: contents.append(content) self.__file_content = '\n'.join(contents) return self.__file_content