-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathupdate_libtbx_env.py
221 lines (187 loc) · 6.86 KB
/
update_libtbx_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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Script to copy and update libtbx_env contents
Usage: libtbx.python update_libtbx_env.py
"""
from __future__ import absolute_import, division, print_function
import argparse
import os
import shutil
import sys
from libtbx.path import absolute_path
# =============================================================================
def get_prefix_dir():
'''
Function that returns $PREFIX or sys.prefix
Parameters
----------
None
Returns
-------
path to prefix
'''
prefix_dir = os.getenv('PREFIX')
if prefix_dir is None:
prefix_dir = sys.prefix
if sys.platform == 'darwin':
if 'python.app' in prefix_dir:
prefix_dir = prefix_dir.split('python.app')[0]
elif sys.platform == 'win32':
prefix_dir = os.path.join(prefix_dir, 'Library')
return prefix_dir
# =============================================================================
def get_default_dir():
'''
Function that returns the default location of libtbx_env in an installation.
The default location is ${PREFIX}/share/cctbx
Parameters
----------
None
Returns
-------
path of the default location
'''
prefix_dir = get_prefix_dir()
default_dir = os.path.join(prefix_dir, 'share', 'cctbx')
return default_dir
# =============================================================================
def copy_libtbx_env(default_dir=None):
'''
Function that copies libtbx_env from $LIBTBX_BUILD to $PREFIX
If $LIBTBX_BUILD is not set, no copy is done. If libtbx_env does not
exist, an IOError is raised.
Parameters
----------
default_dir: str
The directory to copy libtbx_env
Returns
-------
path or None: if the file is copied, the newly created path is returned
'''
value = None
if default_dir is None:
default_dir = get_default_dir()
if os.getenv('LIBTBX_BUILD') is not None:
src = os.path.join(os.getenv('LIBTBX_BUILD'), 'libtbx_env')
dst = os.path.join(default_dir, 'libtbx_env')
if not os.path.isfile(src):
raise IOError(
'The "libtbx_env" file does not exist in {src}.'.format(src=src))
if not os.path.exists(default_dir):
# assumes that only the last level needs to be created.
os.mkdir(default_dir)
value = shutil.copy(src, dst)
return value
# =============================================================================
def update_libtbx_env(default_dir=None):
'''
Function that updates libtbx_env so that modules can be loaded from
standard locations in $PREFIX
Parameters
----------
default_dir: str
The directory to copy libtbx_env
Returns
-------
None
'''
# unset LIBTBX_BUILD and load libtbx_env from $PREFIX
if os.getenv('LIBTBX_BUILD') is not None:
del os.environ['LIBTBX_BUILD']
import libtbx.load_env
if default_dir is None:
default_dir = get_default_dir()
sys_prefix = get_prefix_dir()
# basic path changes
env = libtbx.env
env.build_path = absolute_path(sys_prefix)
env.set_derived_paths()
env.exe_path = env.bin_path
env.pythonpath = list()
sys_executable = sys.executable
if sys.platform == 'darwin' and 'python.app' not in sys_executable:
sys_executable = os.path.join(sys_prefix, 'python.app', 'Contents', 'MacOS', 'python')
env.python_exe = env.as_relocatable_path(sys_executable)
env.no_bin_python = True
site_packages_path = None
for path in sys.path:
if path.endswith('site-packages'):
site_packages_path = env.as_relocatable_path(path)
break
relocatable_sys_prefix = env.as_relocatable_path(sys_prefix)
env.repository_paths = [relocatable_sys_prefix, site_packages_path]
env.scons_dist_path = relocatable_sys_prefix
# libtbx.python dispatcher
env._write_dispatcher_in_bin(
source_file=env.python_exe,
target_file='libtbx.python',
source_is_python_exe=True)
# update module locations
if sys.platform == 'win32':
sys_prefix = get_prefix_dir() # has an extra "Library"
relocatable_sys_prefix = env.as_relocatable_path(
os.path.join(sys_prefix, '..', 'Lib', 'site-packages'))
for name in env.module_dict:
module = env.module_dict[name]
new_paths = [relocatable_sys_prefix, relocatable_sys_prefix]
for path in sys.path:
check_this_path = path.startswith(sys_prefix)
if sys.platform == 'win32':
check_this_path = path.lower().startswith(abs(relocatable_sys_prefix).lower())
if check_this_path:
new_path = os.path.join(path, name)
if os.path.isdir(new_path):
new_paths[0] = env.as_relocatable_path(new_path)
new_paths[1] = env.as_relocatable_path(new_path + '_' + module.mate_suffix)
break
if module.name == 'boost' and os.path.isdir(os.path.join(path, 'boost_adaptbx')):
new_paths[1] = env.as_relocatable_path(new_path + '_' + module.mate_suffix)
break
if module.name == 'annlib' and os.path.isdir(os.path.join(path, 'annlib_adaptbx')):
new_paths[0] = None
new_paths[1] = env.as_relocatable_path(new_path + '_' + module.mate_suffix)
break
if module.name == 'phaser_voyager' and os.path.isdir(os.path.join(path, 'New_Voyager')):
new_paths[0] = env.as_relocatable_path(os.path.join(path, 'New_Voyager'))
new_paths[1] = None
break
dist_paths = module.dist_paths
for i, path in enumerate(dist_paths):
if path is not None:
module.dist_paths[i] = new_paths[i]
env.module_dist_paths[name] = new_paths[0]
if name == 'boost':
env.module_dist_paths[name] = relocatable_sys_prefix / 'include'
name_adaptbx = name + '_' + module.mate_suffix
if name_adaptbx in env.module_dist_paths:
env.module_dist_paths[name_adaptbx] = new_paths[1]
if name == 'libtbx':
env.path_utility = env.as_relocatable_path(
os.path.join(abs(new_paths[0]), 'command_line', 'path_utility.py'))
# update dispatchers
env.reset_dispatcher_bookkeeping()
env.write_python_and_show_path_duplicates()
for module in env.module_list:
module.process_command_line_directories()
# repickle
env.build_path = absolute_path(default_dir)
env.installed = True
env.pickle()
return 0
# =============================================================================
def run():
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
# default location is ${PREFIX}/share/cctbx
default_dir = get_default_dir()
parser.add_argument(
'--default-dir', '--default_dir', default=default_dir, type=str,
help="""The new default for the location of libtbx_env. By default,
the new location is ${PREFIX}/share/cctbx. This feature is not
fully supported yet.""")
namespace = parser.parse_args()
copy_libtbx_env(default_dir=namespace.default_dir)
update_libtbx_env(default_dir=namespace.default_dir)
return 0
# =============================================================================
if __name__ == '__main__':
sys.exit(run())