Skip to content

Commit 55a376f

Browse files
committed
DOC: add/expand docstrings, move code for testers
1 parent 4780b6f commit 55a376f

File tree

1 file changed

+133
-66
lines changed

1 file changed

+133
-66
lines changed

nisext/testers.py

Lines changed: 133 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
''' Test package information in various install settings
22
3-
The routines here install the package in various settings and print out the
4-
corresponding version info from the installation.
3+
The routines here install the package from source directories, zips or eggs, and
4+
check these installations by running tests, checking version information,
5+
looking for files that were not copied over.
56
6-
The typical use for this module is as a makefile target, as in::
7+
The typical use for this module is as a Makefile target. For example, here are
8+
the Makefile targets from nibabel::
9+
10+
# Check for files not installed
11+
check-files:
12+
$(PYTHON) -c 'from nisext.testers import check_files; check_files("nibabel")'
713
814
# Print out info for possible install methods
915
check-version-info:
10-
python -c 'from nisext.testers import info_from_here; info_from_here("mypackage")'
16+
$(PYTHON) -c 'from nisext.testers import info_from_here; info_from_here("nibabel")'
17+
18+
# Run tests from installed code
19+
installed-tests:
20+
$(PYTHON) -c 'from nisext.testers import tests_installed; tests_installed("nibabel")'
21+
22+
# Run tests from installed code
23+
sdist-tests:
24+
$(PYTHON) -c 'from nisext.testers import sdist_tests; sdist_tests("nibabel")'
25+
26+
# Run tests from binary egg
27+
bdist-egg-tests:
28+
$(PYTHON) -c 'from nisext.testers import bdist_egg_tests; bdist_egg_tests("nibabel")'
1129
1230
'''
1331

@@ -66,7 +84,7 @@ def zip_extract_all(fname, path=None):
6684
zf.extract(zipinfo, path, None)
6785

6886

69-
def install_from_to(from_dir, to_dir, py_lib_sdir, bin_sdir='bin'):
87+
def install_from_to(from_dir, to_dir, py_lib_sdir=PY_LIB_SDIR, bin_sdir='bin'):
7088
""" Install package in `from_dir` to standard location in `to_dir`
7189
7290
Parameters
@@ -76,7 +94,7 @@ def install_from_to(from_dir, to_dir, py_lib_sdir, bin_sdir='bin'):
7694
to_dir : str
7795
prefix path to which files will be installed, as in ``python setup.py
7896
install --prefix=to_dir``
79-
py_lib_sdir : str
97+
py_lib_sdir : str, optional
8098
subdirectory within `to_dir` to which library code will be installed
8199
bin_sdir : str, optional
82100
subdirectory within `to_dir` to which scripts will be installed
@@ -93,67 +111,30 @@ def install_from_to(from_dir, to_dir, py_lib_sdir, bin_sdir='bin'):
93111
os.chdir(pwd)
94112

95113

96-
def check_installed_files(repo_mod_path, install_mod_path):
97-
""" Check files in `repo_mod_path` are installed at `install_mod_path`
98-
99-
At the moment, all this does is check that all the ``*.py`` files in
100-
`repo_mod_path` are installed at `install_mod_path`.
101-
102-
Parameters
103-
----------
104-
repo_mod_path : str
105-
repository path containing package files, e.g. <nibabel-repo>/nibabel>
106-
install_mod_path : str
107-
path at which package has been installed. This is the path where the
108-
root package ``__init__.py`` lives.
109-
110-
Return
111-
------
112-
uninstalled : list
113-
list of files that should have been installed, but have not been
114-
installed
115-
"""
116-
return missing_from(repo_mod_path, install_mod_path, filter=r"\.py$")
117-
118-
119-
def missing_from(path0, path1, filter=None):
120-
""" Return filenames present in `path0` but not in `path1`
114+
def install_from_zip(zip_fname, install_path, pkg_finder=None,
115+
py_lib_sdir=PY_LIB_SDIR,
116+
script_sdir='bin'):
117+
""" Install package from zip file `zip_fname`
121118
122119
Parameters
123120
----------
124-
path0 : str
125-
path which contains all files of interest
126-
path1 : str
127-
path which should contain all files of interest
128-
filter : None or str or regexp, optional
129-
A successful result from ``filter.search(fname)`` means the file is of
130-
interest. None means all files are of interest
131-
132-
Returns
133-
-------
134-
path1_missing : list
135-
list of all files missing from `path1` that are in `path0` at the same
136-
relative path.
121+
zip_fname : str
122+
filename of zip file containing package code
123+
install_path : str
124+
output prefix at which to install package
125+
pkg_finder : None or callable, optional
126+
If None, assume zip contains ``setup.py`` at the top level. Otherwise,
127+
find directory containing ``setup.py`` with ``pth =
128+
pkg_finder(unzip_path)`` where ``unzip_path`` is the path to which we
129+
have unzipped the zip file contents.
130+
py_lib_sdir : str, optional
131+
subdirectory to which to write the library code from the package. Thus
132+
if package called ``nibabel``, the written code will be in
133+
``<install_path>/<py_lib_sdir>/nibabel
134+
script_sdir : str, optional
135+
subdirectory to which we write the installed scripts. Thus scripts will
136+
be written to ``<install_path>/<script_sdir>
137137
"""
138-
if not filter is None:
139-
filter = re.compile(filter)
140-
repo_mod_path = os.path.abspath(path0)
141-
uninstalled = []
142-
# Walk directory tree to get py files
143-
for dirpath, dirnames, filenames in os.walk(path0):
144-
out_dirpath = dirpath.replace(path0, path1)
145-
for fname in filenames:
146-
if not filter is None and filter.search(fname) is None:
147-
continue
148-
equiv_fname = os.path.join(out_dirpath, fname)
149-
if not os.path.isfile(equiv_fname):
150-
uninstalled.append(pjoin(dirpath, fname))
151-
return uninstalled
152-
153-
154-
def install_from_zip(zip_fname, install_path, pkg_finder=None,
155-
py_lib_sdir=PY_LIB_SDIR,
156-
script_sdir='bin'):
157138
unzip_path = tempfile.mkdtemp()
158139
try:
159140
# Zip may unpack module into current directory
@@ -251,6 +232,63 @@ def tests_installed(mod_name, source_path=None):
251232
tests_installed.__test__ = False
252233

