diff options
author | Gabriel White-Vega <gwhitevega@google.com> | 2023-09-08 16:53:36 -0400 |
---|---|---|
committer | Gabriel White-Vega <gwhitevega@google.com> | 2023-09-14 14:29:47 -0400 |
commit | 6ec6f1efe5dfdfb720f20ec58fa60ffc2bf8dc77 (patch) | |
tree | 835a20d437566ffe908e97fa2914273367e7368a | |
parent | 01603ca9e47faa979bd7133b0e8d805840272439 (diff) | |
download | bumble-6ec6f1efe5dfdfb720f20ec58fa60ffc2bf8dc77.tar.gz |
Add license header check for rust files
Added binary that can check for and add Apache 2.0 licenses.
Run this binary during the build-rust workflow.
-rw-r--r-- | .github/workflows/python-build-test.yml | 2 | ||||
-rw-r--r-- | rust/Cargo.lock | 159 | ||||
-rw-r--r-- | rust/Cargo.toml | 13 | ||||
-rw-r--r-- | rust/README.md | 2 | ||||
-rw-r--r-- | rust/pytests/assigned_numbers.rs | 14 | ||||
-rw-r--r-- | rust/src/adv.rs | 14 | ||||
-rw-r--r-- | rust/src/wrapper/logging.rs | 14 | ||||
-rw-r--r-- | rust/tools/file_header.rs | 78 |
8 files changed, 293 insertions, 3 deletions
diff --git a/.github/workflows/python-build-test.yml b/.github/workflows/python-build-test.yml index c8a1031..6afefa6 100644 --- a/.github/workflows/python-build-test.yml +++ b/.github/workflows/python-build-test.yml @@ -64,6 +64,8 @@ jobs: with: components: clippy,rustfmt toolchain: ${{ matrix.rust-version }} + - name: Check License Headers + run: cd rust && cargo run --features dev-tools --bin file-header check-all - name: Rust Build run: cd rust && cargo build --all-targets && cargo build --all-features --all-targets # Lints after build so what clippy needs is already built diff --git a/rust/Cargo.lock b/rust/Cargo.lock index bd168dc..c2d0cd3 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -131,6 +131,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] +name = "bstr" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2f7349907b712260e64b0afe2f84692af14a454be26187d9df565c7f69266a" +dependencies = [ + "memchr", + "serde", +] + +[[package]] name = "bumble" version = "0.1.0" dependencies = [ @@ -138,7 +148,9 @@ dependencies = [ "clap 4.4.1", "directories", "env_logger", + "file-header", "futures", + "globset", "hex", "itertools", "lazy_static", @@ -273,6 +285,73 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" [[package]] +name = "crossbeam" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2801af0d36612ae591caa9568261fddce32ce6e08a7275ea334a06a4ad021a2c" +dependencies = [ + "cfg-if", + "crossbeam-channel", + "crossbeam-deque", + "crossbeam-epoch", + "crossbeam-queue", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-channel" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a33c2bf77f2df06183c3aa30d1e96c0695a313d4f9c453cc3762a6db39f99200" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-deque" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6fd6f855243022dcecf8702fef0c297d4338e226845fe067f6341ad9fa0cef" +dependencies = [ + "cfg-if", + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae211234986c545741a7dc064309f67ee1e5ad243d0e48335adc0484d960bcc7" +dependencies = [ + "autocfg", + "cfg-if", + "crossbeam-utils", + "memoffset 0.9.0", + "scopeguard", +] + +[[package]] +name = "crossbeam-queue" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1cfb3ea8a53f37c40dea2c7bedcbd88bdfae54f5e2175d6ecaff1c988353add" +dependencies = [ + "cfg-if", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a22b2d63d4d1dc0b7f1b6b2747dd0088008a9be28b6ddf0b1e7d335e3037294" +dependencies = [ + "cfg-if", +] + +[[package]] name = "directories" version = "5.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -349,6 +428,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" [[package]] +name = "file-header" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5568149106e77ae33bc3a2c3ef3839cbe63ffa4a8dd4a81612a6f9dfdbc2e9f" +dependencies = [ + "crossbeam", + "lazy_static", + "license", + "thiserror", + "walkdir", +] + +[[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -485,6 +577,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6fb8d784f27acf97159b40fc4db5ecd8aa23b9ad5ef69cdd136d3bc80665f0c0" [[package]] +name = "globset" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "759c97c1e17c55525b57192c06a267cda0ac5210b222d6b82189a2338fa1c13d" +dependencies = [ + "aho-corasick", + "bstr", + "fnv", + "log", + "regex", +] + +[[package]] name = "h2" version = "0.3.21" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -711,6 +816,17 @@ dependencies = [ ] [[package]] +name = "license" +version = "3.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66615d42e949152327c402e03cd29dab8bff91ce470381ac2ca6d380d8d9946" +dependencies = [ + "reword", + "serde", + "serde_json", +] + +[[package]] name = "linux-raw-sys" version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -757,6 +873,15 @@ dependencies = [ ] [[package]] +name = "memoffset" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a634b1c61a95585bd15607c6ab0c4e5b226e695ff2800ba0cdccddf208c406c" +dependencies = [ + "autocfg", +] + +[[package]] name = "mime" version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1201,6 +1326,15 @@ dependencies = [ ] [[package]] +name = "reword" +version = "7.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe272098dce9ed76b479995953f748d1851261390b08f8a0ff619c885a1f0765" +dependencies = [ + "unicode-segmentation", +] + +[[package]] name = "rusb" version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1242,6 +1376,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" [[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] name = "schannel" version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1590,6 +1733,12 @@ dependencies = [ ] [[package]] +name = "unicode-segmentation" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" + +[[package]] name = "unindent" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1619,6 +1768,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" [[package]] +name = "walkdir" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71d857dc86794ca4c280d616f7da00d2dbfd8cd788846559a6813e6aa4b54ee" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] name = "want" version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 6c38c82..a553afd 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -24,6 +24,10 @@ itertools = "0.11.0" lazy_static = "1.4.0" thiserror = "1.0.41" +# Dev tools +file-header = { version = "0.1.2", optional = true } +globset = { version = "0.4.13", optional = true } + # CLI anyhow = { version = "1.0.71", optional = true } clap = { version = "4.3.3", features = ["derive"], optional = true } @@ -53,9 +57,14 @@ env_logger = "0.10.0" rustdoc-args = ["--generate-link-to-definition"] [[bin]] +name = "file-header" +path = "tools/file_header.rs" +required-features = ["dev-tools"] + +[[bin]] name = "gen-assigned-numbers" path = "tools/gen_assigned_numbers.rs" -required-features = ["bumble-codegen"] +required-features = ["dev-tools"] [[bin]] name = "bumble" @@ -71,7 +80,7 @@ harness = false [features] anyhow = ["pyo3/anyhow"] pyo3-asyncio-attributes = ["pyo3-asyncio/attributes"] -bumble-codegen = ["dep:anyhow"] +dev-tools = ["dep:anyhow", "dep:clap", "dep:file-header", "dep:globset"] # separate feature for CLI so that dependencies don't spend time building these bumble-tools = ["dep:clap", "anyhow", "dep:anyhow", "dep:directories", "pyo3-asyncio-attributes", "dep:owo-colors", "dep:reqwest", "dep:rusb", "dep:log", "dep:env_logger", "dep:futures"] default = [] diff --git a/rust/README.md b/rust/README.md index 23dec03..15a19b9 100644 --- a/rust/README.md +++ b/rust/README.md @@ -62,5 +62,5 @@ in tests at `pytests/assigned_numbers.rs`. To regenerate the assigned number tables based on the Python codebase: ``` -PYTHONPATH=.. cargo run --bin gen-assigned-numbers --features bumble-codegen +PYTHONPATH=.. cargo run --bin gen-assigned-numbers --features dev-tools ```
\ No newline at end of file diff --git a/rust/pytests/assigned_numbers.rs b/rust/pytests/assigned_numbers.rs index 10e7f3e..7f8f1d1 100644 --- a/rust/pytests/assigned_numbers.rs +++ b/rust/pytests/assigned_numbers.rs @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed 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. + use bumble::wrapper::{self, core::Uuid16}; use pyo3::{intern, prelude::*, types::PyDict}; use std::collections; diff --git a/rust/src/adv.rs b/rust/src/adv.rs index 8a4c979..6f84cc5 100644 --- a/rust/src/adv.rs +++ b/rust/src/adv.rs @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed 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. + //! BLE advertisements. use crate::wrapper::assigned_numbers::{COMPANY_IDS, SERVICE_IDS}; diff --git a/rust/src/wrapper/logging.rs b/rust/src/wrapper/logging.rs index 141cc04..bd932cb 100644 --- a/rust/src/wrapper/logging.rs +++ b/rust/src/wrapper/logging.rs @@ -1,3 +1,17 @@ +// Copyright 2023 Google LLC +// +// Licensed 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. + //! Bumble & Python logging use pyo3::types::PyDict; diff --git a/rust/tools/file_header.rs b/rust/tools/file_header.rs new file mode 100644 index 0000000..fb3286d --- /dev/null +++ b/rust/tools/file_header.rs @@ -0,0 +1,78 @@ +// Copyright 2023 Google LLC +// +// Licensed 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. + +use anyhow::anyhow; +use clap::Parser as _; +use file_header::{ + add_headers_recursively, check_headers_recursively, + license::spdx::{YearCopyrightOwnerValue, APACHE_2_0}, +}; +use globset::{Glob, GlobSet, GlobSetBuilder}; +use std::{env, path::PathBuf}; + +fn main() -> anyhow::Result<()> { + let rust_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?); + let ignore_globset = ignore_globset()?; + // Note: when adding headers, there is a bug where the line spacing is off for Apache 2.0 (see https://github.com/spdx/license-list-XML/issues/2127) + let header = APACHE_2_0.build_header(YearCopyrightOwnerValue::new(2023, "Google LLC".into())); + + let cli = Cli::parse(); + + match cli.subcommand { + Subcommand::CheckAll => { + let result = + check_headers_recursively(&rust_dir, |p| !ignore_globset.is_match(p), header, 4)?; + if result.has_failure() { + return Err(anyhow!( + "The following files do not have headers: {result:?}" + )); + } + } + Subcommand::AddAll => { + let files_with_new_header = + add_headers_recursively(&rust_dir, |p| !ignore_globset.is_match(p), header)?; + files_with_new_header + .iter() + .for_each(|path| println!("Added header to: {path:?}")); + } + } + Ok(()) +} + +fn ignore_globset() -> anyhow::Result<GlobSet> { + Ok(GlobSetBuilder::new() + .add(Glob::new("**/.idea/**")?) + .add(Glob::new("**/target/**")?) + .add(Glob::new("**/.gitignore")?) + .add(Glob::new("**/CHANGELOG.md")?) + .add(Glob::new("**/Cargo.lock")?) + .add(Glob::new("**/Cargo.toml")?) + .add(Glob::new("**/README.md")?) + .add(Glob::new("*.bin")?) + .build()?) +} + +#[derive(clap::Parser)] +struct Cli { + #[clap(subcommand)] + subcommand: Subcommand, +} + +#[derive(clap::Subcommand, Debug, Clone)] +enum Subcommand { + /// Checks if a license is present in files that are not in the ignore list. + CheckAll, + /// Adds a license as needed to files that are not in the ignore list. + AddAll, +} |