-
-
Notifications
You must be signed in to change notification settings - Fork 685
Expand file tree
/
Copy pathnamespace_pkgs.bzl
More file actions
90 lines (72 loc) · 2.8 KB
/
namespace_pkgs.bzl
File metadata and controls
90 lines (72 loc) · 2.8 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""Utilities to get where we should write namespace pkg paths."""
load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
_ext = struct(
py = ".py",
pyd = ".pyd",
so = ".so",
pyc = ".pyc",
)
_TEMPLATE = Label("//python/private/pypi:namespace_pkg_tmpl.py")
def _add_all(dirname, dirs):
dir_path = "."
for dir_name in dirname.split("/"):
dir_path = "{}/{}".format(dir_path, dir_name)
dirs[dir_path[2:]] = None
def get_files(*, srcs, ignored_dirnames = [], root = None):
"""Get the list of filenames to write the namespace pkg files.
Args:
srcs: {type}`src` a list of files to be passed to {bzl:obj}`py_library`
as `srcs` and `data`. This is usually a result of a {obj}`glob`.
ignored_dirnames: {type}`str` a list of patterns to ignore.
root: {type}`str` the prefix to use as the root.
Returns:
{type}`src` a list of paths to write the namespace pkg `__init__.py` file.
"""
dirs = {}
ignored = {i: None for i in ignored_dirnames}
if root:
_add_all(root, ignored)
for file in srcs:
dirname, _, filename = file.rpartition("/")
if filename == "__init__.py":
ignored[dirname] = None
dirname, _, _ = dirname.rpartition("/")
elif filename.endswith(_ext.py):
pass
elif filename.endswith(_ext.pyc):
pass
elif filename.endswith(_ext.pyd):
pass
elif filename.endswith(_ext.so):
pass
else:
continue
if dirname in dirs or not dirname:
continue
_add_all(dirname, dirs)
return sorted([d for d in dirs if d not in ignored])
def create_inits(*, srcs, ignored_dirnames = [], root = None, copy_file = copy_file, **kwargs):
"""Create init files and return the list to be included `py_library` srcs.
Args:
srcs: {type}`src` a list of files to be passed to {bzl:obj}`py_library`
as `srcs` and `data`. This is usually a result of a {obj}`glob`.
ignored_dirnames: {type}`str` a list of patterns to ignore.
root: {type}`str` the prefix to use as the root.
copy_file: the `copy_file` rule to copy files in build context.
**kwargs: passed to {obj}`copy_file`.
Returns:
{type}`list[str]` to be included as part of `py_library`.
"""
ret = []
for i, out in enumerate(get_files(srcs = srcs, ignored_dirnames = ignored_dirnames, root = root)):
src = "{}/__init__.py".format(out)
ret.append(src)
copy_file(
# For the target name, use a number instead of trying to convert an output
# path into a valid label.
name = "_cp_{}_namespace".format(i),
src = _TEMPLATE,
out = src,
**kwargs
)
return ret