-
Notifications
You must be signed in to change notification settings - Fork 361
/
_mpld3_setup.py
265 lines (207 loc) · 7.81 KB
/
_mpld3_setup.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
"""
Tools to help with setup.py
Much of this is based on tools in the IPython project:
http://github.com/ipython/ipython
"""
import os
import subprocess
import sys
import warnings
import shutil
try:
from setuptools import Command
except:
from distutils.cmd import Command
SUBMODULES = ['mplexporter']
SUBMODULE_SYNC_PATHS = [('mplexporter/mplexporter', 'mpld3/mplexporter')]
def get_version():
"""Get the version info from the mpld3 package without importing it"""
with open(os.path.join("mpld3", "__about__.py"), "r") as init_file:
exec(compile(init_file.read(), 'mpld3/__about__.py', 'exec'), globals())
try:
return __version__
except NameError:
raise ValueError("version could not be located")
def is_repo(d):
"""is d a git repo?"""
return os.path.exists(os.path.join(d, '.git'))
def check_submodule_status(root=None):
"""check submodule status
Has three return values:
'missing' - submodules are absent
'unclean' - submodules have unstaged changes
'clean' - all submodules are up to date
"""
if root is None:
root = os.path.dirname(os.path.abspath(__file__))
if hasattr(sys, "frozen"):
# frozen via py2exe or similar, don't bother
return 'clean'
if not is_repo(root):
# not in git, assume clean
return 'clean'
for submodule in SUBMODULES:
if not os.path.exists(submodule):
return 'missing'
# Popen can't handle unicode cwd on Windows Python 2
if sys.platform == 'win32' and sys.version_info[0] < 3 \
and not isinstance(root, bytes):
root = root.encode(sys.getfilesystemencoding() or 'ascii')
# check with git submodule status
proc = subprocess.Popen('git submodule status',
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
cwd=root,
)
status, _ = proc.communicate()
status = status.decode("ascii", "replace")
for line in status.splitlines():
if line.startswith('-'):
return 'missing'
elif line.startswith('+'):
return 'unclean'
return 'clean'
def update_submodules(repo_dir):
"""update submodules in a repo"""
subprocess.check_call("git submodule init", cwd=repo_dir, shell=True)
subprocess.check_call("git submodule update --recursive",
cwd=repo_dir, shell=True)
def sync_files(source, dest):
"""Syncs files copies files from `source` directory to the
`dest` directory. A check is first done to see if the `dest`
directory exists, if so, the directory is removed to provide
a clean install.
"""
if os.path.isdir(source) and os.path.isdir(dest):
try:
print("Remove {0}".format(dest))
shutil.rmtree(dest)
except OSError as e:
# An error occured tyring to remove directory
print(e.errno)
print(e.filename)
print(e.strerror)
print("Copying {0} to {1}".format(source, dest))
shutil.copytree(source, dest)
def sync_submodules(repo_dir):
for source, dest in SUBMODULE_SYNC_PATHS:
source = os.path.join(repo_dir, source)
dest = os.path.join(repo_dir, dest)
sync_files(source, dest)
def require_clean_submodules(repo_dir, argv):
"""Check on git submodules before distutils can do anything
Since distutils cannot be trusted to update the tree
after everything has been set in motion,
this is not a distutils command.
"""
# Only do this if we are in the git source repository.
if not is_repo(repo_dir):
return
# don't do anything if nothing is actually supposed to happen
for do_nothing in ('-h', '--help', '--help-commands',
'clean', 'submodule', 'buildjs'):
if do_nothing in argv:
return
status = check_submodule_status(repo_dir)
if status == "missing":
print("checking out submodules for the first time")
update_submodules(repo_dir)
elif status == "unclean":
print('\n'.join([
"Cannot build / install mpld3 with unclean submodules",
"Please update submodules with",
" python setup.py submodule",
"or commit any submodule changes you have made."
]))
sys.exit(1)
sync_submodules(repo_dir)
class UpdateSubmodules(Command):
"""Update git submodules"""
description = "Update git submodules"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
failure = False
try:
self.spawn('git submodule init'.split())
self.spawn('git submodule update --recursive'.split())
except Exception as e:
failure = e
print(e)
if not check_submodule_status() == 'clean':
print("submodules could not be checked out")
sys.exit(1)
BUILD_WARNING = """
# It appears that the javascript sources may have been modified.
# If this is the case, then the JS libraries should be rebuilt.
# Please run
# python setup.py buildjs
# to re-build the javascript libraries.
# This requires npm to be installed: see CONTRIBUTING.md for details.
# If you have not modified the javascript sources, then you can safely
# disregard this message.
"""
VERSION_ERROR = """
# Javascript libraries for mpld3 version {0} are missing.
# Please run
# python setup.py buildjs
# to re-build the javascript libraries.
# This requires npm to be installed: see CONTRIBUTING.md for details.
"""
def check_js_build_status(version, root=None, srcdir=None):
"""Check the javascript build status.
Summary:
- if we're not in the git repo, or if the source directory doesn't exist,
then do nothing.
- if the JS libraries do not exist, return an error with a message about
building them.
- if the JS sources have been modified, return a warning with a message
about how to use them to build the libraries.
"""
if root is None:
root = os.path.dirname(os.path.abspath(__file__))
if srcdir is None:
srcdir = os.path.join(root, 'src')
# If we're not in the git repo, then we perform no checks
if not is_repo(root):
return
# If the source directory doesn't exist, then perform no checks
# (this is the case in the packaged distribution)
if not os.path.exists(srcdir):
return
# these are the built javascript libraries
js_libs = [os.path.join(root, "mpld3", "js", lib.format(version))
for lib in ('mpld3.v{0}.js', 'mpld3.v{0}.min.js')]
# if the js libraries don't exist, then throw an error
if not all(os.path.exists(lib) for lib in js_libs):
raise ValueError(VERSION_ERROR.format(version))
# these are the javascript sources
js_sources = [os.path.join(root, 'package.json')]
for (directory, subdirs, flist) in os.walk(srcdir):
js_sources.extend([os.path.join(directory, f)
for f in flist if f.endswith('.js')])
# if it looks like the sources have been modified, then warn that
# they should be rebuilt
last_modified_src = max([os.stat(f).st_mtime for f in js_sources])
first_modified_lib = min([os.stat(f).st_mtime for f in js_libs])
if last_modified_src > first_modified_lib:
warnings.warn(BUILD_WARNING)
class BuildJavascript(Command):
"""Build the javascript libraries"""
description = "Build the mpld3 javascript libraries"
user_options = []
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
failure = False
try:
self.spawn('make javascript'.split())
except Exception as e:
failure = e
print(e)