forked from ReactionMechanismGenerator/RMG-Py
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
299 lines (249 loc) · 11.1 KB
/
utilities.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
# -*- coding: utf-8 -*-
###############################################################################
# #
# RMG - Reaction Mechanism Generator #
# #
# Copyright (c) 2002-2018 Prof. William H. Green ([email protected]), #
# Prof. Richard H. West ([email protected]) and the RMG Team ([email protected]) #
# #
# Permission is hereby granted, free of charge, to any person obtaining a #
# copy of this software and associated documentation files (the 'Software'), #
# to deal in the Software without restriction, including without limitation #
# the rights to use, copy, modify, merge, publish, distribute, sublicense, #
# and/or sell copies of the Software, and to permit persons to whom the #
# Software is furnished to do so, subject to the following conditions: #
# #
# The above copyright notice and this permission notice shall be included in #
# all copies or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
# DEALINGS IN THE SOFTWARE. #
# #
###############################################################################
"""
This module contains utilities related to compilation and code maintenance.
"""
from __future__ import print_function
import argparse
import os
import platform
import re
import subprocess
def check_dependencies():
"""
Checks for and locates major dependencies that RMG requires.
"""
missing = install_rdkit = False
print('\nChecking vital dependencies...\n')
print('{0:<15}{1:<15}{2}'.format('Package', 'Version', 'Location'))
# Check for symmetry
try:
result = subprocess.check_output('symmetry -h', stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError:
print('{0:<30}{1}'.format('symmetry', 'Not found. Please install in order to use QM.'))
missing = True
else:
match = re.search(r'\$Revision: (\S*) \$', result)
version = match.group(1)
if platform.system() == 'Windows':
location = subprocess.check_output('where symmetry', shell=True)
else:
location = subprocess.check_output('which symmetry', shell=True)
print('{0:<15}{1:<15}{2}'.format('symmetry', version, location.strip()))
# Check for RDKit
try:
import rdkit
from rdkit import Chem
except ImportError:
print('{0:<30}{1}'.format('RDKit',
'Not found. Please install RDKit version 2015.03.1 or later with InChI support.'))
missing = install_rdkit = True
else:
try:
version = rdkit.__version__
except AttributeError:
version = False
location = rdkit.__file__
inchi = Chem.inchi.INCHI_AVAILABLE
if version:
print('{0:<15}{1:<15}{2}'.format('RDKit', version, location))
if not inchi:
print(' !!! RDKit installed without InChI Support. Please install with InChI.')
missing = install_rdkit = True
else:
print(' !!! RDKit version out of date, please install RDKit version 2015.03.1 or later with InChI support.')
missing = install_rdkit = True
# Check for OpenBabel
try:
import openbabel
except ImportError:
print('{0:<30}{1}'.format('OpenBabel',
'Not found. Necessary for SMILES/InChI functionality for nitrogen compounds.'))
missing = True
else:
version = openbabel.OBReleaseVersion()
location = openbabel.__file__
print('{0:<15}{1:<15}{2}'.format('OpenBabel', version, location))
# Check for lpsolve
try:
import lpsolve55
except ImportError:
print('{0:<30}{1}'.format('lpsolve55',
'Not found. Necessary for generating Clar structures for aromatic species.'))
missing = True
else:
location = lpsolve55.__file__
print('{0:<30}{1}'.format('lpsolve55', location))
# Check for pyrdl
try:
import py_rdl
except ImportError:
print('{0:<30}{1}'.format('pyrdl', 'Not found. Necessary for ring perception algorithms.'))
missing = True
else:
location = py_rdl.__file__
print('{0:<30}{1}'.format('pyrdl', location))
if missing:
print("""
There are missing dependencies as listed above. Please install them before proceeding.
Using Anaconda, these dependencies can be individually installed from the RMG channel as follows:
conda install -c rmg [package name]
{0}
You can alternatively update your environment and install all missing dependencies as follows:
conda env update -f environment_[linux/mac/windows].yml # Choose the correct file for your OS
Be sure to activate your conda environment (rmg_env by default) before installing or updating.
""".format("""
RDKit should be installed from the RDKit channel instead:
conda install -c rdkit rdkit
""" if install_rdkit else ''))
else:
print("""
Everything was found :)
""")
def clean(subdirectory=''):
"""
Removes files generated during compilation.
For *nix systems, remove `.so` files and `.pyc` files.
For Windows, remove `.pyd` files and `.pyc` files.
"""
if platform.system() == 'Windows':
extensions = ['.pyd', '.pyc']
else:
extensions = ['.so', '.pyc']
directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), subdirectory)
for root, dirs, files in os.walk(directory):
for f in files:
ext = os.path.splitext(f)[1]
if ext in extensions:
os.remove(os.path.join(root, f))
def update_headers():
"""
Automatically update the headers for *.py, *.pyx, and *.pxd files
in RMG-Py/rmgpy, RMG-Py/scripts, and the root RMG-Py directory.
Other directories (e.g., examples, documentation, etc.) are ignored
along with directories containing test data.
The headers are automatically generated based on the LICENSE.txt file.
Typical usage would involve running this script after updating the
copyright date in the license file.
Because this script makes assumptions regarding the contents at the
start of each file, be sure to double-check the results to make sure
important lines aren't accidentally overwritten.
"""
shebang = """#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
header = """###############################################################################
# #
# RMG - Reaction Mechanism Generator #
# #
"""
with open('LICENSE.txt', 'r') as f:
for line in f:
line = line.strip()
newline = '# {0:<75} #\n'.format(line)
header += newline
header += """# #
###############################################################################
"""
print(header)
def replace_header(oldfile):
newfile = os.path.join('tmp', 'tempfile')
ext = os.path.splitext(oldfile)[1]
if ext == '.py':
py_file = True
elif ext == '.pyx' or ext == '.pxd':
py_file = False
else:
raise Exception('Unexpected file type: {0}'.format(oldfile))
with open(oldfile, 'r') as old, open(newfile, 'w+') as new:
# Write shebang and encoding for python files only
if py_file:
new.write(shebang)
# Read old file and copy over contents
found_bar = False
first_line = True
start = False
for i, line in enumerate(old):
if i == 0 and line[0] != '#':
# Assume there's no header, so start copying lines right away
new.write(header)
start = True
if start:
if first_line and line.strip() != '':
new.write('\n')
first_line = False
new.write(line)
elif line.startswith('# cython:'):
# Copy over any cython directives
new.write(line)
new.write('\n')
elif line.startswith('##########'):
if found_bar:
# We've reached the end of the license, so start copying lines starting with the next line
new.write(header)
start = True
else:
found_bar = True
# Replace old file with new file
os.rename(newfile, oldfile)
# Create temporary directory for writing files
if not os.path.exists('tmp'):
os.makedirs('tmp')
# Compile list of files to modify
filelist = ['rmg.py', 'arkane.py', 'setup.py']
root_dirs = ['rmgpy', 'arkane', 'scripts']
for root_dir in root_dirs:
for root, dirs, files in os.walk(root_dir):
if 'test_data' in root or 'files' in root or '/tools/data' in root or 'arkane/data' in root:
print('Skipping ' + root)
continue
print(root)
for f in files:
ext = os.path.splitext(f)[1]
if ext in ['.py', '.pyx', '.pxd']:
filelist.append(os.path.join(root, f))
for f in filelist:
print('Updating {0} ...'.format(f))
replace_header(f)
# Remove temporary directory
os.rmdir('tmp')
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='RMG Code Utilities')
parser.add_argument('command', metavar='COMMAND', type=str,
choices=['check-dependencies', 'clean', 'clean-solver', 'update-headers'],
help='command to execute')
args = parser.parse_args()
if args.command == 'check-dependencies':
check_dependencies()
elif args.command == 'clean':
clean()
elif args.command == 'clean-solver':
clean('rmgpy/solver')
elif args.command == 'update-headers':
update_headers()