-
Notifications
You must be signed in to change notification settings - Fork 4
Feature: Python Rules #92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KetanReddy
wants to merge
16
commits into
main
Choose a base branch
from
feature/python-rules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5781113
initial build out
KetanReddy b614c89
fix issue getting local dep version
KetanReddy 25793fa
fix issue with packaging srcs
KetanReddy 7301687
Fix local package references and pulling versions
KetanReddy da40397
move version cleaning to pipeline
KetanReddy 334f869
only use test files as entrypoints but source all of them
KetanReddy d6c3371
fix tests
KetanReddy 2fb9b6f
Take in lint file as arg
KetanReddy 5870cb2
lint
KetanReddy 453c8b9
FIx coverage collection for test stage
KetanReddy ca55ed6
support for test helpers
KetanReddy d6749fd
Fix local version for local packages in requirements file generation
KetanReddy 8a83233
Use mocked package for distribution
KetanReddy 0255222
lint
KetanReddy cbd5c35
python rule examples/test. Update other examples
KetanReddy 6be94c8
fix coverage for the last time
KetanReddy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
| load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_missing_pkgs", "bzlformat_pkg") | ||
|
|
||
| bzlformat_pkg(name = "bzlformat") | ||
|
|
||
| bzlformat_missing_pkgs(name = "bzlformat_missing_pkgs") | ||
|
|
||
| exports_files(["defs.bzl"]) | ||
|
|
||
| bzl_library( | ||
| name = "defs", | ||
| srcs = ["defs.bzl"], | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//python/private:py_pipeline", | ||
| ], | ||
| ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| """ | ||
| Public API for shell Bazel rules. | ||
| """ | ||
|
|
||
| load("//python/private:py_pipeline.bzl", _py_pipeline = "py_pipeline") | ||
|
|
||
| py_pipeline = _py_pipeline |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| load("@aspect_rules_js//js:defs.bzl", "js_binary") | ||
| load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
| load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_missing_pkgs", "bzlformat_pkg") | ||
|
|
||
| bzlformat_pkg(name = "bzlformat") | ||
|
|
||
| bzlformat_missing_pkgs(name = "bzlformat_missing_pkgs") | ||
|
|
||
| filegroup( | ||
| name = "all_files", | ||
| srcs = glob(["*"]), | ||
| visibility = ["//:__subpackages__"], | ||
| ) | ||
|
|
||
| js_binary( | ||
| name = "build_requirements", | ||
| entry_point = "build_requirements.mjs", | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| exports_files( | ||
| glob(["*.bzl"]), | ||
| visibility = ["//docs:__pkg__"], | ||
| ) | ||
|
|
||
| exports_files( | ||
| [ | ||
| "pytest_wrapper.py.tmpl", | ||
| "pytest_wrapper.py", | ||
| ], | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| bzl_library( | ||
| name = "py_pipeline", | ||
| srcs = ["py_pipeline.bzl"], | ||
| visibility = [ | ||
| "//docs:__subpackages__", | ||
| "//python:__subpackages__", | ||
| ], | ||
| deps = [ | ||
| ], | ||
| ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """ | ||
| Populate all the required fields for a package.json file needed for a full build | ||
| """ | ||
|
|
||
| load("@aspect_bazel_lib//lib:stamping.bzl", "STAMP_ATTRS", "maybe_stamp") | ||
|
|
||
| def _build_requirements_impl(ctx): | ||
| """ Builds a requirements.txt from the base and specified deps""" | ||
| output_file = ctx.actions.declare_file("requires.txt") | ||
| stamp = maybe_stamp(ctx) | ||
|
|
||
| stamp_inputs = [] | ||
|
|
||
| args = { | ||
| "input": ctx.file.root_requirements.short_path, | ||
| "output": output_file.short_path, | ||
| "package_names": ctx.attr.package_names, | ||
| "substitutions": ctx.attr.substitutions, | ||
| } | ||
|
|
||
| if stamp: | ||
| stamp_inputs = [stamp.volatile_status_file, stamp.stable_status_file] | ||
| args["BAZEL_STABLE_STATUS_FILE"] = stamp.stable_status_file.path | ||
| args["BAZEL_VOLATILE_STATUS_FILE"] = stamp.volatile_status_file.path | ||
|
|
||
| ctx.actions.run( | ||
| inputs = stamp_inputs + ctx.files.root_requirements, | ||
| env = { | ||
| "BAZEL_BINDIR": ctx.bin_dir.path, | ||
| }, | ||
| arguments = [json.encode(args)], | ||
| outputs = [output_file], | ||
| executable = ctx.executable._build_requirements, | ||
| ) | ||
|
|
||
| return [ | ||
| DefaultInfo( | ||
| files = depset([output_file]), | ||
| ), | ||
| ] | ||
|
|
||
| build_requirements = rule( | ||
| implementation = _build_requirements_impl, | ||
| attrs = dict({ | ||
| "package_names": attr.string_list( | ||
| doc = "A list of packages to parse out of the requirements.txt file.", | ||
| mandatory = True, | ||
| ), | ||
| "root_requirements": attr.label( | ||
| mandatory = True, | ||
| doc = "The root requirements.txt for the project. Used to get the versions of dependencies", | ||
| allow_single_file = ["requirements.txt"], | ||
| ), | ||
| "substitutions": attr.string_dict(default = {}), | ||
| "_build_requirements": attr.label( | ||
| executable = True, | ||
| cfg = "exec", | ||
| default = "//python/private:build_requirements", | ||
| ), | ||
| }, **STAMP_ATTRS), | ||
| ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| import path from "node:path"; | ||
| import fs from "node:fs"; | ||
|
|
||
| const REQUIREMENTSREGEX = | ||
| /^([a-zA-Z0-9._-]+)(?:([<>=!~]+)([^,\s]+(?:,[<>=!~]+[^,\s]+)*))?\s*(?:#.*)?$/g; | ||
|
|
||
|
|
||
| /** Adds support for replacing process.env.* references with stamped values from bazel */ | ||
| function getStampedSubstitutions( | ||
| stableStatusFile, | ||
| volatileStatusFile | ||
| ) { | ||
| const substitutions = {}; | ||
|
|
||
| [stableStatusFile, volatileStatusFile].forEach((statusFile) => { | ||
| if (!fs.existsSync(statusFile)) { | ||
| return; | ||
| } | ||
|
|
||
| const contents = fs.readFileSync(statusFile, "utf-8"); | ||
|
|
||
| contents.split("\n").forEach((statusLine) => { | ||
| if (!statusLine.trim()) { | ||
| return; | ||
| } | ||
|
|
||
| const firstSpace = statusLine.indexOf(" "); | ||
| const varName = statusLine.substring(0, firstSpace); | ||
| const varVal = statusLine.substring(firstSpace + 1); | ||
| substitutions[varName] = varVal; | ||
| }); | ||
| }); | ||
|
|
||
| return substitutions; | ||
| } | ||
| function getStampVars({ | ||
| substitutions, | ||
| BAZEL_STABLE_STATUS_FILE, | ||
| BAZEL_VOLATILE_STATUS_FILE, | ||
| }) { | ||
|
|
||
| if(!BAZEL_STABLE_STATUS_FILE || !BAZEL_VOLATILE_STATUS_FILE){ | ||
| return {} | ||
| } | ||
|
|
||
| const stableStatusFile = path.join( | ||
| process.env.JS_BINARY__EXECROOT, | ||
| BAZEL_STABLE_STATUS_FILE | ||
| ); | ||
| const volatileStatusFile = path.join( | ||
| process.env.JS_BINARY__EXECROOT, | ||
| BAZEL_VOLATILE_STATUS_FILE | ||
| ); | ||
|
|
||
| if (!fs.existsSync(stableStatusFile) || !fs.existsSync(volatileStatusFile)) { | ||
| return {} | ||
| } | ||
|
|
||
| const customSubstitutions = getStampedSubstitutions( | ||
| substitutions, | ||
| stableStatusFile, | ||
| volatileStatusFile | ||
| ); | ||
|
|
||
| return customSubstitutions; | ||
| } | ||
|
|
||
| async function main(args) { | ||
| const { JS_BINARY__EXECROOT } = process.env; | ||
| const { | ||
| input, | ||
| output, | ||
| package_names, | ||
| BAZEL_STABLE_STATUS_FILE, | ||
| BAZEL_VOLATILE_STATUS_FILE | ||
| } = args; | ||
|
|
||
| // Get stamp vars | ||
| const stampVars = getStampVars({ | ||
| BAZEL_STABLE_STATUS_FILE, | ||
| BAZEL_VOLATILE_STATUS_FILE, | ||
| }); | ||
|
|
||
| const local_version = stampVars['STABLE_VERSION'] ?? "0.0.0" | ||
|
|
||
| // clean up canary names to follow pep | ||
| local_version.replace("--",".").replace("-",".") | ||
|
|
||
| // Build map of requirement name to version | ||
| const requirementsFile = await fs.promises.readFile( | ||
| path.join(JS_BINARY__EXECROOT, input), | ||
| "utf-8", | ||
| ); | ||
| const requirementsMap = new Map(); | ||
|
|
||
| requirementsFile.split("\n").map((requirement) => { | ||
| const result = Array.from(requirement.matchAll(REQUIREMENTSREGEX), (m) => | ||
| m.slice(1), | ||
| )[0]; | ||
| if (result) { | ||
| const [pkg, range, version] = result; | ||
| requirementsMap.set(pkg, `${pkg}${range}${version}`); | ||
| } | ||
| }); | ||
|
|
||
| // map requirements to version | ||
| const parsedRequirements = []; | ||
|
|
||
| //clean up requirement names that are passed in like @@rules_python++pip+pypi//pytest:pkg | ||
| const cleanedPackages = package_names.map((p) => { | ||
| if(p.startsWith("//")){ | ||
| const localPackageName = p.split(":")[1].replace("_library", "") | ||
| // if its a local package we can handle it directly | ||
| requirementsMap.set(localPackageName, `${localPackageName}==${local_version}`); | ||
| return localPackageName | ||
| } else { | ||
| return p.split("//")[1].split(":")[0] | ||
| } | ||
| }); | ||
|
|
||
|
|
||
| cleanedPackages.forEach((pkg) => { | ||
| if (requirementsMap.has(pkg)) { | ||
| parsedRequirements.push(`${requirementsMap.get(pkg)}`); | ||
| } | ||
| }); | ||
|
|
||
| // write results | ||
| await fs.promises.mkdir(path.dirname(output), { recursive: true }); | ||
| await fs.promises.writeFile(output, parsedRequirements.join("\n")); | ||
| } | ||
|
|
||
| main(JSON.parse(process.argv[2])).catch((e) => { | ||
| console.error(e); | ||
| process.exit(1); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.