- Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathcommands.py
316 lines (244 loc) · 8.78 KB
/
commands.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
fromdistutilsimportlog
importjson
importos
importplatform
importshutil
fromsubprocessimportcheck_call
importsys
importtime
USAGE="usage: python commands.py [updateplotlyjsdev | updateplotlyjs | codegen]"
PROJECT_ROOT=os.path.dirname(os.path.abspath(__file__))
NODE_ROOT=os.path.join(PROJECT_ROOT, "js")
NODE_MODULES=os.path.join(NODE_ROOT, "node_modules")
TARGETS= [
os.path.join(PROJECT_ROOT, "plotly", "package_data", "widgetbundle.js"),
]
NPM_PATH=os.pathsep.join(
[
os.path.join(NODE_ROOT, "node_modules", ".bin"),
os.environ.get("PATH", os.defpath),
]
)
# Load plotly.js version from js/package.json
defplotly_js_version():
path=os.path.join(PROJECT_ROOT, "js", "package.json")
withopen(path, "rt") asf:
package_json=json.load(f)
version=package_json["dependencies"]["plotly.js"]
version=version.replace("^", "")
returnversion
# install package.json dependencies using npm
definstall_js_deps(local):
npmName="npm"
ifplatform.system() =="Windows":
npmName="npm.cmd"
try:
check_call([npmName, "--version"])
has_npm=True
except:
has_npm=False
skip_npm=os.environ.get("SKIP_NPM", False)
ifskip_npm:
log.info("Skipping npm-installation")
return
ifnothas_npm:
log.error(
"`npm` unavailable. If you're running this command using sudo, make sure `npm` is available to sudo"
)
env=os.environ.copy()
env["PATH"] =NPM_PATH
ifhas_npm:
log.info("Installing build dependencies with npm. This may take a while...")
check_call(
[npmName, "install"],
cwd=NODE_ROOT,
stdout=sys.stdout,
stderr=sys.stderr,
)
iflocalisnotNone:
plotly_archive=os.path.join(local, "plotly.js.tgz")
check_call(
[npmName, "install", plotly_archive],
cwd=NODE_ROOT,
stdout=sys.stdout,
stderr=sys.stderr,
)
check_call(
[npmName, "run", "build"],
cwd=NODE_ROOT,
stdout=sys.stdout,
stderr=sys.stderr,
)
os.utime(NODE_MODULES, None)
fortinTARGETS:
ifnotos.path.exists(t):
msg="Missing file: %s"%t
raiseValueError(msg)
# Generate class hierarchy from Plotly JSON schema
defrun_codegen():
ifsys.version_info< (3, 8):
raiseImportError("Code generation must be executed with Python >= 3.8")
fromcodegenimportperform_codegen
perform_codegen()
defoverwrite_schema_local(uri):
path=os.path.join(PROJECT_ROOT, "codegen", "resources", "plot-schema.json")
shutil.copyfile(uri, path)
defoverwrite_schema(url):
importrequests
req=requests.get(url)
assertreq.status_code==200
path=os.path.join(PROJECT_ROOT, "codegen", "resources", "plot-schema.json")
withopen(path, "wb") asf:
f.write(req.content)
defoverwrite_bundle_local(uri):
path=os.path.join(PROJECT_ROOT, "plotly", "package_data", "plotly.min.js")
shutil.copyfile(uri, path)
defoverwrite_bundle(url):
importrequests
req=requests.get(url)
print("url:", url)
assertreq.status_code==200
path=os.path.join(PROJECT_ROOT, "plotly", "package_data", "plotly.min.js")
withopen(path, "wb") asf:
f.write(req.content)
defoverwrite_plotlyjs_version_file(plotlyjs_version):
path=os.path.join(PROJECT_ROOT, "plotly", "offline", "_plotlyjs_version.py")
withopen(path, "w") asf:
f.write(
"""\
# DO NOT EDIT
# This file is generated by the updatebundle commands.py command
__plotlyjs_version__ = "{plotlyjs_version}"
""".format(
plotlyjs_version=plotlyjs_version
)
)
defrequest_json(url):
importrequests
req=requests.get(url)
returnjson.loads(req.content.decode("utf-8"))
defget_latest_publish_build_info(repo, branch):
url= (
r"https://circleci.com/api/v1.1/project/github/"
r"{repo}/tree/{branch}?limit=100&filter=completed"
).format(repo=repo, branch=branch)
branch_jobs=request_json(url)
# Get most recent successful publish build for branch
builds= [
j
forjinbranch_jobs
ifj.get("workflows", {}).get("job_name", None) =="publish-dist"
andj.get("status", None) =="success"
]
build=builds[0]
# Extract build info
return {p: build[p] forpin ["vcs_revision", "build_num", "committer_date"]}
defget_bundle_schema_local(local):
plotly_archive=os.path.join(local, "plotly.js.tgz")
plotly_bundle=os.path.join(local, "dist/plotly.min.js")
plotly_schemas=os.path.join(local, "dist/plot-schema.json")
returnplotly_archive, plotly_bundle, plotly_schemas
defget_bundle_schema_urls(build_num):
url= (
"https://circleci.com/api/v1.1/project/github/"
"plotly/plotly.js/{build_num}/artifacts"
).format(build_num=build_num)
artifacts=request_json(url)
# Find archive
archives= [aforainartifactsifa.get("path", None) =="plotly.js.tgz"]
archive=archives[0]
# Find bundle
bundles= [aforainartifactsifa.get("path", None) =="dist/plotly.min.js"]
bundle=bundles[0]
# Find schema
schemas= [aforainartifactsifa.get("path", None) =="dist/plot-schema.json"]
schema=schemas[0]
returnarchive["url"], bundle["url"], schema["url"]
# Download latest version of the plot-schema JSON file
defupdate_schema(plotly_js_version):
url= (
"https://raw.githubusercontent.com/plotly/plotly.js/"
"v%s/dist/plot-schema.json"%plotly_js_version
)
overwrite_schema(url)
# Download latest version of the plotly.js bundle
defupdate_bundle(plotly_js_version):
url= (
"https://raw.githubusercontent.com/plotly/plotly.js/"
"v%s/dist/plotly.min.js"%plotly_js_version
)
overwrite_bundle(url)
# Write plotly.js version file
plotlyjs_version=plotly_js_version
overwrite_plotlyjs_version_file(plotlyjs_version)
# Update project to a new version of plotly.js
defupdate_plotlyjs(plotly_js_version):
update_bundle(plotly_js_version)
update_schema(plotly_js_version)
run_codegen()
# Update the plotly.js schema and bundle from master
defupdate_schema_bundle_from_master():
if"--devrepo"insys.argv:
devrepo=sys.argv[sys.argv.index("--devrepo") +1]
else:
devrepo="plotly/plotly.js"
if"--devbranch"insys.argv:
devbranch=sys.argv[sys.argv.index("--devbranch") +1]
else:
devbranch="master"
if"--local"insys.argv:
local=sys.argv[sys.argv.index("--local") +1]
else:
local=None
iflocalisNone:
build_info=get_latest_publish_build_info(devrepo, devbranch)
archive_url, bundle_url, schema_url=get_bundle_schema_urls(
build_info["build_num"]
)
# Update bundle in package data
overwrite_bundle(bundle_url)
# Update schema in package data
overwrite_schema(schema_url)
else:
# this info could be more informative but
# it doesn't seem as useful in a local context
# and requires dependencies and programming.
build_info= {"vcs_revision": "local", "committer_date": str(time.time())}
devrepo=local
devbranch=""
archive_uri, bundle_uri, schema_uri=get_bundle_schema_local(local)
overwrite_bundle_local(bundle_uri)
overwrite_schema_local(schema_uri)
# Update plotly.js url in package.json
package_json_path=os.path.join(NODE_ROOT, "package.json")
withopen(package_json_path, "r") asf:
package_json=json.load(f)
# Replace version with bundle url
package_json["dependencies"]["plotly.js"] = (
archive_urliflocalisNoneelsearchive_uri
)
withopen(package_json_path, "w") asf:
json.dump(package_json, f, indent=2)
# update plotly.js version in _plotlyjs_version
rev=build_info["vcs_revision"]
date=str(build_info["committer_date"])
version="_".join([devrepo, devbranch, date[:10], rev[:8]])
overwrite_plotlyjs_version_file(version)
install_js_deps(local)
# Update project to a new development version of plotly.js
defupdate_plotlyjs_dev():
update_schema_bundle_from_master()
run_codegen()
defmain():
iflen(sys.argv) !=2:
print(USAGE, file=sys.stderr)
sys.exit(1)
elifsys.argv[1] =="codegen":
run_codegen()
elifsys.argv[1] =="updateplotlyjsdev":
update_plotlyjs_dev()
elifsys.argv[1] =="updateplotlyjs":
print(plotly_js_version())
update_plotlyjs(plotly_js_version())
if__name__=="__main__":
main()