Skip to content

Commit

Permalink
Allow adding extra files to site-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
psalaberria002 committed Jun 20, 2024
1 parent b692303 commit 98a19d2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/typical/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ create_venv(name = "create-venv")
create_venv(
name = "create-venv-custom-destination",
destination_folder = ".venv",
site_packages_extra_files = ["sitecustomize.py"],
)
1 change: 1 addition & 0 deletions examples/typical/sitecustomize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hola
6 changes: 6 additions & 0 deletions uv/private/create_venv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,11 @@ fi
source "$BUILD_WORKSPACE_DIRECTORY/$target/bin/activate"
"$UV" pip install -r "$REQUIREMENTS_TXT"

site_packages_extra_files=({{site_packages_extra_files}})
site_packages_dir=$(find "$BUILD_WORKSPACE_DIRECTORY/$target/lib" -type d -name 'site-packages')
for file in "${site_packages_extra_files[@]}"; do
cp "$file" "$site_packages_dir"/
done

echo "${bold}Created '${target}', to activate run:${normal}"
echo " source ${target}/bin/activate"
8 changes: 6 additions & 2 deletions uv/private/venv.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ _PY_TOOLCHAIN = "@bazel_tools//tools/python:toolchain_type"

def _uv_template(ctx, template, executable):
py_toolchain = ctx.toolchains[_PY_TOOLCHAIN]

ctx.actions.expand_template(
template = template,
output = executable,
Expand All @@ -12,13 +13,14 @@ def _uv_template(ctx, template, executable):
"{{requirements_txt}}": ctx.file.requirements_txt.short_path,
"{{resolved_python}}": py_toolchain.py3_runtime.interpreter.short_path,
"{{destination_folder}}": ctx.attr.destination_folder,
"{{site_packages_extra_files}}": " ".join(["'" + file.short_path + "'" for file in ctx.files.site_packages_extra_files]),
},
)

def _runfiles(ctx):
py_toolchain = ctx.toolchains[_PY_TOOLCHAIN]
runfiles = ctx.runfiles(
files = [ctx.file.requirements_txt],
files = [ctx.file.requirements_txt] + ctx.files.site_packages_extra_files,
transitive_files = py_toolchain.py3_runtime.files,
)
runfiles = runfiles.merge(ctx.attr._uv.default_runfiles)
Expand All @@ -35,6 +37,7 @@ def _venv_impl(ctx):
_venv = rule(
attrs = {
"destination_folder": attr.string(default = "venv"),
"site_packages_extra_files": attr.label_list(default = [], doc = "Files to add to the site-packages folder inside the virtual environment. Useful for adding `sitecustomize.py` or `.pth` files", allow_files = True),
"requirements_txt": attr.label(mandatory = True, allow_single_file = True),
"_uv": attr.label(default = "@multitool//tools/uv", executable = True, cfg = "exec"),
"_template": attr.label(default = "//uv/private:create_venv.sh", allow_single_file = True),
Expand All @@ -44,10 +47,11 @@ _venv = rule(
executable = True,
)

def create_venv(name, requirements_txt = None, target_compatible_with = None, destination_folder = None):
def create_venv(name, requirements_txt = None, target_compatible_with = None, destination_folder = None, site_packages_extra_files = []):
_venv(
name = name,
destination_folder = destination_folder,
site_packages_extra_files = site_packages_extra_files,
requirements_txt = requirements_txt or "//:requirements.txt",
target_compatible_with = target_compatible_with,
)

0 comments on commit 98a19d2

Please sign in to comment.