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
|
#!/usr/bin/env python
import os
import pkgutil
import random
import sys
from getpass import getpass
from flask_script import Manager, Command, Option
from flask_migrate import Migrate, MigrateCommand
from jinja2 import Template
from werkzeug.security import generate_password_hash
from ihatemoney.run import create_app
from ihatemoney.models import db
class GeneratePasswordHash(Command):
"""Get password from user and hash it without printing it in clear text."""
def run(self):
password = getpass(prompt='Password: ')
print(generate_password_hash(password))
class ConfigTemplate(Command):
def get_options(self):
return [
Option('config_file', choices=[
'ihatemoney.cfg',
'apache-vhost.conf',
'gunicorn.conf.py',
'supervisord.conf',
'nginx.conf',
]),
]
@staticmethod
def gen_secret_key():
return ''.join([
random.SystemRandom().choice(
'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
for i in range(50)])
def run(self, config_file):
template_content = pkgutil.get_data(
'ihatemoney',
os.path.join('conf-templates/', config_file) + '.j2'
).decode('utf-8')
bin_path = os.path.join(os.path.dirname(sys.executable))
print(Template(template_content).render(
pkg_path=os.path.abspath(os.path.dirname(__file__)),
bin_path=bin_path,
secret_key=self.gen_secret_key(),
))
def main():
QUIET_COMMANDS = ('generate_password_hash', 'generate-config')
backup_stderr = sys.stderr
# Hack to divert stderr for commands generating content to stdout
# to avoid confusing the user
if len(sys.argv) > 1 and sys.argv[1] in QUIET_COMMANDS:
sys.stderr = open(os.devnull, 'w')
app = create_app()
Migrate(app, db)
# Restore stderr (among other: to be able to display help)
sys.stderr = backup_stderr
manager = Manager(app)
manager.add_command('db', MigrateCommand)
manager.add_command('generate_password_hash', GeneratePasswordHash)
manager.add_command('generate-config', ConfigTemplate)
manager.run()
if __name__ == '__main__':
main()
|