def run(self, comment_url, database_uri=None): """Delete a comment, given its loveme.do URL. @param comment_url: A loveme.do comment URL, in the form http://loveme.do/comment/fluidinfo.com/terrycojones/ 2012-08-03T22:04:13.698896 """ database_uri = database_uri or 'postgres:///fluidinfo' setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') try: importer, username, timestamp = parseCommentURL(comment_url) except ValueError as error: self._error(str(error)) # Delete the comment & report status. count = CommentAPI(None).delete(importer, username, timestamp) if count == 0: self._error('Comment not found.') elif count == 1: transaction.commit() print >> self.outf, 'Comment deleted.' sys.exit(0) else: self._error('Unexpected return result (%r) trying to delete ' 'comment.' % count)
def run(self, database_uri, username, email=None, fullname=None, password=None, role=None): if not any([email, fullname, password, role]): print( 'You must provide an email, name, password, or role ' 'to update.') return if role is not None: try: role = Role.fromName(role) except LookupError: print 'Invalid role' return config = setupConfig(None) config.set('store', 'main-uri', database_uri) setConfig(config) setupLogging(self.outf) setupStore(database_uri, 'main') updateUser(username, password, fullname, email, role)
def run(self, database_uri, username, password, fullname, email, role=None): username = username.decode('utf-8') password = password.decode('utf-8') fullname = '' if fullname is None else fullname.decode('utf-8') email = '' if email is None else email.decode('utf-8') roles = { 'anonymous': Role.ANONYMOUS, 'superuser': Role.SUPERUSER, 'user': Role.USER, 'usermanager': Role.USER_MANAGER } role = roles.get(role.lower() if role else 'user') config = setupConfig(None) config.set('store', 'main-uri', database_uri) config.set('index', 'url', '') setConfig(config) setupLogging(self.outf) setupStore(database_uri, 'main') createUser(username, password, fullname, email, role)
def testSetupLoggingWithStream(self): """ L{setupLogging} can configure logging to write messages to a stream. """ stream = StringIO() setupLogging(stream=stream) logging.info('Test log message.') self.assertIn('Test log message.', stream.getvalue())
def make(self, dependency_resources): """Setup logging and return the log stream.""" stream = StringIO() log = logging.getLogger() while log.handlers: log.removeHandler(log.handlers.pop()) setupLogging(stream=stream, format=self._format) return stream
def run(self, database_uri): setupLogging(self.outf) store = setupStore(database_uri, 'logs') rows = reportErrorSummary(store) print_columns(self.outf, rows, shrink_index=2) print >> self.outf print '%s occurrences of %s errors' % ( sum(int(item[0]) for item in rows), len(rows))
def run(self, database_uri, version): version = version.decode('utf-8') config = setupConfig(None) config.set('store', 'main-uri', database_uri) setConfig(config) setupLogging(self.outf) setupStore(database_uri, 'main') setVersionTag(version)
def run(self, database_uri): setupLogging(self.outf) store = setupStore(database_uri, 'logs') rows = reportErrorSummary(store) print_columns(self.outf, rows, shrink_index=2) print >> self.outf print '%s occurrences of %s errors' % (sum( int(item[0]) for item in rows), len(rows))
def run(self, database_uri, max_rows_per_query=None): setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') logging.info('Checking database:') if max_rows_per_query: checkIntegrity(max_rows_per_query) else: checkIntegrity()
def run(self, database_uri, limit=None): if limit is None: limit = 15 setupLogging(self.outf) store = setupStore(database_uri, 'logs') rows = list(reportTraceLogSummary(store, limit)) print_columns(self.outf, rows) print >> self.outf print 'Top %s slowest requests' % limit
def run(self, database_uri, index_url, username): username = username.decode('utf-8') config = setupConfig(None) config.set('store', 'main-uri', database_uri) config.set('index', 'url', str(index_url)) setConfig(config) setupLogging(self.outf) setupStore(database_uri, 'main') deleteUser(username)
def run(self, path, database_uri): setupLogging(self.outf) store = setupStore(database_uri, 'logs') schema = logs.createSchema() logging.info('Creating schema') patchDatabase(store, schema) logging.info('Loading log file %s', path) loadLogs(path, store) logging.info('Finished loading log file %s', path)
def run(self, path, database_uri, old_format=None): setupLogging(self.outf) store = setupStore(database_uri, 'logs') schema = logs.createSchema() logging.info('Creating schema') patchDatabase(store, schema) logging.info('Loading trace logs from %s', path) loadTraceLogs(path, store, old_format) logging.info('Finished loading trace logs from %s', path)
def testSetupLoggingWithFile(self): """ L{setupLogging} can configure logging to write messages to a file. """ path = self.fs.makePath() setupLogging(path=path) logging.info('Log message.') logging.getLogger().handlers[0].flush() with open(path, 'r') as log: self.assertIn('Log message.', log.read())
def run(self, database_uri): setConfig(setupConfig(None)) setupLogging(self.outf) store = setupStore(database_uri, 'main') schema = main.createSchema() logging.info('Creating schema.') patchDatabase(store, schema) logging.info('Creating system data.') bootstrapSystemData(store) logging.info('Creating web admin data.') bootstrapWebAdminData()
def testSetupLoggingWithStreamSupportsUnicode(self): """ The log configured by L{setupLogging} handles C{unicode} messages properly. """ stream = StringIO() setupLogging(stream=stream) logging.info(u'\N{HIRAGANA LETTER A}') logging.getLogger().handlers[0].flush() self.assertIn(u'\N{HIRAGANA LETTER A}'.encode('utf-8'), stream.getvalue())
def testSetupLoggingWithFileSupportsUnicode(self): """ The log configured by L{setupLogging} handles C{unicode} messages properly. """ path = self.fs.makePath() setupLogging(path=path) logging.info(u'\N{HIRAGANA LETTER A}') logging.getLogger().handlers[0].flush() with open(path, 'r') as log: self.assertIn(u'\N{HIRAGANA LETTER A}'.encode('utf-8'), log.read())
def testSetupLoggingWithFileSupportsLogRotation(self): """ L{setupLogging} uses a C{WatchedFileHandler} when a path is used. The handler automatically reopens the log file if it gets moved, by logrotate for example. """ path = self.fs.makePath() setupLogging(path=path) logging.info('Log message 1.') os.rename(path, '%s.1' % path) logging.info('Log message 2.') logging.getLogger().handlers[0].flush() with open(path, 'r') as log: self.assertIn('Log message 2.', log.read()) with open('%s.1' % path, 'r') as log: self.assertIn('Log message 1.', log.read())
def run(self, database_uri): setupLogging(self.outf) store = setupStore(database_uri, 'logs') rows = list(reportErrorTracebacks(store)) for count, exceptionClass, message, traceback in rows: if count > 1: print >> self.outf, '%s occurrences of %s: %s' % ( count, exceptionClass, message) else: print >> self.outf, '%s occurrence of %s: %s' % ( count, exceptionClass, message) print >> self.outf for line in traceback.splitlines(): print >> self.outf, ' ', line print >> self.outf print >> self.outf print >> self.outf, '%s occurrences of %s errors' % (sum( item[0] for item in rows), len(rows))
def run(self, database_uri): setupLogging(self.outf) store = setupStore(database_uri, 'logs') rows = list(reportErrorTracebacks(store)) for count, exceptionClass, message, traceback in rows: if count > 1: print >> self.outf, '%s occurrences of %s: %s' % ( count, exceptionClass, message) else: print >> self.outf, '%s occurrence of %s: %s' % ( count, exceptionClass, message) print >> self.outf for line in traceback.splitlines(): print >> self.outf, ' ', line print >> self.outf print >> self.outf print >> self.outf, '%s occurrences of %s errors' % ( sum(item[0] for item in rows), len(rows))
def run(self, database_uri, username, password, fullname, email, role=None): username = username.decode('utf-8') password = password.decode('utf-8') fullname = '' if fullname is None else fullname.decode('utf-8') email = '' if email is None else email.decode('utf-8') roles = {'anonymous': Role.ANONYMOUS, 'superuser': Role.SUPERUSER, 'user': Role.USER, 'usermanager': Role.USER_MANAGER} role = roles.get(role.lower() if role else 'user') config = setupConfig(None) config.set('store', 'main-uri', database_uri) config.set('index', 'url', '') setConfig(config) setupLogging(self.outf) setupStore(database_uri, 'main') createUser(username, password, fullname, email, role)
def run(self, database_uri): setConfig(setupConfig(None)) setupLogging(self.outf) store = setupStore(database_uri, 'main') schema = main.createSchema() status = getPatchStatus(store, schema) if status.unknownPatches: unknownPatches = ', '.join('patch_%d' % version for version in status.unknownPatches) logging.critical('Database has unknown patches: %s', unknownPatches) return 1 if status.unappliedPatches: unappliedPatches = ', '.join( 'patch_%d' % version for version in status.unappliedPatches) logging.info('Applying patches: %s', unappliedPatches) patchDatabase(store, schema) else: logging.info('Database is up-to-date.')
def run(self, database_uri): setConfig(setupConfig(None)) setupLogging(self.outf) store = setupStore(database_uri, 'main') schema = main.createSchema() status = getPatchStatus(store, schema) if status.unappliedPatches: patches = ', '.join('patch_%d' % version for version in status.unappliedPatches) logging.info('Unapplied patches: %s' % patches) sys.exit(1) if status.unknownPatches: patches = ', '.join('patch_%d' % version for version in status.unknownPatches) logging.info('Unknown patches: %s' % patches) sys.exit(2) if not status.unappliedPatches and not status.unknownPatches: logging.info('Database is up-to-date.') sys.exit(0)
def run(self, database_uri): setConfig(setupConfig(None)) setupLogging(self.outf) store = setupStore(database_uri, 'main') schema = main.createSchema() status = getPatchStatus(store, schema) if status.unknownPatches: unknownPatches = ', '.join( 'patch_%d' % version for version in status.unknownPatches) logging.critical('Database has unknown patches: %s', unknownPatches) return 1 if status.unappliedPatches: unappliedPatches = ', '.join( 'patch_%d' % version for version in status.unappliedPatches) logging.info('Applying patches: %s', unappliedPatches) patchDatabase(store, schema) else: logging.info('Database is up-to-date.')
def run(self, path_list, username=None, database_uri=None, batch_size=None): if database_uri is None: database_uri = 'postgres:///fluidinfo' if batch_size is None: batch_size = 100 if username is None: print >> self.outf, 'You must provide a username.' sys.exit(1) setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') client = DatasetImporter(batch_size) for filename in path_list: with open(filename, 'r') as file: data = load(file) client.upload(username, data['objects'])
def run(self, database_uri, username, email=None, fullname=None, password=None, role=None): if not any([email, fullname, password, role]): print ('You must provide an email, name, password, or role ' 'to update.') return if role is not None: try: role = Role.fromName(role) except LookupError: print 'Invalid role' return config = setupConfig(None) config.set('store', 'main-uri', database_uri) setConfig(config) setupLogging(self.outf) setupStore(database_uri, 'main') updateUser(username, password, fullname, email, role)
def run(self, database_uri, index_uri): setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') return buildIndex(str(index_uri))
def testSetupLoggingUsesRootLogger(self): """L{setupLogging} configures the C{logging} module's root logger.""" stream = StringIO() log = setupLogging(stream=stream) self.assertIdentical(log, logging.getLogger())
def testSetupLoggingUsesDebugLevel(self): """L{setupLogging} uses the C{logging.INFO} log level, by default.""" stream = StringIO() setupLogging(stream=stream) self.assertEqual(logging.INFO, logging.getLogger().level)
def run(self, database_uri): setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') removeTestingData()
def run(self, database_uri, filename, interval, max_objects): setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') return batchIndex(filename, int(interval), int(max_objects))
def run(self, database_uri, index_uri, modified_since): setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') modified_since = datetime.strptime(modified_since, '%Y-%m-%d') return updateIndex(str(index_uri), modified_since)
def run(self, database_uri): setConfig(setupConfig(None)) setupLogging(self.outf) setupStore(database_uri, 'main') prepareForTesting()
def run(self, index_uri): setConfig(setupConfig(None)) setupLogging(self.outf) return deleteIndex(str(index_uri))
def testSetupLoggingWithCustomLevel(self): """L{setupLogging} sets the log level passed by the caller.""" stream = StringIO() log = setupLogging(stream=stream, level=logging.CRITICAL) self.assertEqual(logging.CRITICAL, log.level)
def run(self, username, password, host, max_connections=None): setupLogging(self.outf) if max_connections is None: max_connections = 16 endpoint = 'http://%s' % host generateLoad(username, password, endpoint, max_connections)