def start(): """Starts the WebApp.""" # TODO: Figure out these imports from partify import admin, history, player, queue, statistics, track, user, vote init_db() on_startup() app.logger.debug(app.config) if app.config.get('PROFILE', False): datetime_string = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") f = open("tmp/profile%s.log" % datetime_string, "w") stream = MergeStream(f) app.wsgi_app = ProfilerMiddleware(app.wsgi_app, stream) if app.config['SERVER'] == 'builtin': app.run(host=app.config['SERVER_HOST'], port=app.config['SERVER_PORT']) elif app.config['SERVER'] == 'tornado': import tornado.options from tornado.wsgi import WSGIContainer from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop tornado.options.enable_pretty_logging() http_server = HTTPServer(WSGIContainer(app)) http_server.listen(app.config['SERVER_PORT']) IOLoop.instance().start()
def create_app(config, enable_profiler=False, profiler_quiet=False): """Create a Flask app.""" app = Flask(__name__.split('.')[0], template_folder='web/templates', static_folder='web/static') register_app(app, config) register_hook(app) register_blueprints(app) register_extensions(app) register_errorhandlers(app) register_template_helpers(app) if enable_profiler: from werkzeug.contrib.profiler import ProfilerMiddleware, MergeStream f = open('profiler.log', 'w') if profiler_quiet: app.wsgi_app = ProfilerMiddleware(app.wsgi_app, f, profile_dir="c:/tmp") else: stream = MergeStream(sys.stdout, f) app.wsgi_app = ProfilerMiddleware(app.wsgi_app, stream, profile_dir="c:/tmp") return app
def profile_run(): from werkzeug import run_simple from werkzeug.contrib.profiler import ProfilerMiddleware, MergeStream print "* Profiling" f = open('./profiler.log', 'w') profiled_app = ProfilerMiddleware(app, MergeStream(sys.stderr, f)) run_simple('localhost', 5000, profiled_app, use_reloader=True, use_debugger=True)
def __call__ (self, app) : file_handles = [] for fn in self.log_files : if hasattr (fn, "write") : file_handles.append (fn) elif fn == "stderr" : file_handles.append (sys.stderr) else : if self.delete_logs and os.path.isfile (fn) : os.unlink (fn) file_handles.append (open (fn, "w")) stream = MergeStream (* file_handles) if file_handles else None return ProfilerMiddleware (app, stream, self.sort_by, self.restrictions)
def setup_profiler(app): """ Set up a profiler Outputs to file and stream See profile_analyzer.py in Mycodo/mycodo/scripts/ """ app.config['PROFILE'] = True new = 'profile-{dt:%Y-%m-%d_%H:%M:%S}'.format(dt=datetime.datetime.now()) profile_path = assure_path_exists(os.path.join(INSTALL_DIRECTORY, new)) profile_log = os.path.join(profile_path, 'profile.log') profile_log_file = open(profile_log, 'w') stream = MergeStream(sys.stdout, profile_log_file) app.wsgi_app = ProfilerMiddleware(app.wsgi_app, stream, restrictions=[30]) return app
def setup_profiling(application): # Setup profiling from werkzeug.contrib.profiler import ProfilerMiddleware, MergeStream profiling_dir = "profiling" f = open('profiler.log', 'w') stream = MergeStream(sys.stdout, f) if not os.path.exists(profiling_dir): os.makedirs(profiling_dir) application.config['PROFILE'] = True application.wsgi_app = ProfilerMiddleware( application.wsgi_app, stream, profile_dir=profiling_dir) application.debug = True
else: application = MaintenanceWsgiApp() robots_path = os.path.join(os.path.dirname(__file__), 'files/robots.txt') if os.path.isfile(robots_path): from werkzeug.wsgi import SharedDataMiddleware application = SharedDataMiddleware(application, {'/robots.txt': robots_path}) if settings.is_debug_mode(): from werkzeug.debug import DebuggedApplication application = DebuggedApplication(application) # profiling if settings.debug_level() == settings.DEBUG_AND_PROFILE: from werkzeug.contrib.profiler import ProfilerMiddleware, MergeStream stream = MergeStream( sys.stdout, open(settings.get('global', 'profile_log_path'), 'w')) application = ProfilerMiddleware(application, stream) if __name__ == '__main__': from werkzeug.serving import run_simple from werkzeug.wsgi import SharedDataMiddleware import argparse DEFAULT_PORT = 5000 DEFAULT_ADDR = '127.0.0.1' parser = argparse.ArgumentParser( description='Starts a local development server') parser.add_argument('--port', dest='port_num', action=None,
import sys from werkzeug.contrib.profiler import ProfilerMiddleware, MergeStream from eudat_http_api import app app.config['PROFILE'] = True f = open('profiler.log', 'w') stream = MergeStream(sys.stdout, f) app.wsgi_app = ProfilerMiddleware(app.wsgi_app, stream) app.run(debug=True)
# create timestamp now = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S") profile_dir = 'profiling' now_dir = profile_dir + '/' + now temp_dir = 'temp' if not os.path.exists(profile_dir): os.mkdir(profile_dir) os.mkdir(now_dir) os.mkdir(temp_dir) app.config['PROFILE'] = True f = open('profiler.log', 'w') stream = MergeStream(sys.stdout, f) app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions = [50], stream=stream, profile_dir=temp_dir) # define requests for profiling and create profile data app.test_client().get('/') app.test_client().get('/user') app.test_client().get('/user/1') for file_name in os.listdir(temp_dir): name = '.'.join(file_name.split('.')[:-2]) request_dir = os.path.join(now_dir, name) os.mkdir(request_dir) old_file_path = os.path.join(temp_dir, file_name) new_file_path = os.path.join(request_dir, 'profile.prof') os.rename(old_file_path, new_file_path)
def create_app(debug=True, enable_profiler=False, profiler_quiet=False, enable_timepro=False): """ :param debug: whether to configure app for debug :param enable_profiler: enable flask profiler, causes function to return ProfilerMiddleware rather than Flask object which can be started by run_simple :param profiler_quiet: when profiler enabled sets whether profiler output echoed to stdout """ app = Flask("iiab") app.url_map.strict_slashes = False app.use_x_sendfile = config().getboolean('WEBAPP', 'use_x_sendfile') app.secret_key = '1785132b4fd244a2a1ce6ae3f1d978ac' # Configuration items if debug: app.debug = True app.config['DEBUG'] = True app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 base_prefix = config().get('WEBAPP', 'base_prefix') blueprints = [ (top_views.blueprint, base_prefix), (search_views.blueprint, base_prefix), (gutenberg.gutenberg, base_prefix + "books"), (map_views.blueprint, base_prefix + "maps"), (video_views.blueprint, base_prefix + "video"), (wikipedia_views.blueprint, base_prefix + "wikipedia"), (zim_views.blueprint, base_prefix + "zim"), (gutenberg_content_views.blueprint, base_prefix + "books"), (settings_views.blueprint, base_prefix + "settings"), ] for blueprint, prefix in blueprints: app.register_blueprint(blueprint, url_prefix=prefix) gutenberg.set_flask_app(app) gutenberg.init_db() configure_babel(app) # Auto Index the software repository autoindex = AutoIndex(app, add_url_rules=False) # FIXME: this should be done more elegantly -bcg'13 @app.route(base_prefix + 'software/<path:path>') @app.route(base_prefix + 'software') def software_view(path='.'): software_dir = config().get_path('SOFTWARE', 'software_dir') return autoindex.render_autoindex(path, browse_root=software_dir, endpoint='software_view') #print "URL MAP: ", app.url_map # Static handling from http://flask.pocoo.org/mailinglist/archive/2011/8/25/static-files-subdomains/#9237e5b3c217b2875c59daaac4c23487 app.config['STATIC_ROOT'] = config().get_default('WEBAPP', 'static_url_path', None) def static(path): root = app.config.get('STATIC_ROOT', None) if root is None: # fallback on the normal way return url_for('static', filename=path) return urlparse.urljoin(root, path) @app.context_processor def inject_static(): return dict(static=static) if enable_profiler: from werkzeug.contrib.profiler import ProfilerMiddleware, MergeStream f = open('profiler.log', 'w') if profiler_quiet: profile_app = ProfilerMiddleware(app, f, profile_dir="/tmp") else: stream = MergeStream(sys.stdout, f) profile_app = ProfilerMiddleware(app, stream, profile_dir="/tmp") return profile_app if enable_timepro: from timepro_flask import TimeProMiddleware, MergeStream f = open('timepro.log', 'w') if profiler_quiet: profile_app = TimeProMiddleware(app, f, profile_dir="/tmp") else: stream = MergeStream(sys.stdout, f) profile_app = TimeProMiddleware(app, stream, profile_dir="/tmp") return profile_app return app