forked from fedora-infra/bodhi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.py
executable file
·205 lines (173 loc) · 6.68 KB
/
bootstrap.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
#!/usr/bin/env python
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
""" This script can bootstrap either a python2 or a python3 environment.
The environments it generates are named by the python version they are built
against. i.e.: bodhi-python2.7 or bodhi-python3.2.
To generate one or the other, just use the relevant python binary. For a
python2 env, run::
python2 bootstrap.py
And for a python3 env, run::
python3 bootstrap.py
"""
import subprocess
import shutil
import glob
import sys
import os
ENVS = os.path.expanduser('~/.virtualenvs')
VENV = 'bodhi-python{major}.{minor}'.format(
major=sys.version_info[0],
minor=sys.version_info[1],
)
VENVWRAPPER = os.path.exists('/usr/bin/virtualenvwrapper.sh')
if not VENVWRAPPER:
ENVS = os.getcwd()
if "check_output" not in dir( subprocess ): # duck punch it in!
def f(*popenargs, **kwargs):
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise subprocess.CalledProcessError(retcode, cmd)
return output
subprocess.check_output = f
def _link_system_lib(lib):
workon = '.'
if VENVWRAPPER:
workon=os.getenv("WORKON_HOME")
for libdir in ('lib', 'lib64'):
location = '{libdir}/python{major}.{minor}/site-packages'.format(
major=sys.version_info[0], minor=sys.version_info[1],
libdir=libdir)
if not os.path.exists(os.path.join('/usr', location, lib)):
if os.path.exists(os.path.join('/usr', location, lib + '.so')):
lib += '.so'
elif os.path.exists(os.path.join('/usr', location, lib + '.py')):
lib += '.py'
else:
continue
template = 'ln -s /usr/{location}/{lib} {workon}/{venv}/{location}/'
# Link in the egg-info for Pillow
if lib == 'PIL':
egginfo = glob.glob(os.path.join('/usr', location, 'Pillow-*.egg-info'))
if egginfo:
print("Linking in Pillow egg-info")
cmd = template.format(
location=location, venv=VENV, lib=os.path.basename(egginfo[0]), workon=workon)
try:
subprocess.check_output(cmd.split())
except subprocess.CalledProcessError as e:
# File already linked.
return e.returncode == 256
print("Linking in global module: %s" % lib)
cmd = template.format(
location=location, venv=VENV, lib=lib, workon=workon)
try:
subprocess.check_output(cmd.split())
return True
except subprocess.CalledProcessError as e:
# File already linked.
return e.returncode == 256
print("Cannot find global module %s" % lib)
def link_system_libs():
for mod in ('koji', 'rpm', 'OpenSSL', 'urlgrabber', 'pycurl',
'rpmUtils', 'sqlitecachec', '_sqlitecache', 'psycopg2',
'krbVmodule', 'deltarpm', '_deltarpmmodule',
'fedora_cert', 'libxml2', 'libxml2mod', 'librepo', 'createrepo_c',
'dnf', 'libcomps', 'gpgme', 'lzma', 'iniparse', 'hawkey',
'yum'):
_link_system_lib(mod)
def _do_virtualenvwrapper_command(cmd):
""" This is tricky, because all virtualenwrapper commands are
actually bash functions, so we can't call them like we would
other executables.
"""
print("Trying '%s'" % cmd)
cmds = cmd.split(' ')
if VENVWRAPPER:
cmds = ['bash', '-c', '. /usr/bin/virtualenvwrapper.sh; %s' % cmd]
out, err = subprocess.Popen(
cmds, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
).communicate()
print(out)
print(err)
def rebuild():
""" Completely destroy and rebuild the virtualenv. """
try:
cmd = 'rm -rf %s' % VENV
if VENVWRAPPER:
cmd = 'rmvirtualenv %s' % VENV
_do_virtualenvwrapper_command(cmd)
except Exception as e:
print(unicode(e))
cmd = 'virtualenv --no-site-packages -p /usr/bin/python{major}.{minor} {v}'\
.format(
major=sys.version_info[0],
minor=sys.version_info[1],
v=VENV,
)
if VENVWRAPPER:
cmd = 'mkvirtualenv --no-site-packages -p /usr/bin/python{major}.{minor} {v}'\
.format(
major=sys.version_info[0],
minor=sys.version_info[1],
v=VENV,
)
_do_virtualenvwrapper_command(cmd)
# Do two things here:
# - remove all *.pyc that exist in srcdir.
# - remove all data/templates dirs that exist (mako caches).
for base, dirs, files in os.walk(os.getcwd()):
for fname in files:
if fname.endswith(".pyc"):
os.remove(os.path.sep.join([base, fname]))
if base.endswith('data/templates'):
shutil.rmtree(base)
def setup_develop():
""" `python setup.py develop` in our virtualenv """
workon = '.'
if VENVWRAPPER:
workon=os.getenv("WORKON_HOME")
cmd = '{workon}/{env}/bin/python setup.py develop'.format(
envs=ENVS, env=VENV, workon=workon)
print(cmd)
subprocess.call(cmd.split())
def install_test_deps():
"""
To work around `python setup.py test` downloadling egg files to the current
directory
"""
workon = '.'
if VENVWRAPPER:
workon=os.getenv("WORKON_HOME")
cmd = '{workon}/{env}/bin/pip install nose-cov webtest mock'.format(
envs=ENVS, env=VENV, workon=workon)
print(cmd)
subprocess.call(cmd.split())
if __name__ == '__main__':
print("Bootstrapping bodhi...")
rebuild()
# TODO - yum install
# - pcaro-hermit
# - freetype-devel
# - libjpeg-turbo-devel
link_system_libs()
setup_develop()
install_test_deps()