forked from gratipay/gratipay.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gratipay.py
executable file
·122 lines (87 loc) · 2.98 KB
/
gratipay.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
#!/usr/bin/env python
"""\
Gratipay
~~~~~~
A personal funding platform.
Dependencies:
- Python 2.7
- Postgresql 9.2
To run:
$ gratipay.py
This will also initialize a local environment on the first run.
"""
import os
import sys
import shutil
from subprocess import check_call, check_output, STDOUT, CalledProcessError
is_win = sys.platform.startswith('win')
bin_dir = 'Scripts' if is_win else 'bin'
ext = '.exe' if is_win else ''
default_port = 8537
vendor_path = 'vendor'
env_path = 'env'
requirements_installed_path = os.path.join(env_path, '.requirements_installed')
bin_path = os.path.join(env_path, bin_dir)
default_config_path = 'default_local.env'
config_path = 'local.env'
virtualenv_path = os.path.join(vendor_path, 'virtualenv-12.0.7.py')
pip_path = os.path.join(bin_path, 'pip' + ext)
swaddle_path = os.path.join(bin_path, 'swaddle' + ext)
aspen_path = os.path.join(bin_path, 'aspen' + ext)
def main():
# TODO: Handle command-line arguments to override default config values
# e.g. the address and port to serve on, whether to run tests, etc
try:
bootstrap_environment()
except CalledProcessError as ex:
print ex.output
except EnvironmentError as ex:
print 'Error:', ex
return 1
run_server()
def bootstrap_environment():
ensure_dependencies()
init_config()
init_virtualenv()
install_requirements()
def ensure_dependencies():
if not shell('python', '--version', capture=True).startswith('Python 2.7'):
raise EnvironmentError('Python 2.7 is required.')
try:
shell('pg_config' + ext, capture=True)
except OSError as e:
if e.errno != os.errno.ENOENT:
raise
raise EnvironmentError('Postgresql is required. (Make sure pg_config is on your PATH.)')
def init_config():
if os.path.exists(config_path):
return
print 'Creating a %s file...' % config_path
shutil.copyfile(default_config_path, config_path)
def init_virtualenv():
if os.path.exists(env_path):
return
print 'Initializing virtualenv at %s...' % env_path
shell('python', virtualenv_path,
'--prompt="[gratipay] "',
'--extra-search-dir=' + vendor_path,
'--always-copy',
env_path)
def install_requirements():
# TODO: Detect when requirements.txt changes instead of checking for a file
if os.path.exists(requirements_installed_path):
return
print 'Installing requirements...'
shell(pip_path, 'install', '-r', 'requirements.txt')
shell(pip_path, 'install', os.path.join(vendor_path, 'nose-1.1.2.tar.gz'))
shell(pip_path, 'install', '-e', '.')
open(requirements_installed_path, 'w').close()
def run_server():
# TODO: Wait for Aspen to quit before exiting
shell(swaddle_path, config_path, aspen_path)
def shell(*args, **kwargs):
if kwargs.pop('capture', None):
return check_output(args, stderr=STDOUT, **kwargs)
return check_call(args, **kwargs)
if __name__ == '__main__':
sys.exit(main())