forked from CellProfiler/CellProfiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_dependencies.py
334 lines (292 loc) · 12.2 KB
/
external_dependencies.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
"""
CellProfiler is distributed under the GNU General Public License.
See the accompanying file LICENSE for details.
Copyright (c) 2003-2009 Massachusetts Institute of Technology
Copyright (c) 2009-2015 Broad Institute
All rights reserved.
Please see the AUTHORS file for credits.
Website: http://www.cellprofiler.org
"""
# This file allows developers working in the git repository to fetch
# binary files from SVN (or other site) so that the git repository
# doesn't have to track large files.
import logging
logger = logging.getLogger(__package__)
import re
import os.path
import hashlib
import urllib2
import shutil
import subprocess
import sys
import traceback
import zipfile
#From https://gist.github.com/edufelipe/1027906
def check_output(*popenargs, **kwargs):
r"""Run command with arguments and return its output as a byte string.
Backported from Python 2.7 as it's implemented as pure python on stdlib.
>>> check_output(['/usr/bin/python', '--version'])
Python 2.6.2
"""
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]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
raise error
return output
ACTION_MAVEN = "Maven"
CELLPROFILER_DEPENDENCIES_URL = \
'http://www.cellprofiler.org/linked_files/CellProfilerDependencies'
OMERO_CLIENTS_URL = CELLPROFILER_DEPENDENCIES_URL + '/OMERO.clients-5.0.0-ice35-b19'
# The list of files (relative path) to fetch, their SHA1, and their source URL.
files = [
[['imagej', 'apache-maven-3.0.4-bin.zip'],
'29cfd351206016b67dd0d556098513d2b259c69b',
CELLPROFILER_DEPENDENCIES_URL + '/apache-maven-3.0.4-bin.zip',
ACTION_MAVEN],
]
pom_folders = ["java"]
classpath_filenames = ("cellprofiler-java-dependencies-classpath.txt",)
def filehash(filename):
sha1 = hashlib.sha1()
try:
f = open(filename, 'rb')
for chunk in iter(lambda: f.read(8192), ''):
sha1.update(chunk)
return sha1.hexdigest()
except:
return ''
def fetchfile(filename, url):
print "fetching %s to %s"%(url, filename)
# no try/except, it's wrapped below, and we just fail and whine to the user.
path = os.path.split(filename)[0]
if not os.path.isdir(path):
os.makedirs(path)
src = urllib2.urlopen(url)
dest = open(filename, 'wb')
shutil.copyfileobj(src, dest)
def get_cellprofiler_root_dir():
if __name__ == "__main__":
root = os.path.abspath(os.curdir)
else:
root = os.path.abspath(os.path.split(__file__)[0])
return root
def get_maven_install_path():
'''Return the location of the Maven install'''
root = get_cellprofiler_root_dir()
return os.path.join(root, 'imagej', 'maven')
def fetch_external_dependencies(overwrite=False):
# look for each file, check its hash, download if missing, or out
# of date if overwrite==True, complain if it fails. If overwrite
# is 'fail', die on mismatches hashes.
root = get_cellprofiler_root_dir()
for path, hash, url, action in files:
path = os.path.join(root, *path)
try:
assert os.path.isfile(path)
if overwrite == True:
assert filehash(path) == hash
else:
if filehash(path) != hash:
sys.stderr.write("Warning: hash of depenency %s does not match expected value.\n"%(path))
if overwrite == 'fail':
raise RuntimeError('Mismatched hash for %s'%(path))
continue
except AssertionError, e:
# fetch the file
try:
fetchfile(path, url)
assert os.path.isfile(path)
assert filehash(path) == hash, 'Hashes do not match!'
if action == ACTION_MAVEN:
install_maven(path)
except:
sys.stderr.write(traceback.format_exc())
sys.stderr.write("Could not fetch external binary dependency %s from %s. Some functionality may be missing. You might try installing it by hand.\n"%(path, url))
if overwrite == 'fail':
return
logging.info("Updating Java dependencies using Maven.")
for pom_folder in pom_folders:
pom_dir = os.path.join(root, pom_folder)
try:
try:
if check_maven_repositories(pom_dir):
aggressive_update = overwrite
else:
aggressive_update = None
except:
# check_maven_repositories runs with the -o switch to prevent it
# from going to the Internet. If the local repository doesn't
# have all the necessary pieces, mvn returns an error code
# and check_output throws to here.
#
# Tell run_maven to update aggressively
aggressive_update = True
if overwrite:
run_maven(pom_dir,
goal="clean",
aggressive_update = aggressive_update)
run_maven(pom_dir,
quiet = not overwrite,
run_tests = overwrite,
aggressive_update = aggressive_update)
except:
sys.stderr.write(traceback.format_exc())
sys.stderr.write("Maven failed to update Java dependencies.\n")
if not overwrite:
sys.stderr.write("Run external_dependencies with the -o switch to get full output.\n")
def install_maven(zipfile_path):
'''Install the Maven jars from a zipfile
zipfile_path - path to the zipfile
zip_jar_path - path to the jar files within the zip file
jar_path - destination for the jar files
'''
install_path = get_maven_install_path()
zf = zipfile.ZipFile(zipfile_path)
zf.extractall(install_path)
if sys.platform != 'win32':
import stat
executeable_path = get_mvn_executable_path(install_path)
os.chmod(executeable_path,
stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR|
stat.S_IRGRP | stat.S_IXGRP|
stat.S_IROTH | stat.S_IXOTH)
def get_mvn_executable_path(maven_install_path):
subdir = reduce(max, [x for x in os.listdir(maven_install_path)
if x.startswith('apache-maven')])
if sys.platform == 'win32':
executeable = 'mvn.bat'
else:
executeable = 'mvn'
executeable_path = os.path.join(maven_install_path, subdir, 'bin',
executeable)
return executeable_path
def run_maven(pom_path, goal="package",
quiet=False, run_tests=True, aggressive_update = True,
return_stdout = False, additional_args = []):
'''Run a Maven pom to install all of the needed jars
pom_path - the directory hosting the Maven POM
goal - the maven goal. "package" is the default which is pretty much
"do whatever the POM was built to do"
quiet - feed Maven the -q switch if true to make it run in quiet mode
run_tests - if False, set the "skip tests" maven flag. This is appropriate
if you're silently building a known-good downloaded source.
aggressive_update - if True, use the -U switch to make Maven go to the
internet and check for updates. If False, default behavior.
If None, use the -o switch to prevent Maven from any online
updates.
return_stdout - redirect stdout to capture a string and return it if True,
dump Maven output to console if False
additional_args - additional arguments for the command-line
Runs mvn package on the POM
'''
from javabridge.locate import find_jdk
maven_install_path = get_maven_install_path()
jdk_home = find_jdk()
env = os.environ.copy()
if jdk_home is not None:
env["JAVA_HOME"] = jdk_home
executeable_path = get_mvn_executable_path(maven_install_path)
args = [executeable_path]
if aggressive_update:
args.append("-U")
elif aggressive_update is None:
args.append("-o")
if quiet:
args.append("-q")
if not run_tests:
args.append("-Dmaven.test.skip=true")
args += additional_args
args.append(goal)
logging.debug("Running %s" % (" ".join(args)))
for key in list(env.keys()):
value = env[key]
if isinstance(key, unicode):
key = key.encode("utf-8")
if isinstance(value, unicode):
value = value.encode("utf-8")
env[key] = value
if return_stdout:
return check_output(args, cwd = pom_path, env=env)
else:
subprocess.check_call(args, cwd = pom_path, env=env)
def check_maven_repositories(pom_path):
'''Check the repositories used by the POM for internet connectivity
pom_path - location of the pom.xml file
returns True if we can reach all repositories in a reasonable amount of time,
False if the ping failed.
Throws an exception if Maven failed, possibly because, given the -o switch
it didn't have what it needed to run the POM.
the goal, "dependency-list-repositories", lists the repositories needed
by a POM.
'''
output = run_maven(pom_path,
goal="dependency:list-repositories",
aggressive_update = None,
return_stdout=True)
pattern = r"\s*url:\s+((?:http|ftp|https):.+)"
for line in output.split("\n"):
line = line.strip()
match = re.match(pattern, line)
if match is not None:
url = match.groups()[0]
try:
urllib2.urlopen(url, timeout=1)
except urllib2.URLError, e:
return False
return True
def get_cellprofiler_jars():
'''Return the class path for the Java dependencies
NOTE: should not be called for the frozen version of CP
'''
root = get_cellprofiler_root_dir()
jars = set(filter(lambda x:x.endswith(".jar"), [x[0][-1] for x in files]))
aggressive_update = None
#
# Our jars come first because of patches
#
jar_dir = os.path.join(root, "imagej", "jars")
our_jars = ["cellprofiler-java.jar"]
for filename in classpath_filenames:
path = os.path.join(jar_dir, filename)
if not os.path.isfile(path):
raise RuntimeError(
"Can't determine CellProfiler java dependencies because %s is missing. Please re-run external_dependencies with the -o switch" % path)
jar_line = open(path, "r").readline().strip()
jar_list = jar_line.split(os.pathsep)
jar_filenames = [os.path.split(jar_path)[1] for jar_path in jar_list]
if len(our_jars) > 0:
jar_set = set(our_jars)
jar_filenames = filter((lambda x: x not in jar_set), jar_filenames)
our_jars += jar_filenames
return our_jars + sorted(jars)
if __name__=="__main__":
import optparse
usage = """Fetch external dependencies from internet
usage: %prog [options]"""
parser = optparse.OptionParser(usage=usage)
parser.add_option("-m", "--missing-only",
action="store_const",
const=False,
dest="overwrite",
default=False,
help="Download external dependency only if missing")
parser.add_option("-o", "--overwrite",
action="store_const",
const=True,
dest="overwrite",
help="Download external dependency if hash doesn't match")
parser.add_option("-f", "--fail",
action="store_const",
const="fail",
dest="overwrite",
help="Fail if a dependency exists and its hash is wrong")
options, args = parser.parse_args()
print "Fetching external dependencies..."
fetch_external_dependencies(options.overwrite)
print "Fetch complete"