-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_utils.py
54 lines (51 loc) · 2.31 KB
/
setup_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/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