Skip to content

Commit 40cf804

Browse files
committed
Update setupbase from jupyter-packaging
1 parent c30a5f2 commit 40cf804

File tree

1 file changed

+49
-25
lines changed

1 file changed

+49
-25
lines changed

setupbase.py

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,26 @@
44
# Copyright (c) Jupyter Development Team.
55
# Distributed under the terms of the Modified BSD License.
66

7+
"""
8+
This file originates from the 'jupyter-packaging' package, and
9+
contains a set of useful utilities for including npm packages
10+
within a Python package.
11+
"""
12+
713
import os
814
from os.path import join as pjoin
915
import functools
1016
import pipes
1117
import sys
18+
from glob import glob
19+
from subprocess import check_call
1220

1321
from setuptools import Command
1422
from setuptools.command.build_py import build_py
1523
from setuptools.command.sdist import sdist
1624
from setuptools.command.develop import develop
1725
from setuptools.command.bdist_egg import bdist_egg
1826
from distutils import log
19-
from subprocess import check_call
2027

2128
try:
2229
from wheel.bdist_wheel import bdist_wheel
@@ -36,10 +43,10 @@ def list2cmdline(cmd_list):
3643
# Top Level Variables
3744
# ---------------------------------------------------------------------------
3845

39-
40-
here = os.path.abspath(os.path.dirname(sys.argv[0]))
46+
here = os.path.dirname(__file__)
4147
is_repo = os.path.exists(pjoin(here, '.git'))
4248
node_modules = pjoin(here, 'node_modules')
49+
4350
npm_path = ':'.join([
4451
pjoin(here, 'node_modules', '.bin'),
4552
os.environ.get('PATH', os.defpath),
@@ -57,17 +64,21 @@ def list2cmdline(cmd_list):
5764
# ---------------------------------------------------------------------------
5865

5966

60-
def get_data_files(top):
61-
"""Get data files"""
67+
def expand_data_files(data_file_patterns):
68+
"""Expand data file patterns to a valid data_files spec.
6269
70+
Parameters
71+
-----------
72+
data_file_patterns: list(tuple)
73+
A list of (directory, glob patterns) for the data file locations.
74+
The globs themselves do not recurse.
75+
"""
6376
data_files = []
64-
ntrim = len(here + os.path.sep)
65-
66-
for (d, dirs, filenames) in os.walk(top):
67-
data_files.append((
68-
d[ntrim:],
69-
[pjoin(d, f) for f in filenames]
70-
))
77+
for (directory, patterns) in data_file_patterns:
78+
files = []
79+
for p in patterns:
80+
files.extend([os.path.relpath(f, here) for f in glob(p)])
81+
data_files.append((directory, files))
7182
return data_files
7283

7384

@@ -91,20 +102,17 @@ def update_package_data(distribution):
91102
build_py.finalize_options()
92103

93104

94-
def create_cmdclass(wrappers=None, data_dirs=None):
105+
def create_cmdclass(wrappers=None):
95106
"""Create a command class with the given optional wrappers.
96107
97108
Parameters
98109
----------
99110
wrappers: list(str), optional
100111
The cmdclass names to run before running other commands
101-
data_dirs: list(str), optional.
102-
The directories containing static data.
103112
"""
104113
egg = bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled
105114
wrappers = wrappers or []
106-
data_dirs = data_dirs or []
107-
wrapper = functools.partial(wrap_command, wrappers, data_dirs)
115+
wrapper = functools.partial(wrap_command, wrappers)
108116
cmdclass = dict(
109117
build_py=wrapper(build_py, strict=is_repo),
110118
sdist=wrapper(sdist, strict=True),
@@ -225,6 +233,8 @@ def mtime(path):
225233
def install_npm(path=None, build_dir=None, source_dir=None, build_cmd='build', force=False):
226234
"""Return a Command for managing an npm installation.
227235
236+
Note: The command is skipped if the `--skip-npm` flag is used.
237+
228238
Parameters
229239
----------
230240
path: str, optional
@@ -266,6 +276,26 @@ def run(self):
266276
return NPM
267277

268278

279+
def ensure_targets(targets):
280+
"""Return a Command that checks that certain files exist.
281+
282+
Raises a ValueError if any of the files are missing.
283+
284+
Note: The check is skipped if the `--skip-npm` flag is used.
285+
"""
286+
287+
class TargetsCheck(BaseCommand):
288+
def run(self):
289+
if skip_npm:
290+
log.info('Skipping target checks')
291+
return
292+
missing = [t for t in targets if not os.path.exists(t)]
293+
if missing:
294+
raise ValueError(('missing files: %s' % missing))
295+
296+
return TargetsCheck
297+
298+
269299
# `shutils.which` function copied verbatim from the Python-3.3 source.
270300
def which(cmd, mode=os.F_OK | os.X_OK, path=None):
271301
"""Given a command, mode, and a PATH string, return the path which
@@ -325,7 +355,7 @@ def _access_check(fn, mode):
325355
# ---------------------------------------------------------------------------
326356

327357

328-
def wrap_command(cmds, data_dirs, cls, strict=True):
358+
def wrap_command(cmds, cls, strict=True):
329359
"""Wrap a setup command
330360
331361
Parameters
@@ -348,13 +378,7 @@ def run(self):
348378
pass
349379

350380
result = cls.run(self)
351-
if data_dirs:
352-
data_files = self.distribution.data_files if self.distribution.data_files else []
353-
for dname in data_dirs:
354-
data_files.extend(get_data_files(dname))
355-
# update data-files in case this created new files
356-
self.distribution.data_files = data_files
357-
# also update package data
381+
# update package data
358382
update_package_data(self.distribution)
359383
return result
360384
return WrappedCommand

0 commit comments

Comments
 (0)