forked from fedora-copr/copr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
239 lines (202 loc) · 7.45 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# This is very complicated module. TODO: drop the ignores
# pylint: disable=wrong-import-order,wrong-import-position,cyclic-import
import os
import flask
from werkzeug.routing import RequestRedirect
from flask_sqlalchemy import SQLAlchemy
from contextlib import contextmanager
try:
from flask_caching import Cache
except ImportError:
from flask_cache import Cache
from flask_openid import OpenID
from flask_whooshee import Whooshee
from openid_teams.teams import TeamsResponse
from coprs.redis_session import RedisSessionInterface
from coprs.request import get_request_class
app = flask.Flask(__name__)
if "COPRS_ENVIRON_PRODUCTION" in os.environ:
app.config.from_object("coprs.config.ProductionConfig")
elif "COPRS_ENVIRON_UNITTEST" in os.environ:
app.config.from_object("coprs.config.UnitTestConfig")
else:
app.config.from_object("coprs.config.DevelopmentConfig")
if os.environ.get("COPR_CONFIG"):
app.config.from_envvar("COPR_CONFIG")
else:
app.config.from_pyfile("/etc/copr/copr.conf", silent=True)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
oid = OpenID(
app, app.config["OPENID_STORE"],
safe_roots=[],
extension_responses=[TeamsResponse]
)
db = SQLAlchemy(app)
@contextmanager
def db_session_scope():
"""Provide a transactional scope around a series of operations."""
session = db.session
try:
yield session
session.commit()
except Exception as err:
session.rollback()
raise
whooshee = Whooshee(app)
profiler_enabled = bool(app.config.get('PROFILER', False))
try:
# needs to be installed using pip3
import flask_profiler
except ImportError:
# This is intended to not be presented. Even on most devel setups.
profiler_enabled = False
def setup_profiler(flask_app, enabled):
""" This creates /flask-profiler/ route """
flask_app.config["flask_profiler"] = {
"enabled": enabled,
"storage": {
"engine": "sqlite",
"FILE": "/tmp/profiler.sqlite",
},
"basicAuth":{
"enabled": True,
"username": "admin",
"password": "admin"
},
"ignore": [
"^/static/.*"
]
}
if enabled:
flask_profiler.init_app(flask_app)
import coprs.filters
import coprs.log
from coprs.log import setup_log
import coprs.whoosheers
from coprs.helpers import RedisConnectionProvider
rcp = RedisConnectionProvider(config=app.config)
app.session_interface = RedisSessionInterface(rcp.get_connection())
cache_rcp = RedisConnectionProvider(config=app.config, db=1)
cache = Cache(app, config={
'CACHE_REDIS_HOST': cache_rcp.host,
'CACHE_REDIS_PORT': cache_rcp.port,
})
app.cache = cache
# We don't want to manually define all the routes twice, once with a trailing
# slash and one without it.
app.url_map.strict_slashes = False
app.request_class = get_request_class(app)
# Tell flask-restx to not append generated suggestions at
# the end of 404 error messages
app.config["ERROR_404_HELP"] = False
# Don't display X-Fields inputs in Swagger
app.config["RESTX_MASK_SWAGGER"] = False
# There are some models, that are not used yet. They might still be helpful
# for the users.
app.config["RESTX_INCLUDE_ALL_MODELS"] = True
from coprs.views import admin_ns
from coprs.views.admin_ns import admin_general
from coprs.views import api_ns
from coprs.views.api_ns import api_general
from coprs.views import apiv3_ns
from coprs.views.apiv3_ns import (
apiv3_general, apiv3_builds, apiv3_packages, apiv3_projects,
apiv3_project_chroots, apiv3_modules, apiv3_build_chroots,
apiv3_mock_chroots, apiv3_permissions, apiv3_webhooks, apiv3_monitor,
apiv3_rpmrepo,
)
from coprs.views import batches_ns
from coprs.views.batches_ns import coprs_batches
from coprs.views import coprs_ns
from coprs.views.coprs_ns import coprs_builds
from coprs.views.coprs_ns import coprs_general
from coprs.views.coprs_ns import coprs_chroots
from coprs.views.coprs_ns import coprs_packages
from coprs.views.coprs_ns import pagination_redirect
from coprs.views import backend_ns
from coprs.views.backend_ns import backend_general
from coprs.views import misc
from coprs.views import status_ns
from coprs.views.status_ns import status_general
from coprs.views import recent_ns
from coprs.views.recent_ns import recent_general
from coprs.views.stats_ns import stats_receiver
from coprs.views import tmp_ns
from coprs.views.tmp_ns import tmp_general
from coprs.views.groups_ns import groups_ns
from coprs.views.groups_ns import groups_general
from coprs.views.user_ns import user_ns
from coprs.views.user_ns import user_general
from coprs.views.webhooks_ns import webhooks_ns
from coprs.views.webhooks_ns import webhooks_general
from coprs.views.rss_ns import rss_ns
from coprs.views.rss_ns import rss_general
from coprs.exceptions import (
AccessRestricted,
BadRequest,
CoprHttpException,
ConflictingRequest,
MalformedArgumentException,
ObjectNotFound,
NonAdminCannotCreatePersistentProject,
NonAdminCannotDisableAutoPrunning,
)
from coprs.views.explore_ns import explore_ns
from coprs.error_handlers import get_error_handler, RestXErrorHandler
import coprs.context_processors
with app.app_context():
setup_log()
app.register_blueprint(api_ns.api_ns)
app.register_blueprint(apiv3_ns.apiv3_ns)
app.register_blueprint(admin_ns.admin_ns)
app.register_blueprint(batches_ns.batches_ns)
app.register_blueprint(coprs_ns.coprs_ns)
app.register_blueprint(misc.misc)
app.register_blueprint(backend_ns.backend_ns)
app.register_blueprint(status_ns.status_ns)
app.register_blueprint(recent_ns.recent_ns)
app.register_blueprint(stats_receiver.stats_rcv_ns)
app.register_blueprint(tmp_ns.tmp_ns)
app.register_blueprint(groups_ns)
app.register_blueprint(user_ns)
app.register_blueprint(webhooks_ns)
app.register_blueprint(rss_ns)
app.register_blueprint(explore_ns)
if app.config["MEMORY_ANALYZER"]:
from coprs.views.memory_analyzer import memory_analyzer
app.register_blueprint(memory_analyzer)
app.add_url_rule("/", "coprs_ns.coprs_show", coprs_general.coprs_show)
@app.errorhandler(RequestRedirect)
def handle_request_redirect(error):
return error.get_response(None)
@app.errorhandler(Exception)
def handle_exceptions(error):
error_handler = get_error_handler()
return error_handler.handle_error(error)
@apiv3_ns.api.errorhandler(Exception)
def handle_exceptions_api(error):
"""
Whenever an exception is raised within an API endpoint which is managed by
flask-restx, we end up here.
"""
handler = RestXErrorHandler()
body, code, headers = handler.handle_error(error)
return body, code, headers or {}
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
setup_profiler(app, profiler_enabled)
from flask_sqlalchemy import models_committed
models_committed.connect(coprs.whoosheers.CoprWhoosheer.on_commit, sender=app)
# Serve static files from system-wide RPM files
@app.route('/system_static/<component>/<path:filename>')
@app.route('/system_static/<path:filename>')
def system_static(filename, component=""):
"""
:param component: name of the javascript component provided by a RPM package
do not confuse with a name of the RPM package itself
(e.g. 'jquery' component is provided by 'js-jquery' package)
:param filename: path to a file relative to the component root directory
:return: content of a static file
"""
path = os.path.join("/usr/share/javascript", component)
return flask.send_from_directory(path, filename)