Skip to content

Commit

Permalink
Merge pull request #5 from cedarai/dev/local-lib-support
Browse files Browse the repository at this point in the history
Better support for local libraries
  • Loading branch information
jvolkman committed Dec 6, 2022
2 parents 115e8a4 + 56057e9 commit cc898a8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
8 changes: 3 additions & 5 deletions build_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def find_site_packages(env_path: pathlib.Path) -> pathlib.Path:
def get_files(build_env_input: Dict) -> List[EnvFile]:
files = []

always_link = build_env_input.get("always_link", False)
imports = [pathlib.Path(imp) for imp in build_env_input["imports"]]
workspace = build_env_input["workspace"]
for depfile in build_env_input["files"]:
Expand All @@ -106,10 +107,6 @@ def get_files(build_env_input: Dict) -> List[EnvFile]:
type_ = depfile["t"]
input_path = pathlib.Path(depfile["p"])

# Only add external and generated files
if not (is_external(input_path) or type_ == "G"):
continue

# If this is a directory, expand to each recursive child.
if input_path.is_dir():
paths = input_path.glob("**/*")
Expand All @@ -119,7 +116,8 @@ def get_files(build_env_input: Dict) -> List[EnvFile]:

for path in paths:
site_packages_path = get_site_packages_path(workspace, path, imports)
files.append(EnvFile(path, site_packages_path))
if site_packages_path != path or always_link:
files.append(EnvFile(path, site_packages_path))

return files

Expand Down
18 changes: 18 additions & 0 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ load("@rules_python//python/pip_install:requirements.bzl", "compile_pip_requirem
py_venv(
name = "venv",
deps = [
"//libraries/liba",
requirement("black"),
requirement("numpy"),
],
Expand All @@ -29,6 +30,23 @@ py_venv(
]
)

py_venv(
name = "venv_only_local",
deps = [
"//libraries/liba",
"//libraries/libb",
],
)

py_venv(
name = "venv_only_local_always_link",
deps = [
"//libraries/liba",
"//libraries/libb",
],
always_link = True,
)

compile_pip_requirements(
name = "requirements",
extra_args = ["--allow-unsafe"],
Expand Down
8 changes: 8 additions & 0 deletions example/libraries/liba/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package(default_visibility = ["//visibility:public"])

py_library(
name = "liba",
srcs = ["liba.py"],
imports = ["."],
)

3 changes: 3 additions & 0 deletions example/libraries/liba/liba.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def foo():
pass

7 changes: 7 additions & 0 deletions example/libraries/libb/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package(default_visibility = ["//visibility:public"])

py_library(
name = "libb",
srcs = ["libb.py"],
)

3 changes: 3 additions & 0 deletions example/libraries/libb/libb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def foo():
pass

5 changes: 4 additions & 1 deletion venv.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def _py_venv_deps_impl(ctx):
"imports": imports,
"files": files,
"commands": ctx.attr.commands,
"always_link": ctx.attr.always_link,
}
ctx.actions.write(out, json.encode(doc))

Expand All @@ -54,12 +55,13 @@ _py_venv_deps = rule(
attrs = {
"deps": attr.label_list(),
"commands": attr.string_list(),
"always_link": attr.bool(),
"output": attr.output(),
},
toolchains = [PYTHON_TOOLCHAIN_TYPE],
)

def py_venv(name, deps = None, extra_pip_commands = None, **kwargs):
def py_venv(name, deps = None, extra_pip_commands = None, always_link = False, **kwargs):
deps = deps or []
extra_pip_commands = extra_pip_commands or []

Expand All @@ -70,6 +72,7 @@ def py_venv(name, deps = None, extra_pip_commands = None, **kwargs):
name = deps_name,
deps = deps,
commands = extra_pip_commands,
always_link = always_link,
output = out_name,
**kwargs,
)
Expand Down

0 comments on commit cc898a8

Please sign in to comment.