11# -*- coding: utf-8 -*-
22
33from __future__ import print_function
4- from setuptools import setup , find_packages , Command
5- from setuptools .command .sdist import sdist
6- from setuptools .command .build_py import build_py
7- from setuptools .command .egg_info import egg_info
8- from subprocess import check_call
4+ from setuptools import setup , find_packages
95import os
106import sys
11- import platform
127
13- here = os .path .dirname (os .path .abspath (__file__ ))
14- node_root = os .path .join (here , 'js' )
15- is_repo = os .path .exists (os .path .join (here , '.git' ))
8+ from setupbase import (
9+ create_cmdclass ,
10+ install_npm ,
11+ )
1612
17- npm_path = os .pathsep .join ([
18- os .path .join (node_root , 'node_modules' , '.bin' ),
19- os .environ .get ('PATH' , os .defpath ),
20- ])
2113
2214from distutils import log
2315log .set_verbosity (log .DEBUG )
2618
2719LONG_DESCRIPTION = 'A Python/ThreeJS bridge utilizing the Jupyter widget infrastructure.'
2820
29- def js_prerelease (command , strict = False ):
30- """decorator for building minified js/css prior to another command"""
31- class DecoratedCommand (command ):
32- def run (self ):
33- jsdeps = self .distribution .get_command_obj ('jsdeps' )
34- if not is_repo and all (os .path .exists (t ) for t in jsdeps .targets ):
35- # sdist, nothing to do
36- command .run (self )
37- return
3821
39- try :
40- self .distribution .run_command ('jsdeps' )
41- except Exception as e :
42- missing = [t for t in jsdeps .targets if not os .path .exists (t )]
43- if strict or missing :
44- log .warn ('rebuilding js and css failed' )
45- if missing :
46- log .error ('missing files: %s' % missing )
47- raise e
48- else :
49- log .warn ('rebuilding js and css failed (not a problem)' )
50- log .warn (str (e ))
51- command .run (self )
52- update_package_data (self .distribution )
53- return DecoratedCommand
22+ here = os .path .abspath (os .path .dirname (sys .argv [0 ]))
5423
55- def update_package_data (distribution ):
56- """update package_data to catch changes during setup"""
57- build_py = distribution .get_command_obj ('build_py' )
58- # distribution.package_data = find_package_data()
59- # re-init build_py options which load package_data
60- build_py .finalize_options ()
61-
62-
63- class NPM (Command ):
64- description = 'install package.json dependencies using npm'
65-
66- user_options = []
67-
68- node_modules = os .path .join (node_root , 'node_modules' )
69-
70- targets = [
71- os .path .join (here , 'pythreejs' , 'static' , 'extension.js' ),
72- os .path .join (here , 'pythreejs' , 'static' , 'index.js' )
73- ]
74-
75- def initialize_options (self ):
76- pass
77-
78- def finalize_options (self ):
79- pass
80-
81- def has_npm (self ):
82- try :
83- check_call (['npm' , '--version' ])
84- return True
85- except :
86- return False
87-
88- def should_run_npm_install (self ):
89- package_json = os .path .join (node_root , 'package.json' )
90- node_modules_exists = os .path .exists (self .node_modules )
91- return self .has_npm ()
92-
93- def run (self ):
94- has_npm = self .has_npm ()
95- if not has_npm :
96- log .error ("`npm` unavailable. If you're running this command using sudo, make sure `npm` is available to sudo" )
97-
98- env = os .environ .copy ()
99- env ['PATH' ] = npm_path
100-
101- if self .should_run_npm_install ():
102- log .info ("Installing build dependencies with npm. This may take a while..." )
103- check_call (['npm' , 'install' ], cwd = node_root , stdout = sys .stdout , stderr = sys .stderr )
104- os .utime (self .node_modules , None )
105-
106- for t in self .targets :
107- if not os .path .exists (t ):
108- msg = 'Missing file: %s' % t
109- if not has_npm :
110- msg += '\n npm is required to build a development version of widgetsnbextension'
111- raise ValueError (msg )
112-
113- # update package data in case this created new files
114- update_package_data (self .distribution )
11524
11625version_ns = {}
11726with open (os .path .join (here , 'pythreejs' , '_version.py' )) as f :
11827 exec (f .read (), {}, version_ns )
11928
29+
30+ cmdclass = create_cmdclass (['js' ])
31+ cmdclass ['js' ] = install_npm (
32+ path = os .path .join (here , 'js' ),
33+ build_dir = os .path .join (here , 'pythreejs' , 'static' ),
34+ source_dir = os .path .join (here , 'js' ),
35+ )
36+
12037setup_args = {
12138 'name' : 'pythreejs' ,
12239 'version' : version_ns ['__version__' ],
@@ -134,12 +51,7 @@ def run(self):
13451 'install_requires' : ['ipywidgets>=7.0.0a9' , 'traittypes' ],
13552 'packages' : find_packages (),
13653 'zip_safe' : False ,
137- 'cmdclass' : {
138- 'build_py' : js_prerelease (build_py ),
139- 'egg_info' : js_prerelease (egg_info ),
140- 'sdist' : js_prerelease (sdist , strict = True ),
141- 'jsdeps' : NPM ,
142- },
54+ 'cmdclass' : cmdclass ,
14355 'author' : 'PyThreejs Development Team' ,
14456 'author_email' : 'jason@jasongrout.org' ,
14557 'url' : 'https://github.com/jovyan/pythreejs' ,
0 commit comments