forked from cihai/unihan-etl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap_env.py
executable file
·133 lines (99 loc) · 3.3 KB
/
bootstrap_env.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
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function, \
with_statement, unicode_literals
import os
import sys
import subprocess
def warning(*objs):
print("WARNING: ", *objs, file=sys.stderr)
def fail(message):
sys.exit("Error: {message}".format(message=message))
PY2 = sys.version_info[0] == 2
if PY2:
from urllib import urlretrieve
else:
from urllib.request import urlretrieve
def has_module(module_name):
try:
import imp
imp.find_module(module_name)
del imp
return True
except ImportError:
return False
def which(exe=None, throw=True):
"""Return path of bin. Python clone of /usr/bin/which.
from salt.util - https://www.github.com/saltstack/salt - license apache
:param exe: Application to search PATHs for.
:type exe: string
:param throw: Raise ``Exception`` if not found in paths
:type throw: bool
:rtype: string
"""
if exe:
if os.access(exe, os.X_OK):
return exe
# default path based on busybox's default
default_path = '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin'
search_path = os.environ.get('PATH', default_path)
for path in search_path.split(os.pathsep):
full_path = os.path.join(path, exe)
if os.access(full_path, os.X_OK):
return full_path
message = (
'{0!r} could not be found in the following search '
'path: {1!r}'.format(
exe, search_path
)
)
if throw:
raise Exception(message)
else:
print(message)
return None
project_dir = os.path.dirname(os.path.realpath(__file__))
env_dir = os.path.join(project_dir, '.env')
pip_bin = os.path.join(env_dir, 'bin', 'pip')
python_bin = os.path.join(env_dir, 'bin', 'python')
virtualenv_bin = which('virtualenv', throw=False)
virtualenv_exists = os.path.exists(env_dir) and os.path.isfile(python_bin)
sphinx_requirements_filepath = os.path.join(project_dir, 'doc', 'requirements.pip')
try:
import virtualenv
except ImportError:
message = (
'Virtualenv is required for this bootstrap to run.\n'
'Install virtualenv via:\n'
'\t$ [sudo] pip install virtualenv'
)
fail(message)
try:
import pip
except ImportError:
message = (
'pip is required for this bootstrap to run.\n'
'Find instructions on how to install at: %s' %
'http://pip.readthedocs.org/en/latest/installing.html'
)
fail(message)
def main():
if not virtualenv_exists:
virtualenv_bin = which('virtualenv', throw=False)
subprocess.check_call(
[virtualenv_bin, env_dir]
)
subprocess.check_call(
[pip_bin, 'install', '-e', project_dir]
)
if not os.path.isfile(os.path.join(env_dir, 'bin', 'watching_testrunner')):
subprocess.check_call(
[pip_bin, 'install', 'watching-testrunner']
)
if not os.path.isfile(os.path.join(env_dir, 'bin', 'sphinx-quickstart')):
subprocess.check_call(
[pip_bin, 'install', '-r', sphinx_requirements_filepath]
)
if os.path.exists(os.path.join(env_dir, 'build')):
os.removedirs(os.path.join(env_dir, 'build'))
if __name__ == '__main__':
main()