Created
August 17, 2015 10:32
-
-
Save hstock/fdda11c7000ce118938a to your computer and use it in GitHub Desktop.
helper for recursively adding package data to python setup.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from os import path as osp | |
from fnmatch import fnmatch | |
import os | |
def package_data_with_recursive_dirs(package_data_spec): | |
"""converts modified package_data dict to a classic package_data dict | |
Where normal package_data entries can only specify globs, the | |
modified package_data dict can have | |
a) directory names or | |
b) tuples of a directory name and a pattern | |
as entries in addition to normal globs. | |
When one of a) or b) is encountered, the entry is expanded so | |
that the resulting package_data contains all files (optionally | |
filtered by pattern) encountered by recursively searching the | |
directory. | |
Usage: | |
setup( | |
... | |
package_data = package_data_with_recursive_dirs({ | |
'module': ['dir1', ('dir2', '*.xyz')], | |
'module2': ['dir3/file1.txt'] | |
}) | |
) | |
""" | |
out_spec = {} | |
for package_name, spec in package_data_spec.items(): | |
# replace dots by operating system path separator | |
package_path = osp.join(*package_name.split('.')) | |
out_entries = [] | |
for entry in spec: | |
directory = None # full path to data dir | |
pattern = None # pattern to append | |
datadir = None # data dir relative to package (as specified) | |
try: # entry is just a string | |
directory = osp.join(package_path, entry) | |
datadir = entry | |
pattern = None | |
except (TypeError, AttributeError): # entry has additional pattern spec | |
directory = osp.join(package_path, entry[0]) | |
pattern = entry[1] | |
datadir = entry[0] | |
if osp.isdir(directory): # only apply if it is really a directory | |
for (dirpath, dirnames, filenames) in os.walk(directory): | |
for filename in (osp.join(dirpath, f) for f in filenames): | |
if not pattern or fnmatch(filename, pattern): | |
relname = osp.normpath(osp.join(datadir, osp.relpath(filename, directory))) | |
out_entries.append(relname) | |
else: # datadir is not really a datadir but a glob or something else | |
out_entries.append(datadir) # we just copy the entry | |
out_spec[package_name] = out_entries | |
return out_spec | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment