-
-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
246 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
load("//py/private:generate_devtools.bzl", _generate_devtools = "generate_devtools") | ||
load("//py/private:import.bzl", _py_import = "py_import") | ||
load("//py/private:pytest.bzl", _pytest_test = "pytest_test") | ||
load("//py/private:py_with_lint_macro.bzl", _py_binary = "py_binary", _py_library = "py_library") | ||
load("//py/private:suite.bzl", _py_test_suite = "py_test_suite") | ||
load("//py/private:black_config.bzl", _black_config = "black_config") | ||
|
||
black_config = _black_config | ||
generate_devtools = _generate_devtools | ||
pytest_test = _pytest_test | ||
py_binary = _py_binary | ||
py_library = _py_library | ||
py_import = _py_import | ||
py_test_suite = _py_test_suite |
This file contains 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 |
---|---|---|
@@ -1,10 +1,29 @@ | ||
load("@py_dev_requirements//:requirements.bzl", "requirement") | ||
load("@rules_python//python:defs.bzl", "py_binary") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
py_binary( | ||
name = "untar", | ||
srcs = [ | ||
"untar.py", | ||
], | ||
legacy_create_init = False, | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
alias( | ||
name = "black", | ||
actual = ":black-bin", | ||
) | ||
|
||
py_binary( | ||
name = "black-bin", | ||
srcs = [ | ||
# If the source is called `black` you can't then import black into black | ||
"black-bin.py", | ||
], | ||
legacy_create_init = False, | ||
deps = [ | ||
requirement("black"), | ||
], | ||
) |
This file contains 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,25 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import re | ||
import sys | ||
|
||
from black import patched_main | ||
|
||
if __name__ == "__main__": | ||
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0]) | ||
sys.exit(patched_main()) |
This file contains 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,31 @@ | ||
BlackConfigInfo = provider( | ||
fields = { | ||
"black": "binary: The binary to execute for black", | ||
"line_length": "`int`: Maximum line length", | ||
"python_version": "`array of strings`: The versions of Python to target", | ||
}, | ||
) | ||
|
||
def black_config_impl(ctx): | ||
return [ | ||
BlackConfigInfo( | ||
black = ctx.attr.black, | ||
line_length = ctx.attr.line_length, | ||
python_version = ctx.attr.python_version, | ||
), | ||
] | ||
|
||
black_config = rule( | ||
black_config_impl, | ||
attrs = { | ||
"black": attr.label( | ||
cfg = "exec", | ||
default = "//py/private:black", | ||
executable = True, | ||
), | ||
"line_length": attr.int( | ||
default = 88, | ||
), | ||
"python_version": attr.string_list(), | ||
}, | ||
) |
This file contains 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,63 @@ | ||
load(":black_config.bzl", "BlackConfigInfo") | ||
|
||
def black_test_impl(ctx): | ||
config = ctx.attr.config[BlackConfigInfo] | ||
|
||
all_inputs = [] | ||
black = config.black[DefaultInfo].files_to_run.executable | ||
all_inputs.append(black) | ||
|
||
args = ctx.actions.args() | ||
|
||
for version in config.python_version: | ||
args.add_all(["-t", version]) | ||
|
||
args.add_all(["--line-length", config.line_length]) | ||
|
||
args.add("--check") | ||
|
||
args.add_all(ctx.files.srcs) | ||
all_inputs.extend(ctx.files.srcs) | ||
|
||
# Run on a single core | ||
args.add_all(["--workers", "1"]) | ||
|
||
if config.python_version: | ||
args.add_all(["--target-version", config.python_version]) | ||
|
||
args.use_param_file("@%s", use_always = True) | ||
args_file = ctx.actions.declare_file("%s-black-params" % ctx.label.name) | ||
ctx.actions.write(args_file, args) | ||
all_inputs.append(args_file) | ||
|
||
output = ctx.actions.declare_file("%s-black-test" % ctx.label.name) | ||
ctx.actions.write( | ||
output = output, | ||
content = """#!/usr/bin/env bash | ||
%s $(< %s) """ % (black.short_path, args_file.short_path), | ||
is_executable = True, | ||
) | ||
|
||
return [DefaultInfo( | ||
executable = output, | ||
files = depset(), | ||
runfiles = ctx.runfiles(files = all_inputs).merge( | ||
config.black[DefaultInfo].default_runfiles, | ||
), | ||
)] | ||
|
||
black_test = rule( | ||
black_test_impl, | ||
attrs = { | ||
"srcs": attr.label_list( | ||
allow_files = [".py"], | ||
), | ||
"config": attr.label( | ||
mandatory = True, | ||
providers = [ | ||
[BlackConfigInfo], | ||
], | ||
), | ||
}, | ||
test = True, | ||
) |
This file contains 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,34 @@ | ||
load("@apple_rules_lint//lint:defs.bzl", "get_lint_config") | ||
load("@rules_python//python:defs.bzl", _py_binary = "py_binary", _py_library = "py_library", _py_test = "py_test") | ||
load(":black_test.bzl", "black_test") | ||
|
||
def create_lint_tests(name, **kwargs): | ||
config = get_lint_config("py-black", kwargs.get("tags", [])) | ||
if config: | ||
black_test( | ||
name = "%s-black" % name, | ||
srcs = kwargs.get("srcs", []), | ||
config = config, | ||
tags = kwargs.get("tags", []) + [ | ||
"lint", | ||
"black", | ||
"py-black", | ||
], | ||
) | ||
|
||
def _add_lint_tests(actual, name, **kwargs): | ||
actual( | ||
name = name, | ||
**kwargs | ||
) | ||
|
||
create_lint_tests(name, **kwargs) | ||
|
||
def py_binary(name, **kwargs): | ||
_add_lint_tests(_py_binary, name, **kwargs) | ||
|
||
def py_library(name, **kwargs): | ||
_add_lint_tests(_py_library, name, **kwargs) | ||
|
||
def py_test(name, **kwargs): | ||
_add_lint_tests(_py_test, name, **kwargs) |
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
async-generator==1.10 | ||
attrs==23.1.0 | ||
black==24.2.0 | ||
certifi==2023.7.22 | ||
cffi==1.16.0 | ||
cryptography==42.0.4 | ||
|
This file contains 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
This file contains 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