253234

235+
def check_installed_files(repo_mod_path, install_mod_path):
236+
""" Check files in `repo_mod_path` are installed at `install_mod_path`
237+
238+
At the moment, all this does is check that all the ``*.py`` files in
239+
`repo_mod_path` are installed at `install_mod_path`.
240+
241+
Parameters
242+
----------
243+
repo_mod_path : str
244+
repository path containing package files, e.g. <nibabel-repo>/nibabel>
245+
install_mod_path : str
246+
path at which package has been installed. This is the path where the
247+
root package ``__init__.py`` lives.
248+
249+
Return
250+
------
251+
uninstalled : list
252+
list of files that should have been installed, but have not been
253+
installed
254+
"""
255+
return missing_from(repo_mod_path, install_mod_path, filter=r"\.py$")
256+
257+
258+
def missing_from(path0, path1, filter=None):
259+
""" Return filenames present in `path0` but not in `path1`
260+
261+
Parameters
262+
----------
263+
path0 : str
264+
path which contains all files of interest
265+
path1 : str
266+
path which should contain all files of interest
267+
filter : None or str or regexp, optional
268+
A successful result from ``filter.search(fname)`` means the file is of
269+
interest. None means all files are of interest
270+
271+
Returns
272+
-------
273+
path1_missing : list
274+
list of all files missing from `path1` that are in `path0` at the same
275+
relative path.
276+
"""
277+
if not filter is None:
278+
filter = re.compile(filter)
279+
uninstalled = []
280+
# Walk directory tree to get py files
281+
for dirpath, dirnames, filenames in os.walk(path0):
282+
out_dirpath = dirpath.replace(path0, path1)
283+
for fname in filenames:
284+
if not filter is None and filter.search(fname) is None:
285+
continue
286+
equiv_fname = os.path.join(out_dirpath, fname)
287+
if not os.path.isfile(equiv_fname):
288+
uninstalled.append(pjoin(dirpath, fname))
289+
return uninstalled
290+
291+
254292
def check_files(mod_name, repo_path=None, scripts_sdir='bin'):
255293
""" Print library and script files not picked up during install
256294
"""
@@ -278,8 +316,9 @@ def check_files(mod_name, repo_path=None, scripts_sdir='bin'):
278316
print "Missed script files: ", ', '.join(script_misses)
279317

280318

281-
282319
def get_sdist_finder(mod_name):
320+
""" Return function finding sdist source directory for `mod_name`
321+
"""
283322
def pf(pth):
284323
pkg_dirs = glob(pjoin(pth, mod_name + '-*'))
285324
if len(pkg_dirs) != 1:
@@ -327,6 +366,34 @@ def bdist_egg_tests(mod_name, repo_path=None):
327366

328367

329368
def make_dist(repo_path, out_dir, setup_params, zipglob):
369+
""" Create distutils distribution file
370+
371+
Parameters
372+
----------
373+
repo_path : str
374+
path to repository containing code and ``setup.py``
375+
out_dir : str
376+
path to which to write new distribution file
377+
setup_params: str
378+
parameters to pass to ``setup.py`` to create distribution.
379+
zipglob : str
380+
glob identifying expected output file.
381+
382+
Returns
383+
-------
384+
out_fname : str
385+
filename of generated distribution file
386+
387+
Examples
388+
--------
389+
Make, return a zipped sdist::
390+
391+
make_dist('/path/to/repo', '/tmp/path', 'sdist --formats=zip', '*.zip')
392+
393+
Make, return a binary egg::
394+
395+
make_dist('/path/to/repo', '/tmp/path', 'bdist_egg', '*.egg')
396+
"""
330397
pwd = os.path.abspath(os.getcwd())
331398
try:
332399
os.chdir(repo_path)
@@ -335,8 +402,8 @@ def make_dist(repo_path, out_dir, setup_params, zipglob):
335402
zips = glob(pjoin(out_dir, zipglob))
336403
if len(zips) != 1:
337404
raise OSError('There must be one and only one %s file, '
338-
'but I found "%s"' %
339-
(zipglob, ': '.join(zips)))
405+
'but I found "%s"' %
406+
(zipglob, ': '.join(zips)))
340407
finally:
341408
os.chdir(pwd)
342409
return zips[0]

0 commit comments

Comments
 (0)