Skip to content

Commit

Permalink
zbus-proxies: add login1 proxy (#256)
Browse files Browse the repository at this point in the history
zbus-proxies: initial commit
  • Loading branch information
TheButlah authored Oct 17, 2024
1 parent 0cb7c16 commit f13df5b
Show file tree
Hide file tree
Showing 17 changed files with 1,246 additions and 17 deletions.
35 changes: 22 additions & 13 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ members = [
"update-verifier",
"verity-tree-calc",
"wpa-supplicant",
"zbus-proxies",
"zbus-proxies/cli",
]

[workspace.package]
Expand All @@ -49,6 +51,7 @@ console-subscriber = "0.4"
data-encoding = "2.3"
derive_more = { version = "0.99", default-features = false, features = ["display", "from"] }
eyre = "0.6.12"
ftdi-embedded-hal = { version = "0.22.0", features = ["libftd2xx", "libftd2xx-static"] }
futures = "0.3.30"
libc = "0.2.153"
nix = { version = "0.28", default-features = false, features = [] }
Expand All @@ -67,15 +70,15 @@ tracing = "0.1"
tracing-journald = "0.3.0"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
zbus = { version = "4.4.0", default-features = false, features = ["tokio"] }
ftdi-embedded-hal = { version = "0.22.0", features = ["libftd2xx", "libftd2xx-static"] }
zbus_systemd = "0.25600.0"

can-rs.path = "can"
orb-attest-dbus.path = "orb-attest/dbus"
orb-build-info.path = "build-info"
orb-const-concat.path = "const-concat"
orb-security-utils.path = "security-utils"
orb-slot-ctrl.path = "orb-slot-ctrl"
orb-attest-dbus.path = "orb-attest/dbus"
orb-zbus-proxies.path = "zbus-proxies"

[workspace.dependencies.orb-messages]
git = "https://github.com/worldcoin/orb-messages"
Expand Down
3 changes: 2 additions & 1 deletion nix/shells/development.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ in
cargo-expand # Useful for inspecting macros
cargo-zigbuild # Used to cross compile rust
dpkg # Used to test outputs of cargo-deb
mdbook-mermaid # Adds mermaid support
mdbook # Generates site for docs
mdbook-mermaid # Adds mermaid support
nixpkgs-fmt # Nix autoformatter
protobuf # Needed for orb-messages and other protobuf dependencies
python3
zbus-xmlgen # Used by `orb-zbus-proxies`
zig # Needed for cargo zigbuild

# This is missing on mac m1 nix, for some reason.
Expand Down
2 changes: 1 addition & 1 deletion update-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ tracing = "0.1.37"
tracing-journald.workspace = true
tracing-subscriber = { workspace = true, features = ["env-filter"] }
zbus.workspace = true
zbus_systemd = { version = "0.0.16", features = ["login1"] }
zbus_systemd = { workspace = true, features = ["login1"] }

[dependencies.reqwest]
workspace = true
Expand Down
18 changes: 18 additions & 0 deletions zbus-proxies/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "orb-zbus-proxies"
version = "0.0.0"
description = "Blocking Zbus proxies for third party services"
publish = false

edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

[features]
login1 = []

[dependencies]
zbus = { workspace = true, default-features = false }
tracing.workspace = true
thiserror.workspace = true
22 changes: 22 additions & 0 deletions zbus-proxies/cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "orb-zbus-proxies-cli"
version = "0.0.0"
description = "CLI tool to generate zbus proxies for third party services"
publish = false

edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true

[features]

[dependencies]
clap.workspace = true
color-eyre.workspace = true
orb-build-info.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true

[build-dependencies]
orb-build-info = { workspace = true, features = ["build-script"] }
3 changes: 3 additions & 0 deletions zbus-proxies/cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# orb-zbus-proxies CLI

CLI tool to generate the zbus proxies from the xml files.
3 changes: 3 additions & 0 deletions zbus-proxies/cli/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
orb_build_info::initialize().expect("failed to initialize")
}
152 changes: 152 additions & 0 deletions zbus-proxies/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
use std::path::{Path, PathBuf};

use clap::{
builder::{styling::AnsiColor, Styles},
Parser,
};
use color_eyre::{
eyre::{ensure, OptionExt as _, WrapErr as _},
Result, Section, SectionExt as _,
};
use orb_build_info::{make_build_info, BuildInfo};
use tracing::{info, level_filters::LevelFilter};
use tracing_subscriber::{
fmt, layer::SubscriberExt as _, util::SubscriberInitExt as _, EnvFilter,
};

