| 1 |
# Note: you may want to copy this into your setup.py file verbatim, as |
|---|
| 2 |
# you can't import this from another package, when you don't know if |
|---|
| 3 |
# that package is installed yet. |
|---|
| 4 |
|
|---|
| 5 |
import os |
|---|
| 6 |
import sys |
|---|
| 7 |
from fnmatch import fnmatchcase |
|---|
| 8 |
from distutils.util import convert_path |
|---|
| 9 |
|
|---|
| 10 |
# Provided as an attribute, so you can append to these instead |
|---|
| 11 |
# of replicating them: |
|---|
| 12 |
standard_exclude = ('*.py', '*.pyc', '*~', '.*', '*.bak') |
|---|
| 13 |
standard_exclude_directories = ('.*', 'CVS', '_darcs', './build', |
|---|
| 14 |
'./dist', 'EGG-INFO', '*.egg-info') |
|---|
| 15 |
|
|---|
| 16 |
def find_package_data( |
|---|
| 17 |
where='.', package='', |
|---|
| 18 |
exclude=standard_exclude, |
|---|
| 19 |
exclude_directories=standard_exclude_directories, |
|---|
| 20 |
only_in_packages=True, |
|---|
| 21 |
show_ignored=False): |
|---|
| 22 |
""" |
|---|
| 23 |
Return a dictionary suitable for use in ``package_data`` |
|---|
| 24 |
in a distutils ``setup.py`` file. |
|---|
| 25 |
|
|---|
| 26 |
The dictionary looks like:: |
|---|
| 27 |
|
|---|
| 28 |
{'package': [files]} |
|---|
| 29 |
|
|---|
| 30 |
Where ``files`` is a list of all the files in that package that |
|---|
| 31 |
don't match anything in ``exclude``. |
|---|
| 32 |
|
|---|
| 33 |
If ``only_in_packages`` is true, then top-level directories that |
|---|
| 34 |
are not packages won't be included (but directories under packages |
|---|
| 35 |
will). |
|---|
| 36 |
|
|---|
| 37 |
Directories matching any pattern in ``exclude_directories`` will |
|---|
| 38 |
be ignored; by default directories with leading ``.``, ``CVS``, |
|---|
| 39 |
and ``_darcs`` will be ignored. |
|---|
| 40 |
|
|---|
| 41 |
If ``show_ignored`` is true, then all the files that aren't |
|---|
| 42 |
included in package data are shown on stderr (for debugging |
|---|
| 43 |
purposes). |
|---|
| 44 |
|
|---|
| 45 |
Note patterns use wildcards, or can be exact paths (including |
|---|
| 46 |
leading ``./``), and all searching is case-insensitive. |
|---|
| 47 |
""" |
|---|
| 48 |
|
|---|
| 49 |
out = {} |
|---|
| 50 |
stack = [(convert_path(where), '', package, only_in_packages)] |
|---|
| 51 |
while stack: |
|---|
| 52 |
where, prefix, package, only_in_packages = stack.pop(0) |
|---|
| 53 |
for name in os.listdir(where): |
|---|
| 54 |
fn = os.path.join(where, name) |
|---|
| 55 |
if os.path.isdir(fn): |
|---|
| 56 |
bad_name = False |
|---|
| 57 |
for pattern in exclude_directories: |
|---|
| 58 |
if (fnmatchcase(name, pattern) |
|---|
| 59 |
or fn.lower() == pattern.lower()): |
|---|
| 60 |
bad_name = True |
|---|
| 61 |
if show_ignored: |
|---|
| 62 |
print >> sys.stderr, ( |
|---|
| 63 |
"Directory %s ignored by pattern %s" |
|---|
| 64 |
% (fn, pattern)) |
|---|
| 65 |
break |
|---|
| 66 |
if bad_name: |
|---|
| 67 |
continue |
|---|
| 68 |
if os.path.isfile(os.path.join(fn, '__init__.py')): |
|---|
| 69 |
if not package: |
|---|
| 70 |
new_package = name |
|---|
| 71 |
else: |
|---|
| 72 |
new_package = package + '.' + name |
|---|
| 73 |
stack.append((fn, '', new_package, False)) |
|---|
| 74 |
else: |
|---|
| 75 |
stack.append((fn, prefix + name + '/', package, only_in_packages)) |
|---|
| 76 |
elif package or not only_in_packages: |
|---|
| 77 |
# is a file |
|---|
| 78 |
bad_name = False |
|---|
| 79 |
for pattern in exclude: |
|---|
| 80 |
if (fnmatchcase(name, pattern) |
|---|
| 81 |
or fn.lower() == pattern.lower()): |
|---|
| 82 |
bad_name = True |
|---|
| 83 |
if show_ignored: |
|---|
| 84 |
print >> sys.stderr, ( |
|---|
| 85 |
"File %s ignored by pattern %s" |
|---|
| 86 |
% (fn, pattern)) |
|---|
| 87 |
break |
|---|
| 88 |
if bad_name: |
|---|
| 89 |
continue |
|---|
| 90 |
out.setdefault(package, []).append(prefix+name) |
|---|
| 91 |
return out |
|---|
| 92 |
|
|---|
| 93 |
if __name__ == '__main__': |
|---|
| 94 |
import sys, pprint |
|---|
| 95 |
pprint.pprint( |
|---|
| 96 |
find_package_data(show_ignored=True)) |
|---|