forked from asottile/git-code-debt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
105 lines (84 loc) · 2.96 KB
/
Copy pathapp.py
File metadata and controls
105 lines (84 loc) · 2.96 KB
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
import argparse
import os.path
import shutil
import sqlite3
from typing import Optional
from typing import Sequence
from typing import TYPE_CHECKING
import flask
import pkg_resources
from git_code_debt.server.metric_config import Config
from git_code_debt.server.servlets.changes import changes
from git_code_debt.server.servlets.commit import commit
from git_code_debt.server.servlets.graph import graph
from git_code_debt.server.servlets.index import index
from git_code_debt.server.servlets.status import status
from git_code_debt.server.servlets.widget import widget
from git_code_debt.util import yaml
if TYPE_CHECKING:
from typing import NoReturn
app = flask.Flask(__name__)
app.register_blueprint(changes)
app.register_blueprint(commit)
app.register_blueprint(graph)
app.register_blueprint(index)
app.register_blueprint(status)
app.register_blueprint(widget)
class AppContext:
database_path = 'database.db'
config: Optional[Config] = None
@app.before_request
def before_request() -> None:
flask.g.config = AppContext.config
flask.g.db = sqlite3.connect(AppContext.database_path)
@app.teardown_request
def teardown_request(_: Optional[Exception]) -> None:
flask.g.config = None
flask.g.db.close()
def create_metric_config_if_not_exists() -> None:
"""Creates the sameple metric_config.yaml in the current directory if
it does not exist.
"""
if os.path.exists('metric_config.yaml'):
return
print('WARNING: no metric_config.yaml detected. Creating sample config!')
shutil.copyfile(
pkg_resources.resource_filename(
'git_code_debt.server', 'metric_config.sample.yaml',
),
'metric_config.yaml',
)
def main(argv: Optional[Sequence[str]] = None) -> 'NoReturn':
parser = argparse.ArgumentParser()
parser.add_argument('database_path', type=str)
parser.add_argument('-p', '--port', type=int, default=5000)
mutex = parser.add_mutually_exclusive_group()
mutex.add_argument(
'--debug', action='store_true',
help=(
'Run in debug mode (stacktraces + single process). '
'Not suggested for production.'
),
)
mutex.add_argument(
'--processes', type=int, default=5,
help='Number of processes, does not apply to --debug',
)
args = parser.parse_args(argv)
if not os.path.exists(args.database_path):
print(f'Not found: {args.database_path}')
print('Use git-code-debt-generate to create a database.')
raise SystemExit(1)
create_metric_config_if_not_exists()
with open('metric_config.yaml') as f:
contents = yaml.load(f)
AppContext.config = Config.from_data(contents)
AppContext.database_path = args.database_path
kwargs = {'port': args.port, 'debug': args.debug}
if not args.debug:
kwargs['processes'] = args.processes
kwargs['threaded'] = False
app.run('0.0.0.0', **kwargs)
raise SystemExit(1)
if __name__ == '__main__':
exit(main())