const EXPECTED_ZBUS_XMLGEN_VERSION: &str = "4.1.0";
const BUILD_INFO: BuildInfo = make_build_info!();

#[derive(Debug, clap::Parser)]
#[clap(
version = BUILD_INFO.version,
about,
styles = clap_v3_styles(),
)]
struct Args {}

fn clap_v3_styles() -> Styles {
Styles::styled()
.header(AnsiColor::Yellow.on_default())
.usage(AnsiColor::Green.on_default())
.literal(AnsiColor::Green.on_default())
.placeholder(AnsiColor::Green.on_default())
}

fn find_xml_files(dir: &Path) -> Result<Vec<PathBuf>> {
let mut files = Vec::new();
for entry in std::fs::read_dir(dir)? {
let entry = entry?;
if !entry.file_type()?.is_file() {
continue;
}
let file_name = entry.file_name().to_string_lossy().into_owned();
if !file_name.ends_with(".xml") {
continue;
}
files.push(entry.path());
}

Ok(files)
}

fn check_zbus_xmlgen_version() -> Result<()> {
let output = std::process::Command::new("zbus-xmlgen")
.arg("--version")
.output()
.wrap_err("failed to run `zbus-xmlgen --version`")
.with_suggestion(|| "is zbus-xmlgen installed?")?;
ensure!(
output.status.success(),
"nonzero exit code for `zbus-xmlgen --version`"
);
let stdout = String::from_utf8_lossy(&output.stdout).trim().to_owned();
let stripped = stdout
.strip_prefix("zbus-xmlgen ")
.or_else(|| stdout.strip_prefix("zbus_xmlgen "))
.unwrap_or(&stdout);

(stripped == EXPECTED_ZBUS_XMLGEN_VERSION)
.then_some(())
.ok_or_eyre("unexpected command output")
.section(stdout.clone().header("stdout:"))
}

fn generate_code_from_xml(xml_file: &Path, output_path: &Path) -> Result<()> {
let exit_status = std::process::Command::new("zbus-xmlgen")
.arg("file")
.arg(xml_file.as_os_str())
.arg("--output")
.arg(output_path)
.status()
.wrap_err_with(|| {
format!("failed to run `zbus-xmlgen file {}`", xml_file.display())
})?;
ensure!(exit_status.success(), "command failed");

Ok(())
}

fn main() -> Result<()> {
color_eyre::install()?;
tracing_subscriber::registry()
.with(fmt::layer())
.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
)
.init();

let _args = Args::parse();

let xml_folder = dbg!(Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("xml"));

let generated_folder = dbg!(Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("src")
.join("generated"));

let tweaked_folder = dbg!(Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("src")
.join("tweaked"));

info!(
"Listing contents of xml folder located at {}...",
xml_folder.display()
);
ensure!(xml_folder.exists());

let files = find_xml_files(&xml_folder)
.wrap_err("error while searching through xml directory")
.with_note(|| format!("xml directory was {}", xml_folder.display()))?;

info!("{files:#?}");

check_zbus_xmlgen_version()?;
files
.iter()
.map(|xml_file_path| {
let rust_file_name = format!(
"{}.rs",
xml_file_path.file_stem().unwrap().to_string_lossy()
);
(xml_file_path, rust_file_name)
})
// skip files that exist under `tweaked_folder`
.filter(|(_, rust_file_name)| !tweaked_folder.join(rust_file_name).exists())
.try_for_each(|(xml_file_path, rust_file_name)| {
let out_file = generated_folder.join(rust_file_name);
info!("Generating {out_file:?} from {xml_file_path:?}");
generate_code_from_xml(xml_file_path, &out_file)
})
.wrap_err("error while generating code")?;

Ok(())
}
7 changes: 7 additions & 0 deletions zbus-proxies/src/generated/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated proxy code

All files in this directory are fully autogenerated with zbus-xmlgen WITH NO
MANUAL EDITS.

If you want to tweak the file afterwards, please put it in the [tweaked](../tweaked)
directory.
5 changes: 5 additions & 0 deletions zbus-proxies/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![forbid(unsafe_code)]
#![expect(clippy::type_complexity)]

#[cfg(feature = "login1")]
pub mod login1;
9 changes: 9 additions & 0 deletions zbus-proxies/src/login1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// This code has been tweaked after being autogenerated. List of manual tweaks:
/// - (1) rename WallMessage setter and getter to not conflict with the
/// `set_wall_message` method.
#[path = "../tweaked/login1.rs"]
pub mod proxy;

pub mod shutdown;

pub use self::proxy::{ManagerProxy, ManagerProxyBlocking};
Loading

0 comments on commit f13df5b

Please sign in to comment.