Skip to content

Commit 9e4f39e

Browse files
committed
Add --local flag to updateplotlyjsdev
1 parent 6745ef1 commit 9e4f39e

File tree

1 file changed

+48
-12
lines changed

1 file changed

+48
-12
lines changed

packages/python/plotly/setup.py

+48-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import sys
3+
import time
34
import platform
45
import json
6+
import shutil
57

68
from setuptools import setup, Command
79
from setuptools.command.egg_info import egg_info
@@ -137,10 +139,10 @@ class NPM(Command):
137139
]
138140

139141
def initialize_options(self):
140-
pass
142+
self.local = None
141143

142144
def finalize_options(self):
143-
pass
145+
self.set_undefined_options("updateplotlyjsdev", ("local", "local"))
144146

145147
def get_npm_name(self):
146148
npmName = "npm"
@@ -187,6 +189,14 @@ def run(self):
187189
stdout=sys.stdout,
188190
stderr=sys.stderr,
189191
)
192+
if self.local is not None:
193+
plotly_archive = os.path.join(self.local, "plotly.js.tgz")
194+
check_call(
195+
[npmName, "install", plotly_archive],
196+
cwd=node_root,
197+
stdout=sys.stdout,
198+
stderr=sys.stderr,
199+
)
190200
check_call(
191201
[npmName, "run", "build:prod"],
192202
cwd=node_root,
@@ -224,6 +234,9 @@ def run(self):
224234

225235
perform_codegen()
226236

237+
def overwrite_schema_local(uri):
238+
path = os.path.join(here, "codegen", "resources", "plot-schema.json")
239+
shutil.copyfile(uri, path)
227240

228241
def overwrite_schema(url):
229242
import requests
@@ -234,6 +247,9 @@ def overwrite_schema(url):
234247
with open(path, "wb") as f:
235248
f.write(req.content)
236249

250+
def overwrite_bundle_local(uri):
251+
path = os.path.join(here, "plotly", "package_data", "plotly.min.js")
252+
shutil.copyfile(uri, path)
237253

238254
def overwrite_bundle(url):
239255
import requests
@@ -281,7 +297,6 @@ def request_json(url):
281297
req = requests.get(url)
282298
return json.loads(req.content.decode("utf-8"))
283299

284-
285300
def get_latest_publish_build_info(repo, branch):
286301

287302
url = (
@@ -303,6 +318,11 @@ def get_latest_publish_build_info(repo, branch):
303318
# Extract build info
304319
return {p: build[p] for p in ["vcs_revision", "build_num", "committer_date"]}
305320

321+
def get_bundle_schema_local(local):
322+
plotly_archive = os.path.join(local, "plotly.js.tgz")
323+
plotly_bundle = os.path.join(local, "dist/plotly.min.js")
324+
plotly_schemas = os.path.join(local, "dist/plot-schema.json")
325+
return plotly_archive, plotly_bundle, plotly_schemas
306326

307327
def get_bundle_schema_urls(build_num):
308328
url = (
@@ -390,31 +410,45 @@ class UpdateBundleSchemaDevCommand(Command):
390410
def initialize_options(self):
391411
self.devrepo = None
392412
self.devbranch = None
413+
self.local = None
393414

394415
def finalize_options(self):
395416
self.set_undefined_options("updateplotlyjsdev", ("devrepo", "devrepo"))
396417
self.set_undefined_options("updateplotlyjsdev", ("devbranch", "devbranch"))
418+
self.set_undefined_options("updateplotlyjsdev", ("local", "local"))
397419

398420
def run(self):
399-
build_info = get_latest_publish_build_info(self.devrepo, self.devbranch)
421+
if self.local is None:
422+
build_info = get_latest_publish_build_info(self.devrepo, self.devbranch)
400423

401-
archive_url, bundle_url, schema_url = get_bundle_schema_urls(
402-
build_info["build_num"]
403-
)
424+
archive_url, bundle_url, schema_url = get_bundle_schema_urls(
425+
build_info["build_num"]
426+
)
427+
428+
# Update bundle in package data
429+
overwrite_bundle(bundle_url)
404430

405-
# Update bundle in package data
406-
overwrite_bundle(bundle_url)
431+
# Update schema in package data
432+
overwrite_schema(schema_url)
433+
else:
434+
# this info could be more informative but
435+
# it doesn't seem as useful in a local context
436+
# and requires dependencies and programming.
437+
build_info = {"vcs_revision": "local", "committer_date": str(time.time())}
438+
self.devrepo = self.local
439+
self.devbranch = ""
407440

408-
# Update schema in package data
409-
overwrite_schema(schema_url)
441+
archive_uri, bundle_uri, schema_uri = get_bundle_schema_local(self.local)
442+
overwrite_bundle_local(bundle_uri)
443+
overwrite_schema_local(schema_uri)
410444

411445
# Update plotly.js url in package.json
412446
package_json_path = os.path.join(node_root, "package.json")
413447
with open(package_json_path, "r") as f:
414448
package_json = json.load(f)
415449

416450
# Replace version with bundle url
417-
package_json["dependencies"]["plotly.js"] = archive_url
451+
package_json["dependencies"]["plotly.js"] = archive_url if self.local is None else archive_uri
418452
with open(package_json_path, "w") as f:
419453
json.dump(package_json, f, indent=2)
420454

@@ -430,11 +464,13 @@ class UpdatePlotlyJsDevCommand(Command):
430464
user_options = [
431465
("devrepo=", None, "Repository name"),
432466
("devbranch=", None, "branch or pull/number"),
467+
("local=", None, "local copy of repo, used by itself")
433468
]
434469

435470
def initialize_options(self):
436471
self.devrepo = "plotly/plotly.js"
437472
self.devbranch = "master"
473+
self.local = None
438474

439475
def finalize_options(self):
440476
pass

0 commit comments

Comments
 (0)