Skip to content

Commit

Permalink
parse_meta_from_tarball python bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankland committed Nov 25, 2024
1 parent 25ce229 commit 1251bff
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 27 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions bundle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = { version = "1.0.215", default-features = false, features = ["derive"] }
serde_json = "1.0.133"
tsify-next = { version = "0.5.4", optional = true }
wasm-bindgen = { version = "0.2.95", optional = true }
pyo3-stub-gen = { version = "0.6.0", optional = true }

# For encoding
async-compression = { version = "0.4.17", features = ["futures-io", "zstd"] }
Expand All @@ -27,6 +28,14 @@ tar = { version = "0.4.30", default-features = false }
uuid = { version = "1.10.0", features = ["v5"] }
zstd = { version = "0.13.0", default-features = false }

[target.'cfg(target_os = "linux")'.dependencies]
pyo3 = { version = "0.22.5", optional = true, features = [
"abi3-py39",
"extension-module",
] }

[target.'cfg(target_os = "macos")'.dependencies]
pyo3 = { version = "0.22.5", optional = true, features = ["abi3-py39"] }

[features]
bindings = []
Expand All @@ -38,3 +47,10 @@ wasm = [
"codeowners/wasm",
"context/wasm",
]
pyo3 = [
"bindings",
"dep:pyo3",
"dep:pyo3-stub-gen",
"codeowners/pyo3",
"context/pyo3",
]
19 changes: 14 additions & 5 deletions bundle/src/bundle_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
use codeowners::CodeOwners;
use context::repo::BundleRepo;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pyclass_enum};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[cfg(feature = "wasm")]
Expand All @@ -16,7 +20,8 @@ use crate::{files::FileSet, CustomTag, Test};

