class Listen(Base): def __init__(self): Base.__init__(self, __name__) self.log.info('Inside DS listener') self.message_reader = MessageReader(self, self.config["VHOST_AMQP_URL"], self.config['LISTEN_QUEUE'], self.config['DEFAULT_EXCHANGE']) self.itinerary = None self.execute = None self.log.info('Boot successful') def main(self): self.log.info('inside Listen main') try: self.message_reader.connect_and_read(self.handle_message_callback) except Exception as exc: self.log.error('Error starting listener - {0}'.format(exc)) finally: self.log.debug('Cleaning up the listener') handlers = self.log.handlers[:] for handler in handlers: handler.close() self.log.remove_handler(handler) print 'STOP_TAILING' sys.stdout.flush() def handle_message_callback(self, message, is_redelivered=True): self.log.info('Message callback invoked: {0}'.format(is_redelivered)) response = {'success': True} try: self.execute = Execute(message) if self.execute.container_name != self.config['CONTAINER_NAME']: error_message = 'Invalid DS message received. ' \ 'Unmatched container names. \n CONFIG: {0}' \ '\nCONTAINER_NAME {1}'.format( self.config, self.execute.container_name) self.log.error(error_message) response['success'] = False response['error'] = error_message raise Exception(error_message) else: self.log.info('Valid DS message : {0}'.format( self.config['BOOT_SUCCESS_MESSAGE'])) self.log.info(self.execute.message) # this is read by HDQ to decide whether DS container # boot was successful or not print self.config['BOOT_SUCCESS_MESSAGE'] sys.stdout.flush() self.execute.run() except Exception as exc: self.log.error(str(exc)) trace = traceback.format_exc() self.log.debug(trace) return response
class Listen(Base): def __init__(self): Base.__init__(self, __name__) self.log.info('Inside DS listener') self.message_reader = MessageReader( self, self.config["VHOST_AMQP_URL"], self.config['LISTEN_QUEUE'], self.config['DEFAULT_EXCHANGE']) self.itinerary = None self.execute = None self.log.info('Boot successful') def main(self): self.log.info('inside Listen main') try: self.message_reader.connect_and_read(self.handle_message_callback) except Exception as exc: self.log.error('Error starting listener - {0}'.format(exc)) finally: self.log.debug('Cleaning up the listener') handlers = self.log.handlers[:] for handler in handlers: handler.close() self.log.remove_handler(handler) print 'STOP_TAILING' sys.stdout.flush() def handle_message_callback(self, message, is_redelivered=True): self.log.info('Message callback invoked: {0}'.format(is_redelivered)) response = {'success': True} try: self.execute = Execute(message) if self.execute.container_name != self.config['CONTAINER_NAME']: error_message = 'Invalid DS message received. ' \ 'Unmatched container names. \n CONFIG: {0}' \ '\nCONTAINER_NAME {1}'.format( self.config, self.execute.container_name) self.log.error(error_message) response['success'] = False response['error'] = error_message raise Exception(error_message) else: self.log.info('Valid DS message : {0}'.format( self.config['BOOT_SUCCESS_MESSAGE'])) self.log.info(self.execute.message) # this is read by HDQ to decide whether DS container # boot was successful or not print self.config['BOOT_SUCCESS_MESSAGE'] sys.stdout.flush() self.execute.run() except Exception as exc: self.log.error(str(exc)) trace = traceback.format_exc() self.log.debug(trace) return response
import os from execute import Execute if __name__ == '__main__': print('Booting up CEXEC') executor = Execute() print('Running CEXEC script') exit_code=executor.run() print('CEXEC has completed') os._exit(exit_code)
class Parser: def __init__(self): self.utils = Utils() self.execute = Execute() self.filereader = FileReader() def parse_query(self, query, dictionary): if query: query = self.utils.spaces_rem(query) keywords = ["from", "select", "distinct", "where"] parsedquery = {} d = tuple() for k in keywords: if k in query and k == "from": parsedquery[k] = query.split(k) elif k == "select" and k in parsedquery["from"][0].strip(): parsedquery['select'] = self.utils.spaces_rem(parsedquery["from"][0].strip().split('select')[1]) elif k == "distinct" and k in parsedquery['select']: parsedquery[k] = self.utils.spaces_rem(parsedquery['select'].strip().split('distinct')[1]) distinct = True elif k == "distinct" and k not in parsedquery['select']: parsedquery[k] = self.utils.spaces_rem(parsedquery['select']) distinct = False elif k == "where": parsedquery['where'] = self.utils.spaces_rem(parsedquery['from'][1]).split('where') tableNames = [self.utils.spaces_rem(x) for x in self.utils.spaces_rem(parsedquery['where'][0]).split(',')] self.filereader.check_tables(tableNames, dictionary) else: sys.exit("No" + k + "Clause") parsedquery['distinct'] = self.utils.spaces_rem(parsedquery["distinct"]) parsedquery['distinct'] = [self.utils.spaces_rem(x) for x in parsedquery["distinct"].split(',')] if distinct: for a in parsedquery['distinct']: if '(' in a or ')' in a: d_att = a.split('(')[1][:-1] if d_att: parsedquery['distinct'][parsedquery['distinct'].index(a)] = d_att break aggregation = "" x = self.filereader.add_tableName([d_att], tableNames, dictionary) d = (x[0], True) aggr_func = "" temp = [] for a in parsedquery['distinct']: if '(' in a or ')' in a: print(a) aggr_func = a else: temp.append(a) parsedquery['distinct'] = temp if aggr_func: if '.' not in aggr_func: print(aggr_func) parsedquery['distinct'] = [self.utils.spaces_rem(aggr_func).split('(')[1][:-1]] parsedquery['distinct'] = self.filereader.add_tableName(parsedquery['distinct'], tableNames, dictionary) aggr_func = self.utils.spaces_rem(aggr_func).split('(')[0] + '(' + parsedquery['distinct'][0] + ')' parsedquery['distinct'] = self.filereader.add_tableName(parsedquery['distinct'], tableNames, dictionary) parsedquery['distinct'] = self.utils.check_attributes(parsedquery['distinct'], tableNames, dictionary) if len(parsedquery['where']) > 1: conditions = self.utils.spaces_rem(parsedquery['where'][1]) else: conditions = "" self.execute.run(dictionary, tableNames, parsedquery['distinct'], conditions, d, aggr_func)
import os from execute import Execute if __name__ == '__main__': print('Booting up CEXEC') executor = Execute() print('Running CEXEC script') exit_code = executor.run() print('CEXEC has completed') os._exit(exit_code)