pub const META_VERSION: &str = "1";
// 0.5.29 was first version to include bundle_upload_id and serves as the base
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct BundleMetaBaseProps {
pub version: String,
Expand All @@ -33,21 +38,24 @@ pub struct BundleMetaBaseProps {
pub quarantined_tests: Vec<Test>,
pub codeowners: Option<CodeOwners>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct BundleMetaV0_5_29 {
#[serde(flatten)]
pub base_props: BundleMetaBaseProps,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct BundleMetaJunitProps {
pub num_files: usize,
pub num_tests: usize,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct BundleMetaV0_5_34 {
#[serde(flatten)]
Expand All @@ -56,7 +64,8 @@ pub struct BundleMetaV0_5_34 {
pub junit_props: BundleMetaJunitProps,
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass_enum, pyclass(eq))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
#[cfg_attr(feature = "wasm", tsify(into_wasm_abi, from_wasm_abi))]
#[serde(tag = "schema")]
Expand Down
15 changes: 10 additions & 5 deletions bundle/src/files.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use std::{format, time::SystemTime};
#[cfg(feature = "wasm")]
use tsify_next::Tsify;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use codeowners::{CodeOwners, Owners, OwnersOfPath};
use constants::ALLOW_LIST;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
use pyo3_stub_gen::derive::gen_stub_pyclass;
use regex::Regex;
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use tsify_next::Tsify;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use crate::types::{BundledFile, FileSetType};

Expand All @@ -28,7 +32,8 @@ impl FileSetCounter {
}
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct FileSet {
pub file_set_type: FileSetType,
Expand Down
19 changes: 13 additions & 6 deletions bundle/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use context::repo::BundleRepo;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
use pyo3_stub_gen::derive::{gen_stub_pyclass, gen_stub_pyclass_enum};
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use tsify_next::Tsify;
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;

use context::repo::BundleRepo;
use serde::{Deserialize, Serialize};

pub struct RunResult {
pub exit_code: i32,
pub failures: Vec<Test>,
Expand All @@ -17,7 +20,8 @@ pub struct QuarantineRunResult {
pub quarantine_status: QuarantineBulkTestStatus,
}

#[derive(Debug, Serialize, Clone, Deserialize)]
#[derive(Debug, Serialize, Clone, Deserialize, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct Test {
pub name: String,
Expand Down Expand Up @@ -85,7 +89,8 @@ pub struct BundleUploader {
pub org_slug: String,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass_enum, pyclass(eq, eq_int))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub enum FileSetType {
#[default]
Expand All @@ -94,7 +99,8 @@ pub enum FileSetType {

#[cfg(feature = "wasm")]
// u128 will be supported in the next release after 0.2.95
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct BundledFile {
pub original_path: String,
Expand Down Expand Up @@ -131,6 +137,7 @@ impl BundledFile {
/// Custom tags defined by the user.
///
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass(get_all))]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct CustomTag {
pub key: String,
Expand Down
11 changes: 11 additions & 0 deletions codeowners/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,18 @@ serde = { version = "1.0.215", default-features = false, features = ["derive"] }
serde_json = "1.0.133"
tsify-next = { version = "0.5.4", optional = true }
wasm-bindgen = { version = "0.2.95", optional = true }
pyo3-stub-gen = { version = "0.6.0", optional = true }

[target.'cfg(target_os = "linux")'.dependencies]
pyo3 = { version = "0.22.5", optional = true, features = [
"abi3-py39",
"extension-module",
] }

[target.'cfg(target_os = "macos")'.dependencies]
pyo3 = { version = "0.22.5", optional = true, features = ["abi3-py39"] }

[features]
bindings = []
wasm = ["bindings", "dep:wasm-bindgen", "dep:js-sys", "dep:tsify-next"]
pyo3 = ["bindings", "dep:pyo3", "dep:pyo3-stub-gen"]
14 changes: 10 additions & 4 deletions codeowners/src/codeowners.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use constants::CODEOWNERS_LOCATIONS;
use serde::{Deserialize, Serialize};
use std::{
fs::File,
path::{Path, PathBuf},
};

use constants::CODEOWNERS_LOCATIONS;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
#[cfg(feature = "pyo3")]
use pyo3_stub_gen::derive::gen_stub_pyclass;
use serde::{Deserialize, Serialize};
#[cfg(feature = "wasm")]
use tsify_next::Tsify;
#[cfg(feature = "wasm")]
Expand All @@ -12,7 +17,8 @@ use wasm_bindgen::prelude::*;
use crate::{github::GitHubOwners, gitlab::GitLabOwners, traits::FromReader};

// TODO(TRUNK-13628): Implement serializing and deserializing for CodeOwners
#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq)]
#[derive(Default, Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "pyo3", gen_stub_pyclass, pyclass)]
#[cfg_attr(feature = "wasm", derive(Tsify))]
pub struct CodeOwners {
pub path: PathBuf,
Expand Down Expand Up @@ -61,7 +67,7 @@ impl CodeOwners {
}
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Owners {
GitHubOwners(GitHubOwners),
GitLabOwners(GitLabOwners),
Expand Down
4 changes: 2 additions & 2 deletions codeowners/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{FromPath, FromReader, OwnersOfPath};
/// raw
/// );
/// ```
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq)]
pub enum GitHubOwner {
/// Owner in the form @username
Username(String),
Expand Down Expand Up @@ -67,7 +67,7 @@ impl FromStr for GitHubOwner {
}

/// Mappings of GitHub owners to path patterns
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Eq)]
pub struct GitHubOwners {
paths: Vec<(Pattern, Vec<GitHubOwner>)>,
}
Expand Down
2 changes: 1 addition & 1 deletion codeowners/src/gitlab/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum ErrorType {
InvalidSectionFormat,
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Error {
message: String,
line_number: usize,
Expand Down
2 changes: 1 addition & 1 deletion codeowners/src/gitlab/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{Entry, Error, Section, SectionParser};
pub type ParsedData = IndexMap<String, IndexMap<String, Entry>>;

/// Reference: https://gitlab.com/gitlab-org/gitlab/-/blob/master/ee/lib/gitlab/code_owners/file.rb
#[derive(Debug, Clone, Default, PartialEq)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct File {
path: PathBuf,
errors: Vec<Error>,
Expand Down
2 changes: 1 addition & 1 deletion codeowners/src/gitlab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl fmt::Display for GitLabOwner {
}
}

#[derive(Debug, Clone, Default, PartialEq)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GitLabOwners {
file: File,
}
Expand Down
3 changes: 3 additions & 0 deletions context-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ name = "stub_gen"
path = "bin/stub_gen.rs"

[dependencies]
bundle = { path = "../bundle", default-features = false, features = ["pyo3"] }
context = { path = "../context", features = ["git-access", "pyo3"] }
pyo3-stub-gen = "0.6.0"
futures-io = "0.3.31"
tokio = { version = "*", default-features = false, features = ["rt"] }

[target.'cfg(target_os = "linux")'.dependencies]
pyo3 = { version = "0.22.5", features = ["abi3-py39", "extension-module"] }
Expand Down
14 changes: 14 additions & 0 deletions context-py/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use std::{collections::HashMap, io::BufReader};

use bundle::{parse_meta_from_tarball as parse_tarball, VersionedBundle};
use context::{env, junit, repo};
use pyo3::{exceptions::PyTypeError, prelude::*};
use pyo3_stub_gen::{define_stub_info_gatherer, derive::gen_stub_pyfunction};

mod py_bytes_read;

use py_bytes_read::PyBytesReader;

define_stub_info_gatherer!(stub_info);

#[gen_stub_pyfunction]
Expand Down Expand Up @@ -68,6 +73,15 @@ fn repo_validate(bundle_repo: repo::BundleRepo) -> repo::validator::RepoValidati
repo::validator::validate(&bundle_repo)
}

#[gen_stub_pyfunction]
#[pyfunction]
pub fn parse_meta_from_tarball(py: Python<'_>, reader: PyObject) -> PyResult<VersionedBundle> {
let py_bytes_reader = PyBytesReader::new(reader.into_bound(py))?;
let rt = tokio::runtime::Runtime::new()?;
rt.block_on(parse_tarball(py_bytes_reader))
.map_err(|err| PyTypeError::new_err(err.to_string()))
}

#[pymodule]
fn context_py(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<env::parser::CIPlatform>()?;
Expand Down
Loading

0 comments on commit 1251bff

Please sign in to